
Hi, Guys.
I am building a plugin which is triggered by a creation of contact. Then the plugin will build connection and get Another CRM organization service, to create a new contact as well in that CRM instance. Both CRM instances are 2015 on-premise. And I get error when creating the second CRM proxy.
The error is : System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using System.ServiceModel.Description;
using System.Security;
using Microsoft.Crm.Sdk.Messages;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace SyncronizePlugin
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class Class1:IPlugin
{
string newfirstname, newlastname;
static IOrganizationService service1;
private static bool AcceptAllCertificatePolicy(Object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity entity = (Entity)context.InputParameters["Target"];
string createdentityname = entity.LogicalName;
Guid createdid = entity.Id;
ClientCredentials credentials = new ClientCredentials();
//credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
credentials.UserName.UserName = @"XYX\admin";
credentials.UserName.Password = "Pass12345";
Uri OrganizationUri = new Uri("xyz.xyz.com/.../Organization.svc");
Uri HomeRealmUri = null;
OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null); //thrown error in this line
service1 = (IOrganizationService)serviceProxy;
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
ColumnSet col = new ColumnSet(true);
Entity createdobj = service.Retrieve(createdentityname, createdid, col);
if (createdobj.Contains("firstname"))
{
newfirstname = createdobj.Attributes["firstname"].ToString();
}
if (createdobj.Contains("lastname"))
{
newlastname = createdobj.Attributes["lastname"].ToString();
}
Entity Contact = new Entity();
Contact.LogicalName = "contact";
Contact.Attributes.Add("firstname", newfirstname);
Contact.Attributes.Add("lastname", newlastname);
service1.Create(Contact);
}
}
}
*This post is locked for comments
I have the same question (0)It looks like you've registered the plugin assembly in the sandbox. Plugins in the sandbox are only permitted to make anonymous web service calls. If you need to connect to another Crm organisation, then your plugin assembly will need to be registered outside the sandbox