locked
CRM 2015: How do I get OrganizationService in a plugin registered in sandbox RRS feed

  • Question

  • I'm new to CRM Online 2015. I wrote a plugin registered in isolationmode = sandbox, update of account. It should save a  typed-in telephonenumber into a custom field in a normalized form. Before this is done, the plugin should read some configurations which are saved in the root businessunit. So i need to receive the entity businessunit with a retrieve Request. Unfortunately when i debug the code organizationService is null. Is it possible to get the IOrganizationService or it is also one of the restrictions of sandbox mode?

    public void Execute(IServiceProvider serviceProvider){ 
    var organizationService = ExtractService<IOrganizationService>(serviceProvider);
    ... 
    var businessunit = organizationService.Retrieve(...)
    }

    private static T ExtractService<T>(IServiceProvider serviceProvider){  
    var typeOfService = typeof (T);   
    var service = (T) serviceProvider.GetService(typeOfService);   
    return service; 
    }


    Tuesday, July 28, 2015 1:10 PM

Answers

  • Hello,

    Try to use code similar to following:

    public void Execute(IServiceProvider serviceProvider){ 
    	var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    	var organizationService = serviceFactory.CreateOrganizationService(context.UserId);
    	... 
    	var businessunit = organizationService.Retrieve(...)
    }
    


    Dynamics CRM MVP
    My blog

    • Proposed as answer by Guido PreiteMVP Tuesday, July 28, 2015 2:29 PM
    • Marked as answer by Schwascho Wednesday, July 29, 2015 6:55 AM
    Tuesday, July 28, 2015 2:03 PM
    Moderator

All replies

  • Hello,

    Try to use code similar to following:

    public void Execute(IServiceProvider serviceProvider){ 
    	var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    	var organizationService = serviceFactory.CreateOrganizationService(context.UserId);
    	... 
    	var businessunit = organizationService.Retrieve(...)
    }
    


    Dynamics CRM MVP
    My blog

    • Proposed as answer by Guido PreiteMVP Tuesday, July 28, 2015 2:29 PM
    • Marked as answer by Schwascho Wednesday, July 29, 2015 6:55 AM
    Tuesday, July 28, 2015 2:03 PM
    Moderator
  • Thanks a lot Andrii. Solved my Problem...
    Wednesday, July 29, 2015 6:56 AM