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.
- var hostweburl;
- var appweburl;
-
-
- $(document).ready(function () {
-
- hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
- appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
-
- $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);
- });
-
-
- function runCrossDomainRequest() {
- var executor = new SP.RequestExecutor(appweburl);
- executor.executeAsync({
- url: appweburl +"_api/web/GetFolderByServerRelativeUrl('/CustomFolder')/Files/add(url='Test.txt',overwrite=true)"
- method: POST
- body: "Contents of file"
- Headers:
- Authorization: "Bearer " + accessToken
- X-RequestDigest: form digest value
- content-length:length of post body
- success: successHandler
- error: errorHandler
- });
- }
-
-
- function getQueryStringParameter(paramToRetrieve) {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) return singleParam[1];
- }
- }
Was my blog helpful? Post your feedback here. Thanks for reading my blog.