0

I want to test and debug a remote event receiver for my custom list inside sharepoint online. So i did these steps:-

  1. Inside our sharepoint online i created a new developer site collection.

  2. using Visual Studio pro 2015 >> i create a new Visual Studio project of type SP Add-in.

  3. The type = provider-hosted, as follow:- enter image description here

  4. after that i changed the Handle Add-in installed/uninstalled to True:- enter image description here

  5. then i open the Ngrok command line, i enter the following:-

ngrok http --host-header=rewrite 50452// where this port is our RER port inside visual studio

where 50452 is the port for the VS project:- enter image description here

i got the following:- enter image description here

so i updated the AappManifest.xml with the forwarding url:- enter image description here

  1. I update the app permission as follow:- enter image description here

  2. then i run the project >> i got this popup >> i select the custom list named "projects":- enter image description here

  3. where i got to the break-point inside my code >> but the client content inside my code will be empty:- enter image description here

  4. after that the VS stop running and i got this error:- enter image description here

Here is the full code inside my "AppEventReceiver.svc"

public class AppEventReceiver : IRemoteEventService
    {
        private const string ReceiverName = "ItemAddedEvent";
        private const string ListName = "projects";
        /// <summary>
        /// Handles app events that occur after the app is installed or upgraded, or when app is being uninstalled.
        /// </summary>
        /// <param name="properties">Holds information about the app event.</param>
        /// <returns>Holds information returned from the app event.</returns>
        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;
            }


            return result;
        }
        private void HandleAppUninstalling(SPRemoteEventProperties properties)

        {

            using (ClientContext clientContext =

              TokenHelper.CreateAppEventClientContext(properties, false))

            {

                if (clientContext != null)

                {

                    List myList = clientContext.Web.Lists.GetByTitle(ListName);

                    clientContext.Load(myList, p => p.EventReceivers);

                    clientContext.ExecuteQuery();



                    var rer = myList.EventReceivers.Where(

                        e => e.ReceiverName == ReceiverName).FirstOrDefault();



                    try

                    {

                        System.Diagnostics.Trace.WriteLine("Removing ItemAdded receiver at "

                               + rer.ReceiverUrl);



                        //This will fail when deploying via F5, but works

                        //when deployed to production

                        rer.DeleteObject();

                        clientContext.ExecuteQuery();



                    }

                    catch (Exception oops)

                    {

                        System.Diagnostics.Trace.WriteLine(oops.Message);

                    }



                }

            }

        }
        private void HandleAppInstalled(SPRemoteEventProperties properties)

        {

            using (ClientContext clientContext =

                 TokenHelper.CreateAppEventClientContext(properties, false))

            {

                if (clientContext != null)

                {

                    List myList = clientContext.Web.Lists.GetByTitle(ListName);

                    clientContext.Load(myList, p => p.EventReceivers);

                    clientContext.ExecuteQuery();



                    bool rerExists = false;



                    foreach (var rer in myList.EventReceivers)

                    {

                        if (rer.ReceiverName == ReceiverName)
                        {

                            rerExists = true;

                            System.Diagnostics.Trace.WriteLine("Found existing ItemAdded receiver at "

                                + rer.ReceiverUrl);

                        }

                    }



                    if (!rerExists)

                    {

                        EventReceiverDefinitionCreationInformation receiver =

                             new EventReceiverDefinitionCreationInformation();

                        receiver.EventType = EventReceiverType.ItemAdded;


                        //Get WCF URL where this message was handled

                        OperationContext op = OperationContext.Current;
                        Message msg = op.RequestContext.RequestMessage;



                        receiver.ReceiverUrl = msg.Headers.To.ToString();



                        receiver.ReceiverName = ReceiverName;

                        receiver.Synchronization = EventReceiverSynchronization.Synchronous;
                        myList.EventReceivers.Add(receiver);



                        clientContext.ExecuteQuery();



                        System.Diagnostics.Trace.WriteLine("Added ItemAdded receiver at "

                                + msg.Headers.To.ToString());

                    }

                }

            }

        }
        private void HandleItemAdded(SPRemoteEventProperties properties)

        {

            using (ClientContext clientContext =

                 TokenHelper.CreateRemoteEventReceiverClientContext(properties))

            {

                if (clientContext != null)

                {

                    try

                    {

                        List photos = clientContext.Web.Lists.GetById(

                              properties.ItemEventProperties.ListId);

                        ListItem item = photos.GetItemById(

                             properties.ItemEventProperties.ListItemId);

                        clientContext.Load(item);

                        clientContext.ExecuteQuery();



                        item["Title"] += "\nUpdated by RER " +

                               System.DateTime.Now.ToLongTimeString();

                        item.Update();

                        clientContext.ExecuteQuery();

                    }

                    catch (Exception oops)

                    {

                        System.Diagnostics.Trace.WriteLine(oops.Message);

                    }

                }



            }



        }

        /// <summary>
        /// This method is a required placeholder, but is not used by app events.
        /// </summary>
        /// <param name="properties">Unused.</param>
        public void ProcessOneWayEvent(SPRemoteEventProperties properties)
        {
            throw new NotImplementedException();
        }

    }

So can anyone advice why i am getting Null for the ClientContext ? and why the project will stop running?

EDIT-1 I added a break-point inside the TokenHelper.cs, and the properties inside the return CreateAcsClientContextForUrl(properties, sharepointUrl); will have the following error:-

The endpoint address 'http://6ad2ebe4.ngrok.io/Services/AppEventReceiver.svc' does not match the app's endpoint 'localhost:44327'.

EDIT-2 here is my appmanifest.xml

<?xml version="1.0" encoding="utf-8" ?>
<!--Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9-->
<App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest"
     Name="SharePointAddIn9"
     ProductID="{7c5e7373-350d-4584-a2ef-a91faa4a1c42}"
     Version="1.0.0.0"
     SharePointMinVersion="16.0.0.0"
>
  <Properties>
    <Title>SharePointAddIn9</Title>
    <StartPage>~remoteAppUrl/?{StandardTokens}</StartPage>
    <InstalledEventEndpoint>http://6ad2ebe4.ngrok.io/Services/AppEventReceiver.svc</InstalledEventEndpoint>
    <UninstallingEventEndpoint>http://6ad2ebe4.ngrok.io/Services/AppEventReceiver.svc</UninstallingEventEndpoint>
  </Properties>

  <AppPrincipal>
    <RemoteWebApplication ClientId="*" />
  </AppPrincipal>
  <AppPermissionRequests AllowAppOnlyPolicy="true">
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="FullControl" >
    <Property Name="BaseTemplateId" Value="100"/>
    </AppPermissionRequest>
  </AppPermissionRequests>
</App>

enter image description here

  • 1
    put a break point inside the TokenHelper CreateAppEventClientContext and walk through the logic. Your setup is correct, your authentication just isn't working. You'll need to debug to figure out why. – Derek Gusoff Aug 26 at 14:41
  • @DerekGusoff thanks for the reply and help. i add a breakpoint inside the CreateAppEventClientContext and the properties will have the following error The endpoint address 'http://6ad2ebe4.ngrok.io/Services/AppEventReceiver.svc' does not match the app's endpoint 'localhost:44327'. i edited my question with more detailes. thanks – SharePoint TestDev Aug 26 at 17:27
  • 1
    Can you show your AppManifest.xml file? – dhruvil29 Aug 27 at 8:41
  • @dhruvil29 yes sure, i edited my question showing the AppManifest.xml.let me know your feedback. thanks – SharePoint TestDev Aug 27 at 11:20
  • 1
    Add the permission for site collection and web then try . <AppPermissionRequests> <AppPermissionRequest Scope="sharepoint/content/sitecollection" Right="FullControl" /> <AppPermissionRequest Scope="sharepoint/content/sitecollection/web" Right="FullControl" /> </AppPermissionRequests> – dhruvil29 Aug 27 at 11:26

Your Answer

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

Browse other questions tagged or ask your own question.