Skip to content

Instantly share code, notes, and snippets.

@vikbehal
Created June 13, 2019 15:09
Show Gist options
  • Save vikbehal/8e336242415b384c078cb63bcc02cced to your computer and use it in GitHub Desktop.
Save vikbehal/8e336242415b384c078cb63bcc02cced to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<input type="button" id="btnGetDocuments" value="Get Documents" />
<ul id="allDocuments"></ul>
<script>
$(function()
{
$("#btnGetDocuments").click(function()
{
$.ajax(
{
url:"https://<your-tenant>.sharepoint.com/sites/<target-site>/_api/web/lists/getbytitle('Documents')/files",
type: "GET",
contentType : "application/json",
//headers : {"accept":"application/json;odata=verbose"},
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"Authorization":"Bearer <generated token>"
},
success: OnSuccess,
error: OnError
}
);
function OnSuccess (result){
r = result.d.results;
for (var i =0; i<r.length; i++){
$("#allDocuments").append ("<li>" + r[i].Name + "</li>");
}
}
// Alert the error message when error occurs
function OnError (err){
alert ('Something went wrong, please check the code');
}
});
});
</script>
<body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment