Create a Folder in SharePoint 2013 Using JavaScript (JSOM)

You can use the SharePoint client object model to retrieve, update and manage data in SharePoint 2013. SharePoint makes the object model available in several forms as in the following:

  • .NET Framework redistributable assemblies
  • JavaScript library
  • REST/OData endpoints
  • Windows Phone assemblies
  • Silverlight redistributable assemblies

This article shows how to perform basic operations using the JavaScript object model. You can add a reference to the object model using HTML <script> tags.

The following sections describe the tasks that you can complete programmatically and they include JavaScript code examples that show the operations.

Procedure

Open your SP Site in SharePoint 2013 Designer. Then select an Assets icon from the designer ribbon to add a JavaScript file.



After adding a JavaScript file, the file is available in the SharePoint Site Assets folder as in the following:



Go to the SP site page and add a new page to the SharePoint site.



After adding the page select an Insert button in the Ribbon Menu.



Then add a Content Editor Web part into the page.



Edit the WebPart and add a JavaScript file link to the content link .



Save the web part and save the page in site. Click the button!



List Created Successfully.

Source Code

 

  1. <div><button onclick=" createFolder ()">Click here to AddaFieldtoList</button></div>  
  2.      
  3.   <div id="displayDiv"></div>  
  4.   <script type="text/javascript">   
  5.   function createFolder() {  
  6.     var clientContext;  
  7.     var oWebsite;  
  8.     var oList;  
  9.     var itemCreateInfo;  
  10.     var resultpanel=”https://gauti.sharepoint.com”;  
  11.   
  12.     clientContext = new SP.ClientContext.get_current();  
  13.     oWebsite = clientContext.get_web();  
  14.     oList = oWebsite.get_lists().getByTitle("Shared Documents");  
  15.   
  16.     itemCreateInfo = new SP.ListItemCreationInformation();  
  17.     itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);  
  18.     itemCreateInfo.set_leafName("My new folder!");  
  19.     this.oListItem = oList.addItem(itemCreateInfo);  
  20.     this.oListItem.update();  
  21.   
  22.     clientContext.load(this.oListItem);  
  23.     clientContext.executeQueryAsync(  
  24.         Function.createDelegate(this, successHandler),  
  25.         Function.createDelegate(this, errorHandler)  
  26.     );  
  27.   
  28.     function successHandler() {  
  29.         resultpanel.innerHTML = "Go to the " +  
  30.             "<a href='../Lists/Shared Documents'>document library</a> " +  
  31.             "to see your new folder.";  
  32.     }  
  33.   
  34.     function errorHandler() {  
  35.         resultpanel.innerHTML =  
  36.             "Request failed: " + arguments[1].get_message();  
  37.     }  
  38. }  
  39. </script>  

 

Thanks for reading my article.