{Dynamics CRM Plugins Impersonation} Identify which for your plugins are running under impersonation

Recently we upgraded one customer from their CRM 2011 to CRM 2013 environment. During the process of upgrade and new features implementation, we were developing in separate environment while the existing customer system was still going on.

Just before going live with the upgrade, our client informed us that there had been considerable number of changes in plugin configuration specially in relation to the user’s context under which the plugin was running and their internal team lost the track of all those changes. To tackle this first we did a thorough merging of the plugin code and then deployed the solution in CRM 2013 Production environment. Then we ran the below SQL in the existing production environment and the new production environment.

Select DISTINCT pa.Name [Assembly],
pt.Name [Event Handler],
mpt.Name [Step],
CASE mpt.Stage
WHEN 10 THEN ‘PRE-VALIDATE’
WHEN 20 THEN ‘PRE-OPERATION’
WHEN 40 THEN ‘POST-OPERATION’
END AS Stage,
sm.Name [Message],
ev.LogicalName [Entity],
su.FullName [Impersonating User]
FROM SDKMessageProcessingStep mpt INNER JOIN SDKMessage sm ON mpt.SdkMessageID = sm.SDKMessageId
INNER JOIN SDKMessageFilter mf ON mf.SDKMessageFilterId = mpt.SDKMessageFilterId
INNER JOIN EntityView ev ON ev.ObjectTypeCode = mf.PrimaryObjectTypeCode
INNER JOIN PluginTypeBase pt ON mpt.PluginTypeId = pt.PluginTypeId
INNER JOIN PluginAssemblyBase pa ON pt.PluginAssemblyId = pa.PluginAssemblyId
INNER JOIN Systemuser su ON su.SystemUserId = mpt.ImpersonatingUserId
WHERE mpt.ImpersonatingUserId is NOT NULL
AND mpt.Stage <> 30 AND mpt.IsCustomizable = 1

After running the above code, it became very easy for us to go to the appropriate steps and change it by comparing the results from both the environments.

Hope this helps!