Today during debug of one plugin in QuickWatch window I have seen that CallerOrigin property is still in context but it is not property of IPluginExecutionContext:
Following class is helper which allows you easily get origin of plugin invocation:
namespace CallerOrigin.Plugins { internal enum CallerOrigin : int { Undefind = 0, Application = 1, WebService = 2, AsyncService = 3 } internal class CallerOriginHelper { internal static CallerOrigin GetCallerOrigin( Microsoft.Xrm.Sdk.IPluginExecutionContext context) { if (context.GetType().Name == "SandboxPluginExecutionContext") return CallerOrigin.Undefind; object callerorigin = context.GetType().GetProperty("CallerOrigin").GetValue(context, null); switch (callerorigin.GetType().Name) { case "ApplicationOrigin": return CallerOrigin.Application; case "AsyncServiceOrigin": return CallerOrigin.AsyncService; case "WebServiceApiOrigin": return CallerOrigin.WebService; } return CallerOrigin.Undefind; } } }
Here is a sample how to use this helper:
var callerorigin = CallerOriginHelper.GetCallerOrigin(localContext.PluginExecutionContext);
Great!
ReplyDeleteThanks Andrii, I will try it next week when returning back to my customer office
Very cool
ReplyDeleteThen what to do for achieving this in online,please suggest me...
ReplyDeleteHello, Unfortunately there it is impossible to make this code work for CRM Online.
DeleteNice simple solution, well done, thanks for sharing.
ReplyDeleteWelcome! Unfortunately this is unsupported solution that can be broken with new rollups...
DeletePS Personally, it works better as an extension method on IPluginExecutionContext, but that's just my preference.
ReplyDeletepublic static CallerOrigin GetCallerOrigin(this IPluginExecutionContext context)
{
if (context.GetType().Name == "SandboxPluginExecutionContext")
return CallerOrigin.Undefind;
object callerorigin =
context.GetType().GetProperty("CallerOrigin").GetValue(context, null);
switch (callerorigin.GetType().Name)
{
case "ApplicationOrigin":
return CallerOrigin.Application;
case "AsyncServiceOrigin":
return CallerOrigin.AsyncService;
case "WebServiceApiOrigin":
return CallerOrigin.WebService;
}
return CallerOrigin.Undefind;
}
Thanks for good idea!
Delete