web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to post xml data using webapi in MS CRM using HTML web resource?

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans