0
votes

I have written a simple code on VS 2015 to handle ItemAdded EventReceiver but it won't fire as expected.

public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
  try
  {
    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
    {
      if (clientContext != null)
      {
        switch (properties.EventType)
        {
          case (SPRemoteEventType.ItemAdded):
            clientContext.Load(clientContext.Web);
            clientContext.ExecuteQuery();
            List list = clientContext.Web.GetList("https://learn91.sharepoint.com/sites/Demo/Lists/List1");
            clientContext.Load(list);
            clientContext.ExecuteQuery();
            CamlQuery query = new CamlQuery();
            query.ViewXml = @"<OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy>";
            ListItemCollection items = list.GetItems(query);
            clientContext.Load(items);
            clientContext.ExecuteQuery();
            foreach (ListItem item in items)
            {
                item["Description"] = "Updated After Event Trigger";
                clientContext.Load(item);
                clientContext.ExecuteQuery();
            }
            break;
        }
      }
    }
  }
  catch (Exception ex)
  {
      string ErrorMessage = ex.Message;
  }
}

Can anyone help what could be issue?Thanks in advance!!

1
have you registered the event receiver with associated list/library?Vineet Desai
what you mean by "as expected"? what is happening nowvinayak hegde

1 Answers

0
votes

Ok there are few things that you should think over. First of all I would recommend not using hardcoded URLs for your list like

List list = clientContext.Web.GetList("https://learn91.sharepoint.com/sites/Demo/Lists/List1");

instead just load it by name

var list = clientContext.Web.Lists.GetByTitle("List1");

Another thing is your Caml query. If you would like to get your listItems sorted (I don't know why though since you are updaing all items). You are missing <View><Query> nodes. Your caml should look following

var camlQuery = new CamlQuery {
   ViewXml = @"<View><Query><OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query></View>"
};

Since this is update you might have issues with access to NonInitializedProperty so following part

ListItemCollection items = list.GetItems(query);
clientContext.Load(items);

should also include the Description property as on the code snippet below

ListItemCollection items = list.GetItems(query);
clientContext.Load(items, listItems => listItems.Include(item => item["Description"]));

Another thing is that you are iterating over the items but you are not updating them. Also CSOM handles batch processing so you don't have to call ExecuteQuery everytime you change item. So your loop

foreach (ListItem item in items)
{
   item["Description"] = "Updated After Event Trigger";
   clientContext.Load(item);
   clientContext.ExecuteQuery();
}

should look somewhat like that:

foreach (var item in items)
{
    item["Description"] = "Updated After Event Trigger";
    item.Update();
}
clientContext.ExecuteQuery();

Hope that helps. Please be also advised that above code samples will work for list that has less that 5000 items. If you have breached that limit read about ListItemCollectionPosition on MSDN