Development

MS CRM 2011: CallerOrigin in plugins

Yesterday well-known MVP Tanguy Touzard, author of amazing tools for Dynamics CRM 4.0/2011 asked about CallerOrigin property in plugins for Dynamics CRM 2011. This property worked for CRM 4.0 and returned the source of plugin invocation (Application, AsyncService or WebService). I and Gayan suggested to use HttpContext class from System.Web namespace to check source.
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:

 

Here is a sample how to use this helper:

 

Unfortunately this solution works only for plugins that are not registered in Isolation, so for CRM Online it would not work – Undefined value would be returned.

8 Comments on “MS CRM 2011: CallerOrigin in plugins

  1. PS Personally, it works better as an extension method on IPluginExecutionContext, but that's just my preference.

    public 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;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *