This article shows how to create a folder in the list/document Library using REST. Develop the project using the following Method in the NAPA Tool:
On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.
- Choose the App for SharePoint template, name the project Create Site and then choose the Create button.
- Replace APP.js with the following source code below.
- Publish Your App.
Prerequisites: The following are important steps to be done before creating the app. Specify the permissions that your app needs as in the following:
Choose the Properties button at the bottom of the page.
- In the Properties window, choose Permissions.
- In the Content category, set Write permissions for the Tenant scope.
- In the Social category, set Read permissions for the User Profiles scope.
- Close the Properties window.
Default ASPX: Add the following div content to the default aspx page in the app.
- <div>
- <p>
- <b>Create Folder</b>
- <br />
- <input type="text" value="List Name Here" id="CreateFolder" />
- <button id="btnclick">Create Folder</button>
- </p>
- </div>
CodeUnderstanding
Lib=SharePoint Library Name
Folder B=NewFolder Name
SourceCode
- 'use strict';
- var hostweburl;
- var appweburl;
-
- $(document).ready(function ()
- {
-
- hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
- appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
-
-
- $("#btnclick").click(function (event) {
- FolderCreation();
- event.preventDefault();
- });
-
-
- $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");
- });
-
- function FolderCreation()
{
- var executor;
- var getfoldername= document.getElementById("CreateFolder").value;
-
- executor = new SP.RequestExecutor(appweburl);
- executor.executeAsync({
- url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('lib')/folders?@target='" + hostweburl + "'",
- method: "POST",
- body: "{ '__metadata':{ 'type': 'SP.Folder' }, 'ServerRelativeUrl':'Folder B' }",
- }
- headers:
- {
- "accept": "application/json; odata=verbose",
- "content-type": "application/json; odata=verbose"
- },
- success: FoldersSuccessHandler,
- error: FoldersErrorHandler
- });
- }
-
- function FoldersSuccessHandler(data) {
- alert("Folder Created successfully in Library");
- }
- function FoldersErrorHandler(data, errorCode, errorMessage) {
- alert("Could not Create a Folder in Library: " + errorMessage);
- }
-
-
-
- function getQueryStringParameter(paramToRetrieve) {
- var params = document.URL.split("?")[1].split("&");
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) return singleParam[1];
- }
- }
Publish
Publish the App and click the Trust it button.
Output
The folder has been created successfully in the Document Library SharePointLibraray.