Example of Dynamic Entity Retrieval

by Bill Owens 12. February 2009 23:58

by Danny Varghese 01.31.09


There have been numerous requests on other blogs about sample code to on how to retrieve entities in CRM. One way is to use the CRM web service to retrieve business entities, however by doing so, you're only limited to out-of-the-box entities with system attributes. To retrieve anything more "dynamic," you'll have to employ other methods.  Please remember that in order to retrieve any record, you must have the proper permissions on that entity.

Below is a code example of how to retrieve a record with an id using dynamic entity retrieve:

public DynamicEntity RetrieveEntity()


{


            //variable initialization     


            TargetRetrieveDynamic target = new TargetRetrieveDynamic();         


            RetrieveRequest retrieveRequest = new RetrieveRequest();            


            RetrieveResponse retrieveResponse = null;                                       


            DynamicEntity entity = null;                                      


 


            target.EntityName = <name of entity here>


            target.EntityId = <id of entity here>


 


            //initialize request parameters


            retrieveRequest.ColumnSet = new AllColumns();


            retrieveRequest.ReturnDynamicEntities = true;


            retrieveRequest.Target = target;


 


            //build the response object


            retrieveResponse = (RetrieveResponse)GetCrmService().Execute(retrieveRequest);


 


            //retrieve the service order item from the response


            entity = (DynamicEntity)retrieveResponse.BusinessEntity;


 


            return entity;


}


 




 



The above example is a simple one, but the example below retrieves all contacts that have an account id = some id, and also retrieve the records with only a certain attributes. This is probably a more robust example encompassing many retrieval options:



 





private ArrayList RetrieveMultipleContacts(ICrmService crmService, Guid parentAccountId)


{


    //variable initialization


    ConditionExpression condition = new ConditionBLOCKED EXPRESSION;


    FilterExpression filter = new FilterBLOCKED EXPRESSION;


    QueryExpression query = new QueryBLOCKED EXPRESSION;


    RetrieveMultipleRequest request = new RetrieveMultipleRequest();


    ColumnSet cols = new ColumnSet();


    RetrieveMultipleResponse response = null;


    ArrayList contacts = new ArrayList();


 


    //Set the condition for retrieval


    condition.AttributeName = "parentcustomerid";


    condition.Operator = ConditionOperator.Equal;


    condition.Values = new string[] { parentAccountId.ToString() };


 


     //Set the properties of the filter.


    filter.FilterOperator = LogicalOperator.And;


    filter.AddCondition(condition);


 


    //Set the attributes needed to be returned.  NOTE: The CRM Sdk has an erroneous example


    //of how to set the attributes for retrieval. 


    cols.Attributes.Add("address1_line1");


    cols.Attributes.Add("address1_line2");


    cols.Attributes.Add("address1_line3");


    cols.Attributes.Add("address1_city");


    cols.Attributes.Add("address1_stateorprovince");


    cols.Attributes.Add("address1_postalcode");


    cols.Attributes.Add("address1_country");


    cols.Attributes.Add("telephone1");


    cols.Attributes.Add("fax");


 


    //Set the properties of the QueryExpression object.


    query.EntityName = EntityName.contact.ToString();


    query.ColumnSet = cols;


    query.Criteria = filter;


 


    //Set the query for the request and set the flag to return


    //dynamic entities


    request.Query = query;


 


    //retrieve the contacts


    response = (RetrieveMultipleResponse)crmService.Execute(request);


    foreach (BusinessEntity cont in response.BusinessEntityCollection.BusinessEntities)


    {


        contacts.Add(cont);


    }


 


    return contacts;


}



I hope these examples help someone, happy coding!

Tags: , , , ,

CRM 4.0

Page List

About the author

I work for a consulting firm in Dublin Ohio called Affiliated Resource Group. For the last five years I have been spearheading our Microsoft Dynamics CRM practice. I have a deep appreciation for the Microsoft CRM platform and I am very excited about it. You might even describe me as a Microsoft CRM Advocate. I have many battle scars from my experience with the product and I’m constantly being asked questions about CRM and how-to-do something in it. Hence, this BLOG is to help disseminate that knowledge and information to everyone. As of last year I was posting links to many other blogs to help spread the knowledge, but now with the community.dynamics.com doing that for me, I will be following that practice unless a really juicy article catches my eye. Many people have asked where my post are for the first half of 2010, my company had me posting to another blog and maintain two was near impossible. I am now down to just this blog. So good luck and I hope that this blog may help in some way. If you have suggestions or questions, please email me them.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 BillOnCRM