2

I have the following:-

  1. Sharepoint Online.
  2. Visual Studio 2015.
  3. Azure free trial account.

now i want to do the following:-

  1. Develop a remote event receiver which will get fired when items are added inside custom lists that are found under a site collection.

so i did these steps:-

  1. i create a new sharepoint addin project:-

enter image description here

  1. i specify its type as Provider-Hosted, and i add the url for our Developer site collection:-

enter image description here

  1. after that i chose the following:-
    • version = SP online
    • type = ASP.NET MVC
    • Authntication = Windows Azure.
  2. after that i set the Handle App Installed and HandleAppUninstalling properties to True.
  3. this will create a new service file, where i added this code, mainly to update the list items' titles after that items are added:-
private const string ReceiverName = "ItemAddedEvent";
private const string ListName = "tg";

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

              }

           }



        }



 }
  1. i went to the AppMenifest.xml, and i define to attache the RER to Lists of type custom lists, as follow:- enter image description here
  2. final step is that under "SharePoint" tab inside the project properties, i define the Azure end point (i have created a free Azure account):- enter image description here
  3. i Build >> Run the project, where i get this message to attach the RER to a custom list named "tg" which i have already created:- enter image description here
  4. now i trust the addin >> i add a new item inside the "tg" list, and its title got updated.. GREAT things is working at this point.

But i face these 2 issues:-

First Issue

  1. Now in my case i want to attach the RER to all the custom lists and not to a specif custom list. so inside the AppMenifest.xml i change the setting from List to Site collection as follow:-

enter image description here

  1. now i build and run the project , where i got this message :-

enter image description here

  1. now I trust the addin, but when i add a new item inside the "tg" custom list its title did not chnage!!.

so my first question is how i can attach the RER to all the custom lists inside my site collection ?

Second issue

When i remove the end point settings inside the debugging section,and i build and run the application, i got this error when Visual Studio try to upload/install the addin:-

Add-in failed to install, cleaning up...
Successfully uninstalled the SharePoint Add-in.
Add-in installation encountered the following errors:
12/01/2018 17:21:36

@"Error 1
        CorrelationId: f7908248-48bb-49e7-ba25-08aeef895123
        ErrorDetail: The remote event receiver callout failed.
        ErrorType: Transient
        ErrorTypeName: Intermittent
        ExceptionMessage: There was no endpoint listening at https://localhost:44368/Services/AppEventReceiver.svc that could

accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Source: Common SourceName: Common App Deployment

@"Error 2
        CorrelationId: f7908248-48bb-49e7-ba25-08aeef895123
        ErrorDetail: The remote event receiver callout failed.
        ErrorType: Transient
        ErrorTypeName: Intermittent
        ExceptionMessage: There was no endpoint listening at > https://localhost:44368/Services/AppEventReceiver.svc that could

accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Source: Common SourceName: Common App Deployment

so does this mean that i can not test any provider hosted app unless i have an Azure service bus end point? Can anyone advice on the above 2 issues i am facing?

Thanks.

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.