Understanding Impersonation in Plugins and knowing when to use it
Plugins are usually set to run as the calling user but sometimes you need to run a plugin with System User privileges. Before I explain how, we should first look at the why and how you impersonate users inside a plugin What is impersonation When you create a plugin in CRM you write an execute method which gets passed in IServiceProvider serviceProvider This has lots of variables and objects such as IPluginExecutionContext ITracingService IOrganizationServiceFactory You use the IOrganisationServiceFactory to create a IOrganizationService. The IOrganizationService is the CRM SDK, it provides you access to CRM programmatically . CRM developers use the IOrganizationService (which I will often call CRMService) to CRUD operation – Create, Retrieve, Update, Delete records in CRM Assign records Change status pretty much everything you can do in CRM Here is the code to create the IOrganizationService, taken from the Plugin class of the C...