locked
Creating a IOrganizationService instance programatically for crm online 2015 : exception RRS feed

  • Question

  • Hi,

    I have used below code to create service instance.

    IOrganizationService _service;
                ClientCredentials credentials = new ClientCredentials();
                credentials.UserName.UserName = "xxx";
                credentials.UserName.Password = "xxxx";
                Uri serviceUri = new Uri("https://xxx.api.crm.dynamics.com/XRMServices/2011/Organization.svc");   

                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                proxy.EnableProxyTypes();
                _service = (IOrganizationService)proxy;

    I am getting below shown error

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

    Please help me if any one having idea on this.

    Monday, May 4, 2015 11:34 AM

All replies

  • Hi Samuel ,

    You have to pass windows crendentials. Can you try the below code?

    ClientCredentials userCredentials = new ClientCredentials();
    userCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, Domain);
    Uri serviceUri = new Uri("https://xxx.api.crm.dynamics.com/XRMServices/2011/Organization.svc");  
    OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, userCredentials, null);
    proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    service = (IOrganizationService)proxy;


    If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog

    Monday, May 4, 2015 11:47 AM
  • For online version where can we find the domain?
    Monday, May 4, 2015 11:52 AM
  • Hi,

    Sorry , I didn't see that you are using online version of crm. You can find how to create crm service in online version in sdk

    SampleCode\CS\GeneralProgramming\LateBound\CRUDOperationsDE.cs 

    And Can you check your class that all these namespaces has been added.

    1) using Microsoft.Xrm.Sdk;
    2) using Microsoft.Xrm.Sdk.Client;
    3) using System.ServiceModel.Description;
    4) using System.Web.Services.Protocols;


    If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog



    Monday, May 4, 2015 12:06 PM
  • Do you have Windows Identity Foundation feature enable?
    Monday, May 4, 2015 5:44 PM
  • Use the simplified connection:

    https://msdn.microsoft.com/en-us/library/gg695810.aspx

    CrmConnection crmConnection = CrmConnection.Parse("Url=https://XXX.crm.dynamics.com; Username=user@domain.onmicrosoft.com; Password=passwordhere;");
    OrganizationService service = new OrganizationService(crmConnection);
    
    Entity account = new Entity("account");
    account ["name"] = "Test Account";
    
    Guid accountId = service.Create(account); 


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, May 5, 2015 3:52 AM
  • The error you get ('Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed') relates .Net to code access security, when your code is not running in a full trust environment.

    The most likely cause is if you code is running in the Crm sandbox, which is not a full-trust environment, and only permits anonymous calls. Where is your code running ?


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

    Tuesday, May 5, 2015 8:52 AM
    Moderator
  • it is running in sandbox mode.

    I have a plugin business logic , when there is an error in catch block I am trying to instantiate a new service to log the exception to crm entity. During this time I am getting the above error?

    How to rectify this issue?

    Wednesday, May 6, 2015 5:18 AM
  • If you are connecting to the same Crm organisation, you should get the IOrganizationService instance from the plugin execution context - see the first 2 blocks of code in https://msdn.microsoft.com/en-us/library/gg309673.aspx

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

    Wednesday, May 6, 2015 10:41 AM
    Moderator
  • if we get any exception during the plugin execution, is system allow us to use the same context service to run other service methods? I think once exception received we cannot use the same context service

    Thursday, May 7, 2015 8:56 AM
  • If you catch an exception in your code, then you can keep using the context service. If you're throwing an exception to cancel the original operation, then any changes you make via the same context service in steps within the transaction (stage = 20 and 40), will also get rolled back

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

    Thursday, May 7, 2015 11:37 AM
    Moderator
  • Thanks for all your replys.... I will test and update the same.
    Monday, May 25, 2015 5:29 AM