Thursday, October 4, 2012

Get Linked Entities Attributes in Plugins

Somehow, I have always managed with QueryExpressions in my plugins. I have never (!!) used FetchXML to get data in plugins before, only in JavaScript.
One really nice thing you can do with FetchExpression, that you can’t with QueryExpression, is to get a linked entity’s attributes in the response. Sweet.

Here’s a simple code example on how to do this
  
 EntityCollection entityColl = new EntityCollection();
 FetchExpression fetchEx = new FetchExpression(
        @"<fetch mapping='logical'>
                    <entity name='new_contract'>
                         <attribute name='new_name' />                                  
                         <attribute name='new_contract_number' />
                                 <filter>
                                   <condition attribute='statecode' operator='eq' value='0' />
                                 </filter>
                                 <link-entity name='contact' from='contactid' to='new_contact_id'>
                                             <attribute name='emailaddress1' />
                                 </link-entity>
                    </entity>
          </fetch>");
 
 entityColl = serviceProxy.RetrieveMultiple(fetchEx);
 if(entityColl.Entities.Count > 0)
 {
         string contractName =  entityColl.Entities[0].Attributes["new_name"].ToString();
         string contractNumber = entityColl.Entities[0].Attributes["new_contract_number"].ToString();
         AliasedValue aliasedContactEmail = (AliasedValue)entityColl.Entities[0].Attributes["new_contract1.firstname"];
         string contactEmailAddress = aliasedContactEmail.Value.ToString();
         //Do something useful with the result
 };

3 comments:

  1. Fantastic is the most proper word to portray this blog.
    linkedin

    ReplyDelete
  2. It is a great website.. The Design looks very good.. Keep working like that!. FXGM

    ReplyDelete