Introduction
This article explains how to update a folder in a Document Library in SharePoint 2013 using CSOM-JavaScript.
Prerequisites
- Ensure you have access to the Office 365 online.
- Ensure the Napa Tool is available in your site.
Use the following procedure:
- Create an app for SharePoint using Office 365 Tools. If you have missed out on how to create an app in SharePoint 2013, then Click here.
- Create a Docment Library and name it “MyDocumentLibrary”. Refer to my article if you want to learn how to create a Document Library in SP2013.Click Here.
- If you want to learn how to create a folder then Click here.
- Click on the Default.aspx page.
- Add a button to update a folder and place it inside the “PlaceHolderMain” tag as in the following:
<button id="btnUpdateFolder" onclick="updateFolder()">Update Folder</button><br/>
- Click on the App.js file.
- Globally declare the content, web and list objects as shown below.
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var list = web.get_lists(); //gets the collection of lists
var targetList;
var itemCreation;
- Now write the function to create a folder in a Document Library as in the following:
function updateFolder() {
targetList = list.getByTitle("MyDocumentLibrary"); //gets the doc lib object
var itemUpdate = targetList.getItemById(1); //get the item object to update
itemUpdate.set_item("FileLeafRef", "My updated folder");
//update the field.
itemUpdate.update();
context.load(itemUpdate);
context.executeQueryAsync(onFolderUpdateSuccess, onFolderUpdateFail);
}
- Here in this example we are updating a folder.
- In order to update the fields of an item, we need to invoke the set item method using the list item object and then update the item.
- Then we need to load the updated item object and execute the code by calling executeQueryAsync ().
function onFolderUpdateSuccess() {
debugger;
alert("Folder updated");
}
function onFolderUpdateFail(sender, args) {
debugger;
alert('Failed to update the Folder. Error:' + args.get_message());
}
- That's it!! Now let's start testing.
Testing
- Now to run the app click on the "Play" button that is available towards the left-most corner.
- The app is packaged, deployed and installed on your Office 365 Site.
- Now you will be able to see the following page.
- Before clicking on the Update Folder let's navigate to the following URL.
- Now you will be able to see the “MyCustomFolder”. Now let's proceed. Click on the “Update Folder” button.
- Navigate to the Document Library to see the update folder name.
Summary
Thus in this article you saw how to update the folder in a Document Library in SharePoint 2013 using CSOM-JavaScript.