Monday, August 6, 2018

Use a JSON file in a SharePoint Library

Scenario
  • You want to utilize a JSON file uploaded by an automated process to a SharePoint Document Library
  • In this case we will use the SiteAssets (Site Assets) document library in an Office 365 SharePoint Online (O365) site
Issue
  • If you use a direct path to the file you get the "Sorry, something went wrong"  "File Not Found." error
    • Ex: https://mycompany.sharepoint.com/sites/MySite/SiteAssets/MyFile.json
  • If you click on the file or get a link to the file you are given a URL to an AllItems display form which renders a file editor, but will not grant access to the JSON via script
    • Ex: https://mycompany.sharepoint.com/sites/MySite/SiteAssets/Forms/AllItems.aspx?id=%2Fsites%2FMySite%2FSiteAssets%2FMyFile%2Ejson&parent=%2Fsites%2FMySite%2FSiteAssets
Resolution
  • Using the Download button and Fiddler (or IE dev tools / network), you can identify the correct URL to access the raw JSON file.
    • Look for the HTTPS GET for download.aspx
  • Alternatively, you can use the following URL format and replace mycompany, MySite, and MyFile text with your own values:
    • https://mycompany.sharepoint.com/sites/MySite/_layouts/15/download.aspx?SourceUrl=%2Fsites%2FMySite%2FSiteAssets%2FMyFile%2Ejson
      • Note: This is for a site at /sites/MySite.  You may need to modify this path.
  • If using JQuery, here is some code to get it:
$(document).ready(function () 
{
$.ajax({
type: "GET",
url: "/sites/MySite/_layouts/15/download.aspx?SourceUrl=%2Fsites%2FMySite%2FSiteAssets%2FMyFile%2Ejson",
success: function(result)
{
            console.log(result[0].property1+ " - " + result[0].property2);
}
});
});

6 comments:

  1. Good blog information by the author
    Sanjary Kids is one of the best play school and preschool in Hyderabad,India. Give your child the best preschool experience by choosing the best playschool of Hyderabad in Abids. we provide programs like Play group,Nursery,Junior KG,Senior KG,and provides Teacher Training Program.
    Preschool in hyderabad

    ReplyDelete
  2. Migration to SharePoint Online or Office365 plays a key role in the organizational modernization as by empowering workforces to enhance productivity.
    Migration to SharePoint Online

    ReplyDelete
  3. This is the first time I frequented your website page and to this point? I surprised with the research you made to create this particular post extraordinary. Well done admin..
    Document Management Software
    Document Management System
    Electronic Document Management Software
    Best Document Management Software

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete