3

I've been woking with CRM since seven months ago. I still don't know what is, for what is used and why those variables.

public void Execute(IServiceProvider serviceProvider)
{

     Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
     serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

     IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory));

     IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
}

I've searched for answers without any luck.

Thanks

2 Answers 2

1

MSDN provides a nice set of articles starting from Understand the Data Context.

In short, here it goes:

  1. IPluginExecutionContext is where all the contextual information is brought to your code. InputParameters and OutputParameters member contain for example the record the plugin is being fired upon, the pre- and post-image, the handled message (The actual data depends on how you registered the plugin: pre VS post, the exact message, online or offline...)
  2. IOrganizationService object allows you to query the Organization Service. Most common usage scenario is CRUD operations (Create, Retrieve, Update, Delete : beware of deadlocks).
1
  • serviceFactory.CreateOrganizationService(context.UserId) : what if one user belongs to more than one organization?
    – Sean
    Aug 9, 2016 at 7:28
1

IPluginExectionContext:

  1. Defines the contextual information passed to a plug-in at run-time. Contains information that describes the run-time environment that the plug-in is executing in, information related to the execution pipeline, and entity business information.
  2. The execution context is passed to a plug-in at run time in the System.IServiceProvider parameter of the Execute method. You can obtain the context from the service provider as shown in the following plug-in code. // Obtain the execution context from the service provider.

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof( IPluginExecutionContext));

IOrganizationService Interface: Provides programmatic access to the metadata and data for an organization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.