Showing posts with label dotnetmag. Show all posts
Showing posts with label dotnetmag. Show all posts


Retrieve Marketinglists attached to Campaign Activity

Today I've been doing some coding again and I've found an interesting situation. For my code I do need to find which marketing lists belong to a specific Campaign Activity. Apparently more people on the internet faced the same issue, but I haven't found anybody who supplied the answer on how to do that.

I've managed to find an approach which works for both the fetchXml as well as a QueryExpression. Here is the code for both of these options:

FetchXml:


StringBuilder sbFetchXml = new StringBuilder();
sbFetchXml.Append("<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"true\">");
sbFetchXml.Append("<entity name=\"list\">");
sbFetchXml.Append("<attribute name=\"listname\"/><attribute name=\"listid\"/>");
sbFetchXml.Append("<order attribute=\"listname\" descending=\"true\"/>");
sbFetchXml.Append("<link-entity name=\"campaignactivityitem\" from=\"itemid\" to=\"listid\" visible=\"false\" intersect=\"true\">");
sbFetchXml.Append("<link-entity name=\"campaignactivity\" from=\"activityid\" to=\"campaignactivityid\" alias=\"aa\">");
sbFetchXml.Append("<filter type=\"and\">");
sbFetchXml.AppendFormat("<condition attribute=\"activityid\" operator=\"eq\" uitype=\"campaignactivity\" value=\"{0}\"/>", campaignActivityId);
sbFetchXml.Append("</filter>");
sbFetchXml.Append("</link-entity>");
sbFetchXml.Append("</link-entity>");
sbFetchXml.Append("</entity>");
sbFetchXml.Append("</fetch>");

string strXmlResult = service.Fetch(sbFetchXml.ToString());


QueryExpression:

ConditionExpression condActivityId = new ConditionExpression();
condActivityId.AttributeName = "activityid";
condActivityId.Operator = ConditionOperator.Equal;
condActivityId.Values = new object[] { campaignActivityId };

FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] { condActivityId };

LinkEntity leCampaignActivity= new LinkEntity();
leCampaignActivity.LinkFromEntityName = EntityName.campaignactivityitem.ToString();
leCampaignActivity.LinkFromAttributeName = "campaignactivityid";
leCampaignActivity.LinkToEntityName = EntityName.campaignactivity.ToString();
leCampaignActivity.LinkToAttributeName = "activityid";
leCampaignActivity.LinkCriteria = filter;

LinkEntity leCampaignActivityItem = new LinkEntity();
leCampaignActivityItem.LinkFromEntityName = EntityName.list.ToString();
leCampaignActivityItem.LinkFromAttributeName = "listid";
leCampaignActivityItem.LinkToEntityName = EntityName.campaignactivityitem.ToString();
leCampaignActivityItem.LinkToAttributeName = "itemid";
leCampaignActivityItem.LinkEntities = new LinkEntity[] {leCampaignActivity};

QueryExpression query = new QueryExpression();
query.EntityName = EntityName.list.ToString();
query.ColumnSet = new AllColumns();
query.LinkEntities = new LinkEntity[] { leCampaignActivityItem };

BusinessEntityCollection bec = service.RetrieveMultiple(query);

For me this worked, I hope this helps you as well!