locked
Error on accessing CRM webservice from plug-in RRS feed

  • Question

  • Hello everyone,

    I have a requirement where I need to create some records on an IFD on-premise deployment based on some data in an online deployment. This is where I am currently stuck:

    - I created a plugin and registered on the online org for create of phonecall

    - I used ILMerge to add Xrm.Client to the assembly

    I am getting the following error: 

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

    My code is the following:

    public class Plugin : IPlugin
        {
            IOrganizationService service = null;
    
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                        serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
    
                IOrganizationServiceFactory _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                service = _serviceFactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = context.InputParameters["Target"] as Entity;
    
                    if (entity.LogicalName == "phonecall" && context.MessageName == "Create")
                    {
                        try
                        {
                            var connectionstring = CrmConnection.Parse("Url=https://crmorg.fabrikam.com; Username=username; Password=pwd;");
                            OrganizationService remoteService = new OrganizationService(connectionstring);
    
    
                            Entity phoneCallEntity = new Entity("phonecall");
                            phoneCallEntity["phonenumber"] = entity.Contains("phonenumber") ? entity["phonenumber"] : null;
                            phoneCallEntity["directioncode"] = entity.Contains("directioncode") ? entity["directioncode"] : null;
                            phoneCallEntity["scheduledend"] = entity.Contains("scheduledend") ? entity["scheduledend"] : null;
                            phoneCallEntity["subject"] = entity.Contains("subject") ? entity["subject"] : null;
                            phoneCallEntity["actualdurationminutes"] = entity.Contains("actualdurationminutes") ? entity["actualdurationminutes"] : null;
                            phoneCallEntity["description"] = entity.Contains("description") ? entity["description"] : null;
    
                            Entity sysuser = service.Retrieve("systemuser", ((EntityReference)entity["ownerid"]).Id, new ColumnSet("fullname"));
    
                            QueryExpression query = new QueryExpression
                            {
                                EntityName = "systemuser",
                                ColumnSet = new ColumnSet("systemuserid"),
                                Criteria = new FilterExpression()
                            };
                            query.Criteria.AddCondition("fullname", ConditionOperator.Equal, sysuser["fullname"].ToString());
                            EntityCollection sysusercol = remoteService.RetrieveMultiple(query);
                            if (sysusercol.Entities.Count > 0)
                            {
                                phoneCallEntity["ownerid"] = new EntityReference("systemuser", sysusercol.Entities[0].Id);
    
                                Entity activityparty = new Entity("activityparty");
                                activityparty["partyid"] = new EntityReference("systemuser", sysusercol.Entities[0].Id);
                                activityparty["participationtypemask"] = new OptionSetValue(1);
                                EntityCollection entcol = new EntityCollection();
                                entcol.Entities.Add(activityparty);
                                if ((bool)entity["directioncode"]) phoneCallEntity["to"] = entcol;
                                else phoneCallEntity["from"] = entcol;
                            }
                            remoteService.Create(phoneCallEntity);
                        }
                        catch (Exception ex)
                        {
                            throw new NotImplementedException(ex.Message);
                        }
                    }
                }
            }
    
        }

    Do you have any ideas what might be causing this?

    Regards,

    Adam

    Tuesday, July 8, 2014 12:01 PM

All replies

  • The error indicates the code is registered in the sandbox, and is trying to do something that is not permitted in the sandbox.

    A plugin that runs in Crm Online will not be able to connect to a separate organisation. A plugin in Crm Online can only make anonymous web service calls, and any connection to a Crm organisation needs to be authenticated.

    So, I don't think your current deisgn is achievable. You should be able to make a connection the other way around - i.e. from a plugin in the OnPremise organisation, you should be able to connect to a Crm Online instance, as the pluign can be registered outside of the sandbox


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, July 8, 2014 5:25 PM
    Moderator