'Can't remove contact from marketing list via Dynamics 365 Web API

I'm struggling with removing (unlinking) a contact record from a marketing list via Web API of Dynamics 365 CRM. I was able to successfully link a contact record to a marketing list using the following logic:

var payload =
                new JObject(
                    new JProperty("List",
                        new JObject(
                            new JProperty("listid", listId),
                            new JProperty("@odata.type", "Microsoft.Dynamics.CRM.list"))),
                    new JProperty("Members",
                        new JArray(
                            from c in contactIds
                            select new JObject(
                                new JProperty("contactid", c),
                                new JProperty("@odata.type", "Microsoft.Dynamics.CRM.contact")))));

var request = new HttpRequestMessage(HttpMethod.Post, $"AddListMembersList")
{
    Content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(request).Result;

It seems that RemoveListMembersList should use a similar approach looking at the Web API RemoveListMembersList Action reference page on docs.microsoft.com. So I'm trying to unlink the contact like this:

var payload =
                new JObject(
                    new JProperty("List",
                        new JObject(
                            new JProperty("listid", listId),
                            new JProperty("@odata.type", "Microsoft.Dynamics.CRM.list"))),
                    new JProperty("Members",
                        new JArray(
                            from c in contactIds
                            select new JObject(
                                new JProperty("contactid", c),
                                new JProperty("@odata.type", "Microsoft.Dynamics.CRM.contact")))));

var request = new HttpRequestMessage(HttpMethod.Post, "RemoveListMembersList")
{
    Content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(request).Result;

But I got this error:

{
  "error":{
    "code":"","message":"Resource not found for the segment 'RemoveListMembersList'.","innererror":{
      "message":"Resource not found for the segment 'RemoveListMembersList'.","type":"Microsoft.OData.Core.UriParser.ODataUnrecognizedPathException","stacktrace":"   at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.CreateFirstSegment(String segmentText)\r\n   at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.ParsePath(ICollection`1 segments)\r\n   at Microsoft.OData.Core.UriParser.Parsers.ODataPathFactory.BindPath(ICollection`1 segments, ODataUriParserConfiguration configuration)\r\n   at Microsoft.OData.Core.UriParser.ODataUriParser.Initialize()\r\n   at System.Web.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath, ODataUriResolverSetttings resolverSettings, Boolean enableUriTemplateParsing)\r\n   at System.Web.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath)\r\n   at Microsoft.Crm.Extensibility.OData.CrmODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath)"
    }
  }
}

The request URI is:

http://baseEnvUrl/api/data/v8.2/RemoveListMembersList

Changing "RemoveListMembersList" to "Microsoft.Dynamics.CRM.RemoveListMembersList" does not help.

I've also tried to use a different approach:

var payload =
                new JObject(
                    new JProperty("ListMember",
                        new JObject(
                            new JProperty("listmemberid", contactId),
                            new JProperty("@odata.type", "Microsoft.Dynamics.CRM.listmember"))));

var request = new HttpRequestMessage(HttpMethod.Post, $"lists({listId})/Microsoft.Dynamics.CRM.RemoveMemberList")
{
    Content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(request).Result;

The request URI is:

http://baseEnvUrl/api/data/v8.2/lists(be7afd88-75d9-eb11-80dc-000d3a54c8ce)/Microsoft.Dynamics.CRM.RemoveMemberList

But this request returns a different error:

{
  "error":{
    "code":"","message":"Request message has unresolved parameters.","innererror":{
      "message":"Request message has unresolved parameters.","type":"Microsoft.Crm.CrmHttpException","stacktrace":"   at Microsoft.Crm.Extensibility.OData.CrmODataRoutingConvention.SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup`2 actionMap)\r\n   at System.Web.OData.Routing.ODataActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
    }
  }
}

what is wrong with these requests?



Solution 1:[1]

I know it's probably too late for you, but this is my function for it incase anyone stumbles upon this question:

public static string UpdateMarkertingList(bool add, string listId, string contactId, HttpClient client)
    {
        string addOrRemove = add ? "AddListMembersList" : "RemoveListMembersList";
        JObject jObject =
            new JObject(
                new JProperty("List",
                    new JObject(
                        new JProperty("listid", listId),
                        new JProperty("@odata.type", "Microsoft.Dynamics.CRM.list"))),
                new JProperty("Members",
                    new JArray(
                        new JObject(
                            new JProperty("accountid", contactId),
                            new JProperty("@odata.type", "Microsoft.Dynamics.CRM.account")))));

        var mlistCreateResponse = client.PostAsync(addOrRemove,
                    new StringContent(JObject.FromObject(jObject).ToString(),
                    Encoding.UTF8,
                    "application/json"));

        return mlistCreateResponse.Result.StatusCode.ToString();
    }

Solution 2:[2]

In my case, the issue was caused by the fact that the 8.2 version of Dynamics CRM does not support the RemoveListMembersList operation. It seems it's supported starting from the 9.0 or 9.1 version.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JBatstone
Solution 2 Jon Grey