Hi,
I have following code to add MarkettingList Member. It is working fine. But I think it uses Odata. I want to submit this on MS AppSource. According to their guidelines, OData is now deprecated. So, they suggest to use WebApi.
Here is working code using OData.
var listIdValue = "53b0c3b3-54c1-e711-810e-5065f38a9b91";
var contactIdValue = 8b4597d2-34c1-e711-810e-5065f38a9b91";
var requestMain = ""
requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";
requestMain += " <s:Body>";
requestMain += " <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";
requestMain += " <request i:type=\"b:AddListMembersListRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";
requestMain += " <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>ListId</c:key>";
requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">" + listIdValue + "</c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>MemberIds</c:key>";
requestMain += " <c:value i:type=\"d:ArrayOfguid\" xmlns:d=\"schemas.microsoft.com/.../Arrays\">";
requestMain += " <d:guid>" + contactIdValue + "</d:guid>";
requestMain += " </c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " </a:Parameters>";
requestMain += " <a:RequestId i:nil=\"true\" />";
requestMain += " <a:RequestName>AddListMembersList</a:RequestName>";
requestMain += " </request>";
requestMain += " </Execute>";
requestMain += " </s:Body>";
requestMain += "</s:Envelope>";
debugger
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: serverUrl + "/XRMServices/2011/Organization.svc/web",
data: requestMain,
async: false,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");
},
success: function (data, textStatus, XmlHttpRequest) {
var response = "";
if (XmlHttpRequest.status === 200) {
response = $(XmlHttpRequest.responseText).find('b\\:value').text();
console.log(response);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
Can anybody please suggest how to post using WebApi? I have tried but didn't work.
*This post is locked for comments
Hi Chhaya,
Try the below code. This is same as above, the only difference is that it is using web api-
======================================
var parameters = {}; parameters.EntityId = "8b4597d2-34c1-e711-810e-5065f38a9b91"; var req = new XMLHttpRequest(); req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/lists(53b0c3b3-54c1-e711-810e-5065f38a9b91)/Microsoft.Dynamics.CRM.AddMemberList", false); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(JSON.stringify(parameters));
======================================
Hope this helps.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156