0

I am using this link for reference to fetch data from document library using Rest api.

The issue I am facing is I am getting junk data in result. Below is my code sample:

<button type='button' onclick="retriveItems()">Copy document</button>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type='text/javascript'>

    function retriveItems() {
    alert("Hello World");

    var requesturi= "";
    if((_spPageContextInfo.webAbsoluteUrl) && (_spPageContextInfo.webAbsoluteUrl.indexOf("sites")>0))
    {
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('"+_spPageContextInfo.webServerRelativeUrl+"/Shared%20Documents/teghjhs.docx')/$value";
    }
    else
    {
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('/Shared%20Documents/teghjhs.docx')/$value";
    }
    console.log(requesturi);
    $.ajax  
        ({  
            url: requesturi,
            type: "GET",   
            headers:  
            {  
                "accept": "application/json;odata=verbose",  
                "content-type": "application/json;odata=verbose",
            },  
            cache: false,  
            success: function(data)   
            {  
                console.log('success');
                console.log(data.d.results[0].Title);                 
            },  
            error: function(data)  
            {  
                console.log('error occured');  
            }  
        });
}   
</script>

Below is output screenshot when I hover on data or add it in console:

enter image description here

enter image description here

Can someone suggest me how can I get data in correct json output?

4

1 Answer 1

0

There is nothing wrong with your code.

It's the format of the document you are trying to read (i.e. docx).

SharePoint doesn't provide API to read the word file content. If you will try to read any other file with .txt format from your document library, it will read in proper format.

Refer this thread for more information :

https://social.msdn.microsoft.com/Forums/office/en-US/f0274065-7d7b-4da6-87c5-cb3c50a38bfb/read-docx-file-with-rest-javascript?forum=sharepointdevelopment

1
  • your provided link's accepted answer has two links. One redirect me to same stack overflow link I go through. The other uses word interop. I can't use word interop with SharePoint Online. Commented Nov 11, 2017 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.