Random Musings

December 8, 2008

RetrieveByAttribute Code Snippet

Filed under: Dynamics CRM — haditeo @ 3:28 am
Tags: , ,

My custom RetrieveByAttribute code snippet is frequently used to retrieve an object based on one attribute.

In example, SELECT ContactId, FirstName,EmailAddress1 FROM Contact WHERE EmailAddress1 = ’email@email.com’

This is the signature of the method :

public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)

This is how to invoke this method based on the above query

BusinessEntityCollection becContact = Utilities.RetrieveByAttributeReturnColl(ref service, "contact", "emailaddress1", "email@email.com", new string[] {"contactid", "firstname", "emailaddress1"});

When there is multiple contact records returned by this method, the collection can be accessed by using becContact.BusinessEntities.

Here is the definition of the method:


public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)
{
    try
    {
	// Create the ConditionExpression.
	ConditionExpression condition = new ConditionExpression();

	// Set the condition for the retrieval  
	condition.AttributeName = attributes;
	condition.Operator = ConditionOperator.Like;
	condition.Values = new string[] { values };

	// Create the FilterExpression.
	FilterExpression filter = new FilterExpression();

	// Set the properties of the filter.
	filter.FilterOperator = LogicalOperator.And;
	filter.Conditions.Add(condition);

	// Create the QueryExpression object.
	QueryExpression query = new QueryExpression();

	// Set the properties of the QueryExpression object.
	query.EntityName = entityName;

	if (colSet.Length > 0)
	{
	    ColumnSet col = new ColumnSet();

	    foreach (string s in colSet)
	    {
               col.AddColumn(s);
	    }

	    query.ColumnSet = col;
	}
	else
	{
	    query.ColumnSet = new AllColumns();
	}

	query.Criteria = filter;

	// Create the request object.
	RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

	// Set the properties of the request object.
	retrieve.Query = query;
	retrieve.ReturnDynamicEntities = true;

	// Execute the request.
	RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retrieve);

	return retrieved.BusinessEntityCollection;
    }
    catch (System.Web.Services.Protocols.SoapException ex)
    {
        WriteEntryToEventLog("public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)", ex);

         throw ex;
    }
    catch (Exception e)
    {
        WriteEntryToEventLog("public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)", e);

        throw e;
    }
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: