Dec 14, 2011

Avoid plug-in when call comes from JavaScript

Though, it’s not new for most of the CRM developers, I thought of writing about caller origin since I solve one of my recent scenarios with this approach. This is practically very important.

My scenario was to do a calculation in creation of quote product. Speciality in this case is that, quote products were to be added as usual through the form and also through a tool (i.e. experlogix) which uses the CRM service. Obviously I have to have a plug-in. Then I am also told that user can override some calculations when creating quote products through form and put some arbitrary values. Then I came to a point that plug-in shouldn’t work when creation happen through a native CRM form. This is the place I used the caller Origin before executing the plug-in steps. Below code did the trick for me.

if (context.CallerOrigin.GetType() == typeof(Microsoft.Crm.Sdk.WebServiceApiOrigin))
{
  //plug-in steps
}

Below are the three caller origin types we can use to filters an execution.

ApplicationOrigin   - Call from Application
AsyncServiceOrigin  - Call from Async service (identify actions occur through workflows)
WebServiceApiOrigin - Call from Web Service