mercredi 21 janvier 2015

The remote event receiver callout didn't return a successful result



I created an app that when installed adds a RER to a list by code. I have copied pasted the ACS connection string from azure service bus.


The app starts deploying but after one minute or two I got this error:



@"Error 1
CorrelationId: 28281df5-1492-4321-a6d3-08f6787ad0b8
ErrorDetail: The remote event receiver callout didn't return a successful result.
ErrorType: App
ErrorTypeName: App Related
ExceptionMessage:
Source: Common
SourceName: Common App Deployment


The code is as follows:



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 xxx.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;
}
}
}




}
}




using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace xxx.IntranetWeb.Provisioning
{
public static class AppEvents
{
const string CLIENTES_ITEMADDED = "CLIENTES_ITEMADDED";


/// <summary>
/// Metodo que se ejecuta al instalarse por primera vez
/// </summary>
/// <param name="properties">Propiedades del evento remoto</param>
public static void AppInstalled(SPRemoteEventProperties properties)
{
Uri hostWebURL = properties.AppEventProperties.HostWebFullUrl;
Uri appWebURL = properties.AppEventProperties.AppWebFullUrl;

using (ClientContext cc = TokenHelper.CreateAppEventClientContext(properties, false))
{

//Create a property bag to know which version the app is installed on the site
cc.Web.SetPropertyBagValue("Capatech.Intranet.Version", 1);

//Associate by code the Remote Event Receivers
Clients.AssociateItemAddedEventReceiverToClientList(cc);
Proyectos.AssociateItemAddedEventReceiverToProjectList(cc);

//Hide on the new forms the client site and the version
Clients.ConfigureClientFields(cc);

//Creacion de librerias de documentos


// CSOM code that accesses the host web
}
}

public static void AppUpgraded(SPRemoteEventProperties properties)
{
Uri hostWebURL = properties.AppEventProperties.HostWebFullUrl;
Uri appWebURL = properties.AppEventProperties.AppWebFullUrl;

using (ClientContext cc = TokenHelper.CreateAppEventClientContext(properties, false))
{
Version ver2OOO = new Version("2.0.0.0");
if (properties.AppEventProperties.PreviousVersion < ver2OOO)
{
// Code to update from 1.0.0.0 to 2.0.0.0 (previous update code) is here.
}
}
}

private static void CleanUp(ClientContext ctx)
{
List clientsList = ctx.Web.Lists.GetByTitle("Clientes");
ctx.Load(clientsList, p => p.EventReceivers);
ctx.ExecuteQuery();

var rer = clientsList.EventReceivers.Where(
e => e.ReceiverName == CLIENTES_ITEMADDED).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();
ctx.ExecuteQuery();
}
catch (Exception _ex)
{
System.Diagnostics.Trace.WriteLine(_ex.Message);
}

}
}
}



using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;

namespace xxx.IntranetWeb.Provisioning
{
/// <summary>
/// Methods to provision Client subsites and their info, lists and content types.
/// </summary>
public static class Clients
{
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)
{

}
}
}







0 commentaires:

Enregistrer un commentaire