Using C# to Add Members to a Static Segment in Dynamics for Marketing

Adam Murchison, 31 January 2022

When adding a list of members into a segment from Excel, you can’t always query them from Dynamics 365. There are many scenarios, such as you have a list of email addresses that may or may not exist in Dynamics 365. Microsoft suggests that we add a new field onto contact in this documentation. Microsoft’s suggestion doesn’t allow for people to be added to multiple Dynamic segments at a time.

Solution:

We can fix this problem by using the Action - msdyncrm_SegmentMembersUpdate. This allows you to upload a single contact, or a list of contacts into the segment by invoking the action and passing through the correct parameters. The parameters are ‘msdyncrm_operation’ this is set to the string “addByIds”, ‘msdyncrm_segmentid’ this is set to the segments id in a string format e.g. “6a7cdb328-7413-e811-a1g0-02bf0zd152db” and ‘msdyncrm_memberids’ this is set to the contacts id in string format, for singular you’d set it as “6a7cdb328-7413-e111-a1h0-02bf0zd152dc” or as a list you’d set it as "[\"6a7cdb328-7413-e111-a1h0-02bf0zd152dc \", \"9p7cdb328-7513-e121-a1h0-04bg0zd153ef \"]"; The most important part is that the ID’s are all of the type string.

Code:

OrganizationRequest request = new OrganizationRequest("msdyncrm_SegmentMembersUpdate");

request["msdyncrm_operation"] = "addByIds";

request["msdyncrm_segmentid"] = "6a7cdb328-7413-e811-a1g0-02bf0zd152db";

request["msdyncrm_memberids"] = "[\"6a7cdb328-7413-e111-a1h0-02bf0zd152dc \", \"9p7cdb328-7513-e121-a1h0-04bg0zd153ef \"]";

sdk.Execute(request);

Summary:

In conclusion, this example is another way to add contacts to a static segment. For our situation, this was a better way than Microsoft’s suggested implementation and you can use this code to implement your own custom way of adding contacts to static segment.