Saturday, September 17, 2011

Get All Activities based on ActivityPointer and ActivityParty through QueryExpression and LinkEntity

Database Query: "SELECT * FROM ActivityPointerBase INNER JOIN ActivityPartyBase ON ActivityPartyBase.activityId = ActivityPointerBase.activityid"

static void GetAllActivities()
        {
            CRMHelper objCRMHelper = new CRMHelper();
            CrmService service = objCRMHelper.GetCRMService();

            AllColumns activityPointerBaseColumns = new AllColumns();

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

            LinkEntity objLinkEntity = new LinkEntity();
            objLinkEntity.LinkFromEntityName = EntityName.activitypointer.ToString();
            objLinkEntity.LinkFromAttributeName = "activityid";
            objLinkEntity.LinkToEntityName = EntityName.activityparty.ToString();
            objLinkEntity.LinkToAttributeName = "activityid";

            // Link from ActivityPointer to ActivityPartyBase
            query.LinkEntities = new LinkEntity[] { objLinkEntity };

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