XMLHttpRequest fails reading XML InfoPath form due to mso-application line

与世无争的帅哥 提交于 2020-01-16 18:01:27

问题


I'm trying to create an ASPX page on my SharePoint site to read existing InfoPath forms. Testing locally with JavaScript and XMLHttpRequest worked fine but when the page is uploaded to SharePoint something very odd happens if the XML file has a specific line of data in it. When testing with simple XML files this line causes a problem:

<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>

When present in the XML file I'm trying to read, something odd happens. Instead of getting the contents of the file I get what appears to be an HTML page from SharePoint. The page doesn't display anything and has references to InfoPath and SharePoint libraries. I have no idea where the HTML is coming from. Removing that single line from the XML file causes everything to work as expected. Running outside of SharePoint appears to work as well. I will include a sample XML file and code I used to test.

Update : If the input file extension is TXT and not XML then the problem goes away. I assume this means that SharePoint is running code when XML files are read and injecting itself into my get request.

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2017-05-05T14:19:13">
  <my:User_Name>Joe</my:User_Name>
  <my:Email_Address>joe.smith@abc.com</my:Email_Address>
</my:myFields>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="lib/jquery/jquery-3.4.1.min.js"></script>
    <title></title>

    <script>
            var oReq = new XMLHttpRequest();
            oReq.addEventListener("progress", updateProgress);
            oReq.addEventListener("error", transferFailed);
            oReq.addEventListener("abort", transferCanceled);
            oReq.addEventListener("loadend", transferComplete);

            function Test_Req_xml() {
                console.log("starting test_req_xml function");
                let filename = document.getElementById('inFileName').value;
                console.log("file name " + filename);
                oReq.addEventListener("load", transferComplete_xml);
                oReq.open("GET", filename);
                oReq.responseType = "document";
                oReq.send();
            }
            var transferComplete_xml = function (response) {
                console.log({ 'transferComplete xml response:': response });
                console.log({ 'oReq.responseXML': oReq.responseXML });
                console.log({ 'oReq.responseType': oReq.responseType });
                console.log({ 'oReq.responseURL': oReq.responseURL });
                console.log({ 'oReq': oReq });
                parseFile(oReq.responseXML.documentElement.outerHTML);
            };
            // progress on transfers from the server to the client (downloads)
            function updateProgress(oEvent) {
                if (oEvent.lengthComputable) {
                    var percentComplete = oEvent.loaded / oEvent.total * 100;
                    console.log("percent " + percentComplete);
                } else {
                    // Unable to compute progress information since the total size is unknown
                    console.log("loaded is " + oEvent.loaded);
                }
            }
            function transferComplete(evt) {
                console.log("The transfer is complete.");
            }
            function transferFailed(evt) {
                console.log("An error occurred while transferring the file.");
            }
            function transferCanceled(evt) {
                console.log("The transfer has been canceled by the user.");
            }

            //this will parse XML file and output it to website
            var parseFile = function (text) {
                var xmlDoc = $.parseXML(text),
                    $xml = $(xmlDoc),
                    $email = $xml.find("Email_Address"),
                    $naming = $xml.find("User_Name");
                console.log({ 'xmldoc ': xmlDoc });
                var currentdate = new Date();
                var datetime = currentdate.getDate() + "/" + (currentdate.getMonth() + 1) + "/" + currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
                $("#output").empty();
                $("#output").append("<br/>");
                $("#output").append("<span>Date: " + datetime + "</span><br/>");
                $("#output").append("<span>Name: " + $naming.text() + "</span><br/>");
                $("#output").append("<span>Email: " + $email.text() + "</span><br/>");
            };

    </script>

</head>
<body>
    <div class="row m-sm">
        <span>File name: </span><input id="inFileName" type="text" class="form-control" placeholder="" value="test_xml_file3.xml">
    </div>
    <div class="row m-sm">
        <button id="btnTest3" class="btn btn-outline-secondary" type="button" onclick="Test_Req_xml()">Test xml </button>
    </div>
    <div class="row m-sm">
        <ul id="output"></ul>
    </div>

</body>

</html>

回答1:


I am not entirely sure how it happens but my guess is SharePoint Online is intercepting the get request for files with the XML extension and when it finds the line below it attempts to run some code against the request. I don't see any issues when the file doesn't have an XML extension, nor do I see an issue when the line below is missing from an XML file. Now I need to find out if there is a way around this.

<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>



来源:https://stackoverflow.com/questions/58342845/xmlhttprequest-fails-reading-xml-infopath-form-due-to-mso-application-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!