Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Write data in XML file

Posted on by 12,070 Super User 2024 Season 1

Hi all

I have XML as shown below

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<APIkey>ab15c4d132d2argrjfvvb952ca886a92e5a-us14</APIkey>
</Configuration>

I want to change this APIkey when I have a new APIkey. How can I change it by using Javascript.?

Thank You

*This post is locked for comments

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Write data in XML file

    Hi Moshe Hayun

    Than How can I update my value? By using your code only. It is not updating xml file.

    Thank You

  • Suggested answer
    Moshe Hayun Profile Picture
    Moshe Hayun 365 on at
    RE: Write data in XML file

    You cannot save the WebResource like that, you must use CRM WebServices (Update & Publish request) operations.

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Write data in XML file

    Hi Moshe Hayun, Alok Srivastav, Shidin Haridas and Sandeep Mishra

    I use the following code

    function Test(){

    var serverUrl = window.parent.Xrm.Page.context.getClientUrl();

    var webResourceName = "apikey"

    var webResourceURL = WebResourceURL(webResourceName);

    var nodePath = "//Configuration";

    var XmlDoc= new ActiveXObject("Microsoft.XMLDOM");

    XmlDoc.preserveWhiteSpace = true;

    XmlDoc.async = false;

    XmlDoc.load(webResourceURL);

    var nodelist;

    nodelist = XmlDoc.selectNodes(nodePath);

    document.getElementById('APIkey').value = nodelist[0].selectSingleNode("APIkey").nodeTypedValue;

    nodelist[0].selectSingleNode("APIkey").nodeTypedValue = "sdsds"//nodeTypedValue = "sdsd";

    XmlDoc.save(nodePath);

    //nodelist[0].selectSingleNode("APIkey").value("fgdddft");

    //XmlDoc.selectNodes(nodePath).set("sdsd")

    webResourceName = "logo";

    document.getElementById('logo').src = WebResourceURL(webResourceName);

    }

    It is giving me error at

    4278.Untitled5.png

    How may resolve this issue?

    Thank You

  • Suggested answer
    Dynamics_Alok Profile Picture
    Dynamics_Alok 1,746 on at
    RE: Write data in XML file

    Yes ,you can change it dynamically by 

    xmlDoc.getElementsByTagName("APIkey")[0].childNodes[0].nodeValue = Your New APIKey.
    

  • Suggested answer
    Moshe Hayun Profile Picture
    Moshe Hayun 365 on at
    RE: Write data in XML file

    Hi Abdul,

    If you're still using the ActiveXObject (like on your previous post), you can update the node value by this code:

    var XmlDoc= new ActiveXObject("Microsoft.XMLDOM");
    XmlDoc.preserveWhiteSpace = true;
    XmlDoc.async = false;
    XmlDoc.load("C:/Users/moshehay/Desktop/demo.xml");
    var nodelist;
    nodelist = XmlDoc.selectNodes("//Configuration");
    //document.getElementById("demo2").innerHTML = nodelist[0].selectSingleNode("APIkey").nodeTypedValue;
    nodelist[0].selectSingleNode("APIkey").nodeTypedValue = "MyNewApiKey";


    Don't forget to update your WebResource! 

  • Suggested answer
    Shidin Haridas Profile Picture
    Shidin Haridas 3,497 on at
    RE: Write data in XML file

    You can take advantage of the jQuery library to modify the XMl file.

  • Suggested answer
    sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: Write data in XML file

    Hi,

    You can use XML DOM to read and modify Xml content. Please visit this link.

    www.peachpit.com/.../article.aspx;seqNum=4

    If this answer is helpful, don't forget to verify it.

    Thanks,

    Sandeep

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans