Stefan Pienaar
I would love to change the world, but they won't give me the source code

MS CRM - Using Generic Methods to call Web Service

September 10, 2008 12:02 by StefanPienaar

I just started out with a company specializing in CRM deployments and customizations through the exposed web service. So naturally I've been playing around with with some of the QueryExpression and QueryByAttribute Classes.

Working though some examples in the book I have (yes I'm a total newby when it comes to CRM) I thought that there has to be a cleaner way to deal with querying data so I had a look around if anyone else has written something...

This is what I found: Microsoft CRM 3.0 Web Service Calls With Generic Methods. Now I haven't used this extensively nor am I familiar enough with CRM to know if this would have any negative impacts along the line but it looked like a clean solution which is easy to expand by writing other overloaded methods.

Here is the main method which can then be overloaded for simplicity. Have a look at the original post for some overloaded methods:

 

public static T[] FindEntities<T>(string[] attributes, object[] values, 
    ConditionOperator[] operators, string[] columns, string[] orderAttribute, 
    OrderType[] orderType) where T : BusinessEntity
{
    if (_service == null)
        _service = GetInstance();
 
    QueryExpression query = new QueryExpression();
    query.EntityName = typeof(T).Name;
 
    ConditionExpression[] conditionExpressions = null;
 
    if (attributes != null && values != null)
    {
        conditionExpressions = new ConditionExpression[attributes.Length];
 
        for (int i = 0; i < conditionExpressions.Length; i++)
        {
            ConditionExpression conditionExpression = new ConditionExpression();
            conditionExpression.AttributeName = attributes[i];
            conditionExpression.Operator = 
                operators == null ? ConditionOperator.Equal : operators[i];
            conditionExpression.Values = new object[] { values[i] };
 
            conditionExpressions[i] = conditionExpression;
        }
    }
 
    FilterExpression filterExpression = new FilterExpression();
    filterExpression.FilterOperator = LogicalOperator.And;
    filterExpression.Conditions = conditionExpressions;
    query.Criteria = filterExpression;
 
    if (orderAttribute != null)
    {
        for (int i = 0; i < orderAttribute.Length; i++)
        {
            OrderExpression order = new OrderExpression();
            order.AttributeName = orderAttribute[i];
            order.OrderType = 
                orderType[i] == null ? OrderType.Ascending : orderType[i];
            query.Orders = new OrderExpression[] { order };
        }
    }
 
    ColumnSetBase columnSet = null;
 
    if (columns == null)
    {
        columnSet = new AllColumns();
    }
    else
    {
        columnSet = new ColumnSet();
        ((ColumnSet)columnSet).Attributes = columns;
    }
 
    query.ColumnSet = columnSet;
    query.EntityName = typeof(T).Name;
 
    BusinessEntityCollection result = _service.RetrieveMultiple(query);
 
    T[] entities = new T[result.BusinessEntities.Length];
    Array.Copy(result.BusinessEntities, entities, entities.Length);
    return entities;
}

Hope that makes a fellow CRM newby's life a little easier :)


Categories: Microsoft CRM 4
Actions: E-mail | Permalink | Comments (0)