How to retrieve campaign and target product in Dynamics CRM 2011

To retrieve campaign and target products in Dynamics CRM 2011 follow below steps:

  1. Use Link Entities to get N:N relationship entity details
  2. Passing Campaign Activity Guid array and it will return campaign details and target products associated with campaign
private EntityCollection RetrieveCampaign(Guid[] campaignactivityids, IOrganizationService service)
        {
            EntityCollection ecCampaigns = new EntityCollection();
            try
            {
                QueryExpression query = new QueryExpression();
                query.EntityName = "campaignactivity";
                query.ColumnSet = new ColumnSet("regardingobjectid", "activityid");
                query.Criteria = new FilterExpression();
                query.Criteria.AddCondition(new ConditionExpression("activityid", ConditionOperator.In, campaignactivityids));
                LinkEntity objcamp = new LinkEntity("campaignactivity", "campaign", "regardingobjectid", "campaignid", JoinOperator.Inner);
                objcamp.Columns = new ColumnSet("new_campaignpriority");
                objcamp.EntityAlias = "campaign";
                LinkEntity objcamp1 = new LinkEntity("campaign", "campaignitem", "campaignid", "campaignid", JoinOperator.Natural);
                LinkEntity product = new LinkEntity("campaignitem", "product", "entityid", "productid", JoinOperator.Inner);
                product.Columns = new ColumnSet("productid");
                product.EntityAlias = "camapignproduct";
                objcamp1.LinkEntities.Add(product);
                objcamp.LinkEntities.Add(objcamp1);
                query.LinkEntities.Add(objcamp);
                ecCampaigns = service.RetrieveMultiple(query);
            }
            catch (Exception ex)
            {
                throw new Exception("An error occured while executing the workflow. Error Details : " + ex.ToString());
            }
            return ecCampaigns;
        }
This entry was posted in MS CRM 2011 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *