1

I was successfully able to create a folder in SharePoint 2013 document library using REST API in C# with below code:

<sharepoint site URL>/_api/Web/Folders/add('/DocLibName/FolderName')

I also added custom metadata columns Code, Department, Location but I was not able to add values on folder level. However, when I upload a file using REST API, the metadatas are successfully updated.

How can I add metadatas at folder level?

  • 1
    check this link – Gautam Sheth Jan 23 '18 at 11:49
  • this is for renaming folder. I tried sending metadata(custom columns) but getting BAD request error. – Vishwanath Mishra Jan 23 '18 at 12:59
  • The code for renaming the folder is the same as setting values for custom columns. If you're getting an error, you should indicate what the error details are (it should be more than "bad request"). – wjervis Jan 24 '18 at 13:38
2

Finally got it to work using REST API. But it involves additional REST call.

Below is the code to Create a folder, from that code we will call the get Folder method to get the folder's ID:

function CreateFolder(){

        jQuery.ajax({
            "url" : _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Folders/add('/sites/testsite/Shared Documents/newfolderRest16')",
            "type" : "POST",
            "headers" : { 
                    "accept" : "application/json; odata=verbose", 
                    "content-type" : "application/json; odata=verbose",
                    "X-RequestDigest" : $("#__REQUESTDIGEST").val()
            },
            success: function(data){
                console.log(data);
                GetFolder(data);
            },
            error: function(data){
                console.log(data);
            }
        }); 

    }

Now, getFolder method will get the folder's ID which we will then pass to the UpdateFolder method to update the folder:

function GetFolder(data){
    var url = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('/sites/testsite/Shared Documents/newfolderRest16')?$expand=ListItemAllFields";

    $.getJSON(url).then(function(data){
         console.log(data.ListItemAllFields.ID);
         UpdateFolder(data.ListItemAllFields.ID);
    });
}

function UpdateFolder(id){  
    $.ajax ({  
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('Documents')/items("+ id +")",
        type: "POST",  
        data: JSON.stringify({  
            "__metadata":  
            {  
                type: "SP.Data.Shared_x0020_DocumentsItem"  
            },  
            "Department": "HR",
            "Location": "India"
        }),  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "IF-MATCH": "*",  
            "X-HTTP-Method": "MERGE"  
        },  
        success: function(data, status, xhr)  
        {  
            console.log("success");
        },  
        error: function(xhr, status, error)  
        {  
            console.log(error);
        }  
    });
}

here, in the update folder method, please ensure that you are using the correct ListItemEntityTypeFullName or else it will throw bad request error.

To get the correct ListItemEntityTypeFullName, make a REST call to the below endpoint in the browser.:

https://sitecollectionurl/_api/web/lists/getbytitle('Documents')?$select=ListItemEntityTypeFullName

0

Get folder using item ID:[To every folder an id is assigned] and then update item properties.

        var insertItems = [[]];
        var ItemId='1'      
        insertItems[0] = ["Department","DeptA"];
        insertItems[1]=["Code","Code1"];
        try 
        {
            var clientContext = SP.ClientContext.get_current();
            var oList = clientContext.get_web().get_lists().getByTitle('listname');
            var item;
            if (ItemId!='')
            {
                item = oList.getItemById(ItemId);
            for (var i = 0; i < insertItems.length; i++) 
            {
                if (insertItems[i][1] != undefined && insertItems[i][1] != null)
                                item.set_item(insertItems[i][0], insertItems[i][1]);
            }

            item.update();
            clientContext.load(item);
            clientContext.executeQueryAsync(function () {
                console.log('item updated');
            }, function (sender, args) {
                console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            });
            }
        }
        catch (e) {
            console.log('Request failed. ' + e.message);
        }
  • could you please describe little more code as I am new in sharepoint – Vishwanath Mishra Jan 24 '18 at 11:37
  • updated the original post. Please check. I have tried it at my end and its working as expected. – Deepmala Jan 24 '18 at 11:57
  • as I mentioned, I am using REST api, I cant use CSOM – Vishwanath Mishra Jan 24 '18 at 13:11
-1

To my knowledge Metadata for Folders is not supported in SharePoint, whether accessed via the API or UI. It's generally recommended to use separate libraries instead of folders.

  • using UI we can edit metadata by clicking 'QuickEdit' from ribbon menu – Vishwanath Mishra Jan 23 '18 at 11:30
  • Apologies. It seems this functionality is available through the UI – J. Doe Jan 23 '18 at 11:42
  • Folder property also added through JSOM – Jinesh Kaneriya May 12 '18 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.