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:

 


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!

No comments: