Question

I'm trying to create a plugin which creates a task in response to the create message for a custom entity.

I've used CrmSvcUtil.exe to generate a custom OrganisationServiceContext, I have a console application test host which successfully uses this to create a task (although using the SDK serverConnect.GetServerConfiguration() to create the OrganizationServiceProxy).

When i deploy the plugin assembly (sandboxed) to the online instance, the code below blows up with:

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed

// Obtain the execution context from the service provider.
var executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(executionContext.UserId);

//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

tracingService.Trace("Buiding");

var organizationUri = new Uri("{theuri}/XRMServices/2011/Organization.svc");          
var credentials = new ClientCredentials();
credentials.Windows.ClientCredential = NetworkCredential)CredentialCache.DefaultCredentials;

var organizationServiceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
organizationServiceProxy.EnableProxyTypes();

var context = new CustomContext(organizationServiceProxy);

Can somebody point me in the right direction?

Thanks

Was it helpful?

Solution

Your CustomContext should accept a parameter of type Microsoft.Xrm.Sdk.IOrganizationService. Like this (excerpt of a crmsvcutil generated file)

/// <summary>
/// Constructor.
/// </summary>
public CrmContext(Microsoft.Xrm.Sdk.IOrganizationService service) : base(service)
{
}

You could (and should) simply generate the connection with help of the plugin context

var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);

using (var crmContext = new CrmContext(service))
{
 ...
}

Depending on the value of {theuri} you are using, I assume that your request is blocked by the sandbox as it violates its constraints.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top