Last active
January 30, 2018 21:51
-
-
Save dev-kperera/7dfde9a51f955b92c4033aa446bd5867 to your computer and use it in GitHub Desktop.
Azure Functions with SharePoint Online CSOM C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Microsoft.SharePoint.Client.Runtime.dll" | |
#r "Microsoft.SharePoint.Client.dll" | |
using System; | |
using System.Security; | |
using System.Configuration; | |
using Microsoft.SharePoint.Client; | |
public static void Run(TimerInfo myTimer, TraceWriter log) | |
{ | |
string userName = ConfigurationManager.AppSettings["User"]; | |
string password = ConfigurationManager.AppSettings["Pass"]; | |
string spSite = ConfigurationManager.AppSettings["SharePointSiteUrl"]; | |
const string listName = "List00"; | |
const string listNameDestination = "List01"; | |
log.Info($"Trigger function executed at: {DateTime.Now} with {userName} | {password} on {spSite}"); | |
using (ClientContext ctx = new ClientContext(spSite)) | |
{ | |
// Authenticating to SPO | |
SecureString securePassword = new SecureString(); | |
foreach (char c in password.ToCharArray()) | |
securePassword.AppendChar(c); | |
ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword); | |
// Getting all SPO list items | |
List myList = ctx.Web.Lists.GetByTitle(listName); | |
CamlQuery query = CamlQuery.CreateAllItemsQuery(100); | |
ListItemCollection collListItem = myList.GetItems(query); | |
ctx.Load(collListItem); | |
ctx.ExecuteQuery(); | |
// Destination list item - create list items | |
List destinationList = ctx.Web.Lists.GetByTitle(listNameDestination); | |
ListItemCreationInformation itemCreateInfo; | |
foreach (ListItem oListItem in collListItem) | |
{ | |
// Getting list item | |
string customColumnValue = Convert.ToString(oListItem["CustomColumn"]); | |
log.Info($" List Item retrieved from {listName} => {customColumnValue}"); | |
// Writing values to destination list which are retrieved | |
if (!string.IsNullOrEmpty(customColumnValue)){ | |
itemCreateInfo = new ListItemCreationInformation(); | |
ListItem newItem = destinationList.AddItem(itemCreateInfo); | |
newItem["Title"] = customColumnValue; | |
newItem.Update(); | |
log.Info($" Updating column Title with {listNameDestination} with {customColumnValue}"); | |
ctx.ExecuteQuery(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment