Update a Folder in Document Library in SharePoint 2013 Using CSOM-JavaScript

Introduction

This article explains how to update a folder in a Document Library in SharePoint 2013 using CSOM-JavaScript.

Prerequisites

  1. Ensure you have access to the Office 365 online.
  2. Ensure the Napa Tool is available in your site.

Use the following procedure:

  1. 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.
  2. 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.
  3. If you want to learn how to create a folder then Click here.
  4. Click on the Default.aspx page.

    aspx page

  5. 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/>
     
  6. Click on the App.js file.

    js file

  7. 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;

     

  8. 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);

        }


  9. Here in this example we are updating a folder.
  10. 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.
  11. 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());
    }
     
  12. That's it!! Now let's start testing.

Testing

  1. Now to run the app click on the "Play" button that is available towards the left-most corner.

    Play button

  2. The app is packaged, deployed and installed on your Office 365 Site.

    app

  3. Now you will be able to see the following page.

    page

  4. Before clicking on the Update Folder let's navigate to the following URL.

    Update Folder

  5. Now you will be able to see the “MyCustomFolder”. Now let's proceed. Click on the “Update Folder” button.
  6. Navigate to the Document Library to see the update folder name.

    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.