I want to fire the event receiver when new file is uploaded to the document library in site in SharePoint online.
Is ItemAdded
event fire when file is uploaded to the documents?
Yes.
ItemAdded
event fires when a new file is uploaded to the document library in SharePoint.
Reference:
Official Documentation:
Sample code to add remote event receiver for host web library.
private void AppInstalledMethod(SPRemoteEventProperties _properties)
{
using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(_properties, false))
{
if (clientContext != null)
{
Web web = clientContext.Web;
var list = web.Lists.GetByTitle("MyDoc3");
clientContext.Load(web);
clientContext.Load(list);
clientContext.Load(list.EventReceivers);
clientContext.ExecuteQuery();
EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
receiver.EventType = EventReceiverType.ItemAdding;
OperationContext op = OperationContext.Current;
Message msg = op.RequestContext.RequestMessage;
receiver.ReceiverUrl = msg.Headers.To.ToString();
receiver.ReceiverName = "ItemAddingEvent";
receiver.Synchronization = EventReceiverSynchronization.Synchronous;
receiver.SequenceNumber = 5000;
list.EventReceivers.Add(receiver);
EventReceiverDefinitionCreationInformation receiverUpdate = new EventReceiverDefinitionCreationInformation();
receiverUpdate.EventType = EventReceiverType.ItemUpdating;
//op = OperationContext.Current;
//msg = op.RequestContext.RequestMessage;
receiverUpdate.ReceiverUrl = msg.Headers.To.ToString();
receiverUpdate.ReceiverName = "ItemUpdatingEvent";
receiverUpdate.Synchronization = EventReceiverSynchronization.Synchronous;
receiverUpdate.SequenceNumber = 5000;
list.EventReceivers.Add(receiverUpdate);
clientContext.ExecuteQuery();
}
}
}
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
switch (properties.EventType)
{
case SPRemoteEventType.AppInstalled:
AppInstalledMethod(properties);
break;
case SPRemoteEventType.AppUpgraded:
AppUpgradedMethod(properties);
break;
case SPRemoteEventType.ItemAdding:
ItemAddingMethod(properties);
break;
case SPRemoteEventType.ItemUpdating:
ItemUpdatingMethod(properties);
break;
case SPRemoteEventType.WebAdding:
// you can implement webaddding event
break;
case SPRemoteEventType.WebDeleting:
//you can implemet web deleting event if needed.
default:
break;
}
return result;
}
private void ItemUpdatingMethod(SPRemoteEventProperties _properties)
{
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(_properties))
{
if (clientContext != null)
{
var afterproperties = _properties.ItemEventProperties.AfterProperties;
}
}
}