Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Error on accessing CRM webservice from plug-in

$
0
0

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


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>