Friday, October 30, 2009

Example of Dynamic Entity Retrieval

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:

 






   1: public DynamicEntity RetrieveEntity()
   2: {
   3: //variable initialization
   4: TargetRetrieveDynamic target = new TargetRetrieveDynamic();
   5: RetrieveRequest retrieveRequest = new RetrieveRequest();
   6: RetrieveResponse retrieveResponse = null;
   7: DynamicEntity entity = null;
   8: target.EntityName = <name of entity here>
   9: target.EntityId = <id of entity here>
  10: //initialize request parameters
  11: retrieveRequest.ColumnSet = new AllColumns();
  12: retrieveRequest.ReturnDynamicEntities = true;
  13: retrieveRequest.Target = target;
  14: //build the response object
  15: retrieveResponse = 
  16: (RetrieveResponse)GetCrmService().Execute(retrieveRequest);
  17: //retrieve the service order item from the response
  18: entity = (DynamicEntity)retrieveResponse.BusinessEntity;
  19: return entity;
  20: }
 

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:



   1:  
   2: private ArrayList RetrieveMultipleContacts(ICrmService crmService, Guid 
   3: parentAccountId) 
   4: { 
   5: //variable initialization 
   6: ConditionExpression condition = new ConditionBLOCKED EXPRESSION; 
   7: FilterExpression filter = new FilterBLOCKED EXPRESSION; 
   8: QueryExpression query = new QueryBLOCKED EXPRESSION; 
   9: RetrieveMultipleRequest request = new RetrieveMultipleRequest(); 
  10: ColumnSet cols = new ColumnSet(); 
  11: RetrieveMultipleResponse response = null; 
  12: ArrayList contacts = new ArrayList(); 
  13: //Set the condition for retrieval 
  14: condition.AttributeName = "parentcustomerid"; 
  15: condition.Operator = ConditionOperator.Equal; 
  16: condition.Values = new string[] { parentAccountId.ToString() }; 
  17: //Set the properties of the filter. 
  18: filter.FilterOperator = LogicalOperator.And; 
  19: filter.AddCondition(condition); 
  20: //Set the attributes needed to be returned. NOTE: The CRM 
  21: Sdk has an erroneous example 
  22: //of how to set the attributes for retrieval. 
  23: cols.Attributes.Add("address1_line1"); 
  24: cols.Attributes.Add("address1_line2"); 
  25: cols.Attributes.Add("address1_line3"); 
  26: cols.Attributes.Add("address1_city"); 
  27: cols.Attributes.Add("address1_stateorprovince"); 
  28: cols.Attributes.Add("address1_postalcode"); 
  29: cols.Attributes.Add("address1_country"); 
  30: cols.Attributes.Add("telephone1"); 
  31: cols.Attributes.Add("fax"); 
  32: //Set the properties of the QueryExpression object. 
  33: query.EntityName = EntityName.contact.ToString(); 
  34: query.ColumnSet = cols; 
  35: query.Criteria = filter; 
  36: //Set the query for the request and set the flag to return 
  37: //dynamic entities 
  38: request.Query = query; 
  39: //retrieve the contacts 
  40: response = (RetrieveMultipleResponse)crmService.Execute(request); 
  41: foreach (BusinessEntity cont in 
  42: response.BusinessEntityCollection.BusinessEntities) 
  43: { 
  44: contacts.Add(cont); 
  45: } 
  46: return contacts; 
  47: } 


I hope these examples help someone, happy coding!

No comments: