Create a Folder in Document Library Using Javascript
Here, I demonstrated how to create a floder in a document library using Javascript
Example code for JSOM:
1.Open Default.aspx page.
2.Add a button to create a folder.
<button id="btnFolder" onclick="floderCreation()">Click Here</button>
3.Open App.js file and below code
Example code for JSOM:
1.Open Default.aspx page.
2.Add a button to create a folder.
<button id="btnFolder" onclick="floderCreation()">Click Here</button>
3.Open App.js file and below code
'use strict'; var context = SP.ClientContext.get_current(); var hostweburl; var appweburl; $(document).ready(function () { hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl")); appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")); }); function folderCreation() { var ctx = new SP.ClientContext(appweburl); var appCtxSite = new SP.AppContextSite(ctx, hostweburl); var web = appCtxSite.get_web(); var list = web.get_lists().getByTitle("Documents"); var folderCreation = new SP.ListItemCreationInformation(); folderCreation.set_underlyingObjectType(SP.FileSystemObjectType.floder); folderCreation.set_leafName("Approved"); var folderDoc = list.addItem(folderCreation1); folderDoc.update(); ctx.load(folderDoc); ctx.executeQueryAsync(onFloderCreationSuccess, onCreationFolderFail); } function onFloderCreationSuccess() { alert("Floder created successfully"); } function onCreationFolderFail(sender, args) { alert("Floder created failed" + args.get_message()); } // Function to retrieve a query string value. // For production purposes you may want to use // a library to handle the query string. 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]; } }
Comments
Post a Comment