Impersonating plugin as SYSTEM programatically

Frequently we have to execute plugins as Service Accounts so they have the right privileges to execute the business logic we want. Although you can set a user in the “Run In User Context” field of the plugin step, sometimes our organizations doesn’t actually have service accounts to be used there.

To programatically run a plugin as SYSTEM, you have to change the user sent as parameter when creating your IOrganizationService object. Instead of sending: Context.InitiatingUserID, you should set null.

IOrganizationService Service = Factory.CreateOrganizationService(null); // To execute plugin as SYSTEM

IOrganizationService Service = Factory.CreateOrganizationService(Context.InitiatingUserId); // To execute plugin as the user set in "Run In User Context"

Hope it helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s