Tuesday, June 5, 2012

Retrieve Plugin: MS CRM 2011

I was curious in retrieving the information from the context in Retrieve Plugin message. I realized that the context.InputParameters[“Target”], would return null/ throw exception in the retrieve plugin message.
  1.  To overcome that and to access the attribute collection through the context, here are the steps.
Plugin Configuration


2. The changes that needs to go in to the Plugin Retrieve message code. We need to make use of the OutputParameters collection.

            var orderRetrieve = (Entity) context.OutputParameters["BusinessEntity"];
            DateTime? date = null;
            if (orderRetrieve.Attributes.Contains("modifiedon"))
                date = (DateTime)orderRetrieve.Attributes["modifiedon"];



Also, couple of constraints, that I noticed in the Retrieve message plugin,
·         We cannot debug the Retrieve plugin by throwing an exception, in turn, we will not be able to see the tracing messages.
·         We cannot run plugin profiler to debug.

There is a way to handle the above constarints. will be blogging it on those topic soon...

8 comments:

Anonymous said...

Hi Chaitanya,
Thanks for the Tips, Can you update me how to handle the Debugging constraints for the Retreive Message

Chaitanya... said...

Yes, its long pending, I will try to blog on this soon, till then, the possible solution that I am thinking of would be a Log4net integration, which basically helps us in capturing the information at the required levels in a custom entity. That said you should try to create an another service context to work on this.

Thanks,
Chaitanya...

markpittsnh said...

Hello Chaitanya

I am trying to change the content of the results returned by the 'Retrieve' request. I tried using the 'pre-operation' stage and accessing the 'inputparameters' collection. Unfortunately, my plugin fails because this collection is not available. Since this is 'PRE' stage, the 'outputparameters' collection is also not available.

QN: Do you know if it is possible to change the results returned from a 'Retrieve' request?

Cheers.

Unknown said...

Hi Chaitanya. Thank you for this post. I have a question for you in regards to this. I'm trying to understand where in the MSDN documentation you were able to find "BusinessEntity" property defined? I've checked both the RetrieveRequest & Response, but I don't see this property defined.

http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.messages.retrieverequest_properties.aspx

Chaitanya... said...

I think, I would say this is a trick to retrieve the data with out using an extra service call. For Retrieve message the Entity is in the output parameters and is called "BusinessEntity". I know, this is 4.0 fashion but works on 2011 as well. Keep in mind on the Plugin configuration part of it to configure on the Post event.

Let me know, if you need any additional information on this?

Thanks,
Chaitanya...

Anonymous said...

((Microsoft.Xrm.Sdk.EntityReference)(context.InputParameters["Target"])

This works with retrieve.

Thanks,
Prem

Hache said...

Anybody knows how to debug a Retrieve Multiple Plugin?

Anonymous said...

after fetching businessEntity, I am not able to get the value of “createdby” field value from it.

means EntityReference er = orderRetrive.GetAttributeValue("createdby");
not working., please help