Friday, November 23, 2018

Fetch Marketing List of a campaign with C# plugin in Dynamic 365 CRM


How to fetch the marketing lists associated with a campaign or campaign activity in Dynamic 365 CRM?

Within Dynamics 365 CRM, there are a number of entities that have N:N relationships. One of these is the CampaignItem entity that connects Campaigns, Marketing Lists, Products and Sales Literature.

I have created a generic method below in C# with detail explanations.



Here is how I am invoking my method

EntityCollection DynamicMarketingLists =
    GetManytoManyRelationShip(CampaignActivityId,
                                    "list",
                                    "listid",
                                    "campaignactivitylist_association",
                                    "campaignactivity",
                                    "subject",
                                    _service);




Here is method definition


private EntityCollection GetManytoManyRelationShip(
    Guid entityBGuid,
    string offenceEntityName,
    string offenceColumnSet,                                                            
    string relationShipName,
    string legalEntityName,
    string legalColumnSet,                                                            
    IOrganizationService _service)
{
 QueryExpression query = new QueryExpression(offenceEntityName);
 ColumnSet cols = new ColumnSet();
 cols.AddColumn(offenceColumnSet);
 cols.AddColumn("listname");
 query.ColumnSet = cols;

 Relationship relationship = new Relationship(relationShipName);
 RelationshipQueryCollection relationshipColl = new  RelationshipQueryCollection();
 relationshipColl.Add(relationship, query);

 RetrieveRequest request = new RetrieveRequest();
 request.RelatedEntitiesQuery = relationshipColl;
 request.Target = new EntityReference(legalEntityName, entityBGuid);
 request.ColumnSet = new ColumnSet(legalColumnSet);
 RetrieveResponse response = (RetrieveResponse)_service.Execute(request);

 return response.Entity.RelatedEntities[relationship];
}











Thanks.
Vipin Jaiswal
Vipinjaiswal12@gmail.com

No comments: