CRM Plug-in

Posted on 2009-04-23
Last Modified: 2012-05-06
I have a form in CRM called "Manual Purchase". By using a plug - in I want to retrieve the columns on that form and read the data out of that. Can someone tell me what kind of code I will need in order to retrieve the entity and then go through the controls of the entity.
I have attached a piece of code which does the same kind of stuff with "Opportunity" screen.
The problem I have is that I do not have somethng similiar to "TargetRetrieveOpportunity" for Manual Purchase,something like "TargetRetrieveManualPurchase" or similiar.

TargetRetrieveOpportunity target = new TargetRetrieveOpportunity();

                            target.EntityId = opportunityID;

                            RetrieveRequest retrieve = new RetrieveRequest();

                            retrieve.Target = target;

                            retrieve.ColumnSet = new AllColumns();

                            retrieve.ReturnDynamicEntities = true;

                            RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);

Open in new window

Question by:pratikshahse
    LVL 11

    Expert Comment

    You have to use dynamic entity.  You have to go through sdk to learn this.  I will pass on the code if i get one

    Rakesh Agarwal
    LVL 9

    Accepted Solution


    If this is a Post Plug-in you will receive only what has changed in the Plug-in execution context.  If you want to look at all fields of the form you'll have to get the id from the plug-in execution context and retrieve the full entity using all columns.  After this has been retrieved you can look at all the fields.

    Depending on the plug-in message will depend on how you retrieve the entity id.  Let's assume it's a post update plug-in then you would do it something like the below:

    public void Execute(IPluginExecutionContext context)
                    case "account":
                    case "new_manualpurchase":

    private void ProcessManualPurchases(IPluginExecutionContext context)
              if (context.Stage.ToString() == "50")
                    if (context.MessageName.ToLower() == "update")
                        if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
                            DynamicEntity manualPurchase = (DynamicEntity)context.InputParameters.Properties["Target"];
                            Key manualPurchaseKey = (Key) manualPurchase.Properties["new_manualpurchaseid"];
                            DynamicEntity fullManualPurchase = GetFullDynamic(manualPurchaseKey.Value.ToString(), "new_manualpurchaseid", "new_manualpurchase");
                            //Do something with the dynamic entity

    private DynamicEntity GetFullDynamic(string entityId, string fieldName, string entityName)
                ConditionExpression con1 = new ConditionExpression();
                con1.AttributeName = fieldName;
                con1.Operator = ConditionOperator.Equal;
                con1.Values = new string[] { entityId };
                FilterExpression filter = new FilterExpression();
                filter.FilterOperator = LogicalOperator.And;
                QueryExpression query = new QueryExpression();
                query.ColumnSet = new AllColumns();
                query.Criteria = filter;
                query.EntityName = entityName;

                RetrieveMultipleRequest request = new RetrieveMultipleRequest();
                request.Query = query;
                request.ReturnDynamicEntities = true;

                    RetrieveMultipleResponse response = (RetrieveMultipleResponse)crmServ.Execute(request);
                    if (response != null && response.BusinessEntityCollection.BusinessEntities.Count == 1)
                        return (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];
                    //Null or more than one entity found so return null
                    return null;
                catch (System.Web.Services.Protocols.SoapException ex)
                    string errorMessage = ex.Message.ToString();
                    return null;

    Now I've guessed at the names obviously and changed the code outside of VS so there may be a couple of syntax errors but you get the idea...

    Use the DynamicEntity.Properties.Contains method to check if your fields exists and go from there.


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    On Sep 22nd 2014 Microsoft released Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1 and back in July Update Rollup 3 was released.  So we now have:   Update Rollup 1Update Rollup 2Update Rollup 3Service Pack 1Update Rollup 1 for S…
    Automatically creating a Trello card using data from a Microsoft Dynamics CRM record turned out to be an easy project that yielded great results.  Here's how I did this for an internal team at General Code.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now