Question

My scenario: I have setup a webhook on a list and each time a new folder is created,my azure function is called(which is acting as the notification hub),where i am using the changequery to get the changes on the list.I am able to get the ObjectType and ChangeType of the event that occured in the SPList,buti am not able to figure out how to find out the ids of the newly created folder and hence do some manipulation.

Was it helpful?

Solution 2

This works for me:

try {
    log.Info("Executing function");

    using (var ctx = await csomHelper.GetClientContext("ur url to a site"))
    {

   ListCollection list = ctx.Web.Lists; 
    List targetlist = list.GetByTitle("Company"); 
    ChangeQuery cq = new ChangeQuery(false, false);
    cq.Item = true;
    cq.Folder = true;
    cq.File = true;

    cq.List = true;           
    cq.Add = true;           
    ctx.Load(list);
    ctx.ExecuteQuery();
    ChangeCollection coll = targetlist.GetChanges(cq);
    ctx.Load(coll);
    ctx.ExecuteQuery();

     foreach (Change change in coll)
            {
                if (change is ChangeItem)
                {

                    ListItem changedListItem = targetlist.GetItemById((change as ChangeItem).ItemId); 
                    ctx.Load(changedListItem); 
                    ctx.ExecuteQuery();
                    Folder folder=changedListItem.Folder;
                    ctx.Load(folder); 
                    ctx.ExecuteQuery();
                    log.Info("folder is "+folder.UniqueId);


                }


            }


    }

OTHER TIPS

I have created a step by step article in TechNet that simulates SharePoint Webhooks as Event Receivers for SharePoint Online List. You can find the implementation under the heading SharePoint Webhooks as Event Receivers in the above mentioned article.

You can get the item id as (change as ChangeItem).ItemId} . Please see the below code :

foreach (Change change in changes)
               {
                   if (change is ChangeItem)
                      {
                       ListItem changedListItem = changedList.GetItemById((change as ChangeItem).ItemId); 
                       SPClientContext.Load(changedListItem); 
                       SPClientContext.ExecuteQuery();

                       string emailBody = "<b>New Change in the SLA List</b> </br><table style='border: 1px solid black;'>";
                       emailBody +=   "<table style='border: 1px solid black;'><tr style='color:green;'><td>ID :"+ (change as ChangeItem).ItemId +" -- </td><td><b> Action Item : "+ changedListItem["Title"]+"</b></td><td> -- SLA : "+ changedListItem["SLADate"]+"</td></tr>";
                       emailBody +=  "</table>"; 

                       Microsoft.SharePoint.Client.Utilities.EmailProperties emailProperties = new Microsoft.SharePoint.Client.Utilities.EmailProperties();
                       emailProperties.To = new string[] { "Priyaranjan@SharePointChronicle.com" };
                       emailProperties.Subject = "Attention : A new modification has occurred in the SLA List";
                       emailProperties.Body = emailBody;
                       Microsoft.SharePoint.Client.Utilities.Utility.SendEmail(SPClientContext, emailProperties);

                       SPClientContext.ExecuteQuery();
                            log.Info($"A list item with ID : { (change as ChangeItem).ItemId} is {change.ChangeType.ToString() }" );
                      }
               }

As you can see I have got the item ID in the Azure Logs : enter image description here

Updated

To answer your question on how to get the folder from the list item id, you can create the list item object and cast it to the folder object using ListItem.Folder property

Reference

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top