Saturday, September 17, 2011

Retrive all Activities for specific Case in MS-CRM 4.0 through QueryExpression, FilterExpression and ConditionExpression

Database Query: "SELECT * FROM ActivityPointerBase WHERE RegardingObjectId = 'BF0F0BD9-FCCA-E011-9E6F-00155DE0140E'"

static void GetActivitiesFromSpecificEntity()
        {
            CRMHelper objCRMHelper = new CRMHelper();
            CrmService service = objCRMHelper.GetCRMService();
            AllColumns activityPointerBaseColumns = new AllColumns();

            QueryExpression query = new QueryExpression();
            query.ColumnSet = activityPointerBaseColumns;
            query.EntityName = EntityName.activitypointer.ToString();

            //WHERE CONDITION
            FilterExpression activityFilter = new FilterExpression();

            ConditionExpression activityCondition = new ConditionExpression();
            activityCondition.AttributeName = "regardingobjectid";
            activityCondition.Operator = ConditionOperator.Equal;
            activityCondition.Values = new object[] { "BF0F0BD9-FCCA-E011-9E6F-00155DE0140E" };//Case ID
            activityFilter.Conditions = new ConditionExpression[] { activityCondition };

            query.Criteria = activityFilter;

            BusinessEntityCollection result = service.RetrieveMultiple(query);
            if (result.BusinessEntities.Length > 0)
            {
                foreach (BusinessEntity objBE in result.BusinessEntities)
                {
                }
            }
        }