I'm trying to update the list item using RER (Provider Hosted App + SharePoint Online) and the problem is that the ItemUpdated event is is triggered multiple times.
Any suggestions?
AppEventReceiver.cs
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
switch (properties.EventType)
{
case SPRemoteEventType.AppInstalled:
HandleAppInstalled(properties);
break;
case SPRemoteEventType.AppUninstalling:
HandleAppUninstalling(properties);
break;
case SPRemoteEventType.ItemAdded:
HandleItemAdded(properties);
break;
case SPRemoteEventType.ItemUpdated:
HandleItemUpdated(properties);
break;
}
return result;
}
private void HandleItemUpdated(SPRemoteEventProperties properties)
{
using (ClientContext clientContext =
TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
new RemoteEventReceiverManager().ItemUpdatedToListEventHandler(clientContext, properties.ItemEventProperties.ListId, properties.ItemEventProperties.ListItemId);
}
}
}
RemoteEventReceiverManager.cs
public void ItemUpdatedToListEventHandler(ClientContext clientContext, Guid listId, int listItemId){
try
{
List photos = clientContext.Web.Lists.GetById(listId);
ListItem item = photos.GetItemById(listItemId);
clientContext.Load(item);
clientContext.ExecuteQuery();
DateTime lastModifiedTime = Convert.ToDateTime(item["Modified"].ToString());
if (lastModifiedTime < DateTime.Now.AddMilliseconds(-2))
{
item["Description"] += "\nUpdated by RER " +
System.DateTime.Now.ToLongTimeString();
item.Update();
clientContext.ExecuteQuery();
}
}
catch (Exception oops)
{
System.Diagnostics.Trace.WriteLine(oops.Message);
}
}