B. Allred
B. Allred

Reputation: 447

Trying to read a file's contents in Sharepoint using Javascript

I have successfully embedded javascipt with jQuery into a page in SharePoint and I am trying to write a function that, when called on load, obtains the data from an excel file that has been uploaded into the SharePoint's library, so that I can manipulate it with JavaScript and push it into a SharePoint List. My issues are coming from that first part. I have attempted to do this with the following code, which was obtained from SharePoint's documentation and modified to use the ".fail" function instead of the deprecated ".error" function:

function readFile() {
    var clientContext;
    var oWebsite;
    var fileUrl;

    clientContext = new SP.ClientContext.get_current();
    oWebsite = clientContext.get_web();

    clientContext.load(oWebsite);
    clientContext.executeQueryAsync(function () {
        fileUrl = "My file's url as obtained from SharePoint";

        $.ajax({
            url: fileUrl,
            type: "GET"
        })
            .done(Function.createDelegate(this, successHandler))
            .fail(Function.createDelegate(this, errorHandler));
    }, errorHandler);

    function successHandler(data) {
        console.log(data);
    }

    function errorHandler() {
        console.log("Request failed: " + arguments[2]);
    }
}

This appears to work, as I receive no errors and the data is indeed displayed in the console. However, instead of the file's contents, I see this:

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name='viewport' content='width=device-width, initial-scale=1' />
	<meta name='robots' content='noindex' />
	<link rel="shortcut icon" href="http://spoos-16-rdn.bankofamerica.com/wv/resources/1033/FavIcon_Word.ico" />
	<title>Document.docx</title>
	
	<script type="text/javascript">
	   var WOPIPerf_UserClick = null;
	   if (window.sessionStorage)
	   {
		   WOPIPerf_UserClick = window.sessionStorage.getItem("WOPIPerf_UserClickTime");
		   window.sessionStorage.removeItem("WOPIPerf_UserClickTime");
	   }
	</script>
	
	<script type="text/javascript">
function ULS6zp(){var o=new Object;o.ULSTeamName="Microsoft SharePoint Foundation";o.ULSFileName="WOPIFrame.aspx";return o;}
		function getWopiIFrameElement()
		{ULS6zp:;
			return document.querySelector("iframe[name=WebApplicationFrame]");
		}
		function WACRedirector()
		{ULS6zp:;
			var myFrame = getWopiIFrameElement();
			myFrame.id = "WebAp

I'm not sure what's going on, but I feel like there's a step missing here. When I enter the url into my browser, it redirects to SharePoint's in-house document editor, where I can indeed see the document's contents and even edit it. Is there something else I need to do to obtain the file's actual content in JavaScript?

Upvotes: 1

Views: 3052

Answers (2)

B. Allred
B. Allred

Reputation: 447

I found the answer: The query url needs to utilize the ExcelRest API built into SharePoint. To do this, I simply had to add "/_vti_bin/ExcelRest.aspx/" to the url in the following fashion:

https://yourDomain/sites/yourSite/yourLibrary/_vti_bin/ExcelRest.aspx/yourFolder/yourFileName.xlsx

Upvotes: 0

Mike Smith - MCT
Mike Smith - MCT

Reputation: 1231

You need to use the URL to the file, not the URL to Office Online's viewers/editors. The direct URL should look something like this:

https://yourDomain/sites/yourSite/yourLibrary/yourFileName.xlsx

https://yourDomain/sites/yourSite/yourLibrary/yourFolder/yourFileName.xlsx

When entered in a browser, this URL should start a download.

Upvotes: 1

Related Questions