Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Is it unable to use Xrm.WebAPI methods in Dynamics 365 V9.0 HTML webresources ?

Posted on by 240

Hi All,

Recently I have a piece of html web resource containing some JavaScript code which utilize the latest Dynamics CRM V9 client API for creating/updating records.

The html page contains some simple button and already included the clientGlobalcontext.js

(for getting the Xrm namespace)

 <script src="../../clientGlobalcontext.js.aspx"></script>

The code example is as the following:

var contactObj = new Object();

contactObj ["firstname"] = "John";

contactObj ["lastname"] = "Smith";

Xrm.WebApi.createRecord("contact", contactObj).then(successCallBack, errorCallBack);

However, the code seems does not working. It throwing an error in my error call back method, and complaining "contact" property is null or undefined.

Then I have to switch the code by using the old fashioned XMLHttpRequest to perform a web api call. Which works normal.

Using the code like the following:

var req = new XMLHttpRequest();
req.open("POST", encodeURI(Util.getWebAPIEndpoint() + type), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function () {

   //.....

}

So just one question: 

Is it possible to use the standard wrapped Xrm.WebApi (CRUD) methods in Dynamics V9.0 html webresources (for example, html pages)? 

Many thanks.

Regards

Harry

*This post is locked for comments

  • ocnerf Profile Picture
    ocnerf 135 on at
    RE: Is it unable to use Xrm.WebAPI methods in Dynamics 365 V9.0 HTML webresources ?

    Hi,

    I'm experiencing the same issue but with a Webresource called from a javascript function triggered by a command button.

    Thanks

  • Suggested answer
    Shidin Haridas Profile Picture
    Shidin Haridas 3,497 on at
    RE: Is it unable to use Xrm.WebAPI methods in Dynamics 365 V9.0 HTML webresources ?

    Yes, you can.

    This is how I am doing it in one of my HTML web-resources.

    <script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>

    Later, in the script section, I use the parent.Xrm to get the Xrm object.

    I hope you are referring to HTML web-resources which are placed on your CRM forms.

    If this answer helps you, please mark it as verified.

    Cheers!

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Is it unable to use Xrm.WebAPI methods in Dynamics 365 V9.0 HTML webresources ?

    Hi Harry ,

    Try to with window.parent- 

    window.parent.Xrm.WebApi.createRecord("contact", contactObj).then(successCallBack, errorCallBack);

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans