虽然这似乎已经被问过一段时间了(我认为 OP 现在已经找到了他的解决方案!)我最近遇到了它,正在寻找类似的答案。需要进一步研究才能找到我需要的东西,因此我也会在这里为遇到它的其他人添加它。
Firstly, if you are looking for it, this property has been made obsolete. Supposedly because it was unreliable, but there were a few reasons why we needed the CallerOrigin in MSCRM 4.0. On the other hand, there are ways around this becoming obsolete too:
Prevent infinite loops (over 2 plugins)
This was the reason I was looking for the CallerOrigin and how I came across this question. I only wanted the plugin to fire if it came from a user on the form, not from another plugin (i.e. asyc process/webservice). In my case the distinction of it being "over 2 plugins" is quite important, because I cannot use InputParameters to solve the problem. My example was similar to the following:
Update Plugin for "Parent" Entity. If optionset called "Status" on the parent entity was set to "Approved" I subsequently wanted to set a status on all the child entities to "Approved" as well.
Update Plugin for "Child" Entity. If the optionset called "Status" on the child entity was set to "approved", and all other children of the same parent has this set to "Approved" I needed to update the Status on the parent to approved as well.
This causes an infinite loop if you don't protect yourself against it. You also cannot use the InputParameters to solve it. One basic solution is to use depth checking:
context.PluginExecutionContext.Depth
If this is greater than 1 it has been called by another plugin/workflow. Note: If you have a workflow that is triggering the initial update you might want to be careful with what value you are checking for.
Prevent syncing issues from an offline client
We've been given different properties to help us distinguish these ones. Use these instead:
context.PluginExecutionContext.IsExecutingOffline
context.PluginExecutionContext.IsOfflinePlayback
Reacting differently depending on what the origin is
OK, so this is the only scenario where we really do need the CallerOrigin. The only way I think you'd be able to do this is by checking the type of the PluginExecutionContext itself. I know for async it's the type:
Microsoft.Crm.Asynchronous.AsyncExecutionContext
and for plugins it seems to be:
Microsoft.Crm.Extensibility.PipelineExecutionContext
Not sure what it is when coming from an external source, I unfortunately don't have any code available at the moment to test and figure this out. Outside of all that you would probably have to check:
PluginExecutionContext.ParentContext
The only other method I've come across for detecting where the update came from is using a custom flag on the form. So you could create an OptionSet called "OriginOfChange" (or something similar) with the options
- CRM Form (JavaScript onsave)
- Workflow
- Plugin
- etc.
Then what ever updates the entity sets this field during the update. In this way you could check the Input Parameters each time to see where the update has come from.
This last method is most likely the safest to employ if you need to react differently depending on the source.