3

Event is attached to document library, but it is not firing when I upload document. Below is the Element.xml file.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="## Heading ##http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>DocumentEventReceiverItemAdded</Name>
      <Type>ItemAdded</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Url>~remoteAppUrl/Services/DocumentEventReceiver.svc</Url>
    </Receiver>
    <Receiver>
      <Name>DocumentEventReceiverItemAdding</Name>
      <Type>ItemAdding</Type>
      <Url>~remoteAppUrl/Services/DocumentEventReceiver.svc</Url>
      <SequenceNumber>10000</SequenceNumber>
    </Receiver>
  </Receivers>
</Elements>

ItemAdded Code:

if (properties.EventType == SPRemoteEventType.ItemAdded)
{
    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
    {
        if (clientContext != null)
        {
            try
            {
                List lstDemoeventReceiver = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);
                ListItem itemDemoventReceiver = lstDemoeventReceiver.GetItemById(properties.ItemEventProperties.ListItemId);
                itemDemoventReceiver["testname"] = " Event fired.";
                itemDemoventReceiver.Update();
                clientContext.ExecuteQuery();
            }
            catch (Exception ex) { }
        }
    }
}
0

The remote event receiver is attached to app/add-in web lists/libraries as per my knowledge, if you want to attache remote ER to host web lists/libraries.you need register it manually.

Sample Code:

private void AppInstalledMethod(SPRemoteEventProperties _properties)
        {
            using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(_properties, false))
            {
                if (clientContext != null)
                {
                    Web web = clientContext.Web;
                    var list = web.Lists.GetByTitle("Doc4");
                    clientContext.Load(web);
                    clientContext.Load(list);
                    clientContext.Load(list.EventReceivers);
                    clientContext.ExecuteQuery();

                    EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
                    receiver.EventType = EventReceiverType.ItemAdded;
                    OperationContext op = OperationContext.Current;
                    Message msg = op.RequestContext.RequestMessage;
                    receiver.ReceiverUrl = msg.Headers.To.ToString();
                    receiver.ReceiverName = "ItemAdded";
                    receiver.Synchronization = EventReceiverSynchronization.Synchronous;
                    receiver.SequenceNumber = 5000;
                    //adding receiver the host web.
                    // we are adding event receiver to site context so that event receriver fires for sub-sub-sites
                    list.EventReceivers.Add(receiver);
                    clientContext.ExecuteQuery();                   
                }
            }
        }
| improve this answer | |

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.