I have the following:-
- Sharepoint Online.
- Visual Studio 2015.
- Azure free trial account.
now i want to do the following:-
- 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:-
- i create a new sharepoint addin project:-
- i specify its type as Provider-Hosted, and i add the url for our Developer site collection:-
- after that i chose the following:-
- version = SP online
- type = ASP.NET MVC
- Authntication = Windows Azure.
- after that i set the Handle App Installed and HandleAppUninstalling properties to True.
- 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); } } } }
- i went to the
AppMenifest.xml
, and i define to attache the RER to Lists of type custom lists, as follow:- - final step is that under "SharePoint" tab inside the project properties, i define the Azure end point (i have created a free Azure account):-
- 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:-
- 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
- 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:-
- now i build and run the project , where i got this message :-
- 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.