Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to post xml data using webapi in MS CRM using HTML web resource?

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans