Update 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 tasks that you can complete programmatically and they include JavaScript code examples that demonstrate 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 the 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 a 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=" updateFolder ()">Click here to updateFolder </button></div>  
  2.      
  3.     <div id="displayDiv"></div>  
  4.    <script type="text/javascript">   
  5.   
  6.    function updateFolder() {  
  7.     var clientContext;  
  8.     var oWebsite;  
  9.     var oList;  
  10.     clientContext = new SP.ClientContext.get_current();  
  11.     oWebsite = clientContext.get_web();  
  12.     oList = oWebsite.get_lists().getByTitle("Shared Documents");  
  13.   
  14.     this.oListItem = oList.getItemById(1);  
  15.     this.oListItem.set_item("FileLeafRef""My updated folder");  
  16.     this.oListItem.update();  
  17.   
  18.     clientContext.load(this.oListItem);  
  19.     clientContext.executeQueryAsync(  
  20.         Function.createDelegate(this, successHandler),  
  21.         Function.createDelegate(this, errorHandler)  
  22.     );  
  23.   
  24.     function successHandler() {  
  25.         displayDiv.innerHTML = "Go to the " +  
  26.             "<a href='../Lists/Shared Documents'>document library</a> " +  
  27.             "to see your updated folder.";  
  28.     }  
  29.   
  30.     function errorHandler() {  
  31.         displayDiv.innerHTML = "Request failed: " + arguments[1].get_message();  
  32.     }  
  33. }  
  34. </script>  

 

Thanks for reading my article.