I have a list called clients, when a new item is added to the list a new sub site should be created. I created an Azure Service Bus and copy pasted the ACS connection string for debugging.
I can debug the App Installed event without any problems, however when a new item is added to the list breakpoints are never hit.
I checked on Sharepoint Client Browser and the remote event receiver is there. http://screencast.com/t/OoPDhqRq
The code to add the RER and to execute the item added is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using Capatech.IntranetWeb.Provisioning;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace xx.IntranetWeb.Services
{
public class AppEventReceiver : IRemoteEventService
{
/// <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();
try
{
switch (properties.EventType)
{
case SPRemoteEventType.AppInstalled:
AppEvents.AppInstalled(properties);
result.Status = SPRemoteEventServiceStatus.Continue;
break;
case SPRemoteEventType.AppUninstalling:
//this.HandleAppUnInstall(properties);
result.Status = SPRemoteEventServiceStatus.Continue;
break;
case SPRemoteEventType.AppUpgraded:
AppEvents.AppUpgraded(properties);
result.Status = SPRemoteEventServiceStatus.Continue;
break;
}
return result;
}
catch (Exception ex)
{
//Log message
result.ErrorMessage = "Capatech.Intranet: " + ex.Message;
result.Status = SPRemoteEventServiceStatus.CancelWithError;
return result;
}
}
/// <summary>
/// To handle list events
/// </summary>
/// <param name="properties"></param>
public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
//Remote event receivers for clients
if (properties.ListEventProperties.ListTitle == "Clientes")
{
switch (properties.EventType)
{
case SPRemoteEventType.ItemAdded:
Clients.HandleItemClientAdded(properties);
break;
case SPRemoteEventType.ItemUpdated:
Clients.HandleItemClientAdded(properties);
break;
}
}
}
}
}
{
const string CLIENTES_ITEMADDED = "CLIENTES_ITEMADDED";
const string CLIENTES_ITEMUPDATED = "CLIENTES_ITEMUPDATED";
/// <summary>
/// Associate RER to client list.
/// </summary>
/// <param name="clientcontext">Client context</param>
public static void AssociateItemAddedEventReceiverToClientList(ClientContext clientcontext)
{
List clientList = clientcontext.Web.Lists.GetByTitle("Clientes");
clientcontext.Load(clientList);
var eventReceivers = clientList.EventReceivers;
clientcontext.Load(eventReceivers);
clientcontext.ExecuteQuery();
var rerExists = false;
foreach (var rer in clientList.EventReceivers)
{
if (rer.ReceiverName == CLIENTES_ITEMADDED)
{
rerExists = true;
System.Diagnostics.Trace.WriteLine("Found existing ItemAdded receiver at " + rer.ReceiverUrl);
}
}
if (!rerExists)
{
EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
receiver.EventType = EventReceiverType.ItemAdded;
//receiver.Synchronization = EventReceiverSynchronization.Synchronous;
//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 = CLIENTES_ITEMADDED;
//Add the new event receiver to a list in the host web
clientList.EventReceivers.Add(receiver);
clientcontext.ExecuteQuery();
}
}
/// <summary>
/// This method is executed when a new client is added to the list
/// </summary>
/// <param name="properties">Remote vent properties</param>
public static void HandleItemClientAdded(SPRemoteEventProperties properties)
{
using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
CreateClientSiteAndUpdateSiteOnList(properties, clientContext);
}
}
}
/// <summary>
/// Method to create a client site
/// </summary>
/// <param name="properties">Remote event properties</param>
/// <param name="clientContext">Client Context</param>
private static void CreateClientSiteAndUpdateSiteOnList(SPRemoteEventProperties properties, ClientContext clientContext)
{
List requestList = clientContext.Web.Lists.GetById(properties.ItemEventProperties.ListId);
ListItem item = requestList.GetItemById(properties.ItemEventProperties.ListItemId);
clientContext.Load(item);
clientContext.ExecuteQuery();
string site_title = item["NIT"].ToString() + " " + item["Nombre Cliente"].ToString();
string site_url = item["NIT"].ToString();
Web newWeb = clientContext.Web.CreateWeb(site_title, site_url, site_title, "STS#0", 1033);
clientContext.Load(newWeb);
clientContext.ExecuteQuery();
//Updates site on the client list
FieldUrlValue siteUrl = new FieldUrlValue();
siteUrl.Url = newWeb.Url;
siteUrl.Description = site_title + " Sitio";
item["Sitio Cliente"] = siteUrl;
item.Update();
clientContext.ExecuteQuery();
}
/// <summary>
/// Configure properties on the client fields
/// </summary>
/// <param name="ctx">Client context</param>
public static void ConfigureClientFields(ClientContext ctx)
{
List clientList = ctx.Web.Lists.GetByTitle("Clientes");
ctx.Load(clientList);
ctx.ExecuteQuery();
clientList.Fields.GetByInternalNameOrTitle("Sitio Cliente").SetShowInNewForm(false);
clientList.Fields.GetByInternalNameOrTitle("Version Sitio").SetShowInEditForm(false);
ctx.ExecuteQuery();
}
/// <summary>
/// This method is executed when a client info is updated
/// </summary>
/// <param name="properties">Remote event properties</param>
public static void HandleItemClientUpdated(SPRemoteEventProperties properties)
{
}
}