Office365 Basic Operations Using REST API In SharePoint Online 2013-Create File

REST API supports only the format given below, so please be cautious before executing your code.

Create a file and add it to a folder.

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/newFolder')/Files
method: GET
Headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
content-length:length of post body

Source code

Copy the code given below and paste in your SharePoint ADD-IN.

Create a file into the folder in SharePoint O365, using REST API. 
  1. var hostweburl;  
  2. var appweburl;  
  3.   
  4.   
  5. $(document).ready(function () {  
  6.     
  7.   hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  8.   appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  9.   
  10.   $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);  
  11. });  
  12.   
  13.   
  14. function runCrossDomainRequest() {  
  15.   var executor = new SP.RequestExecutor(appweburl);   
  16.   executor.executeAsync({  
  17.     url: appweburl +"_api/web/GetFolderByServerRelativeUrl('/CustomFolder')/Files/add(url='Test.txt',overwrite=true)"  
  18.         method: POST  
  19.         body: "Contents of file"  
  20.         Headers:   
  21.             Authorization: "Bearer " + accessToken  
  22.             X-RequestDigest: form digest value  
  23.             content-length:length of post body  
  24.               success: successHandler  
  25.               error: errorHandler   
  26.   });  
  27. }  
  28.   
  29.   
  30. function getQueryStringParameter(paramToRetrieve) {  
  31.   var params = document.URL.split("?")[1].split("&");  
  32.   var strParams = "";  
  33.   for (var i = 0; i < params.length; i = i + 1) {  
  34.     var singleParam = params[i].split("=");  
  35.     if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  36.   }  
  37. }   
Was my blog helpful? Post your feedback here. Thanks for reading my blog.
Ebook Download
View all
Learn
View all