0

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?

1

Yes.

ItemAdded event fires when a new file is uploaded to the document library in SharePoint.

Reference:

  1. How to Implement Remote Event Receiver in SharePoint Online.

Official Documentation:

  1. Use remote event receivers in SharePoint.
  • can you provide any code or article to how to implement this. – Manoj Sanjeewa Aug 13 at 11:35
1

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;
                }
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.