2

I have seen this question many times and not seen a concrete answer. I have a very simple scenario, I have a list with a folder, i need to add an item inside the folder. I have to do it using REST API. Now the catch is its not a file that i need to add its just a normal default list item with an attachment.

I have searched through the internet and not able to find a solution.

Kindly help!!!

5

The REST API does not have complete support for folders - particularly when it comes to working with list items. While it's quite possible that there is a way to get this to work using the REST API, you're much better off using the Client Object Model in this case.

See: SP2013 : Create a list item inside a folder

  • Hi Rob, thanks for your response. It seems they are insisting on usage of Rest to achieve this. Do you have any sample code which i can try? I am basically stuck with Url, how to form that..? – Sree Kuttan Jun 16 '17 at 0:36
  • No. I do not have any REST API code samples. I only use the Client Object Model when working with lists with folders – Rob Windsor Jun 16 '17 at 10:41
  • I haven't tested his code by Vadim Gremyachev suggested an option that may work in the answer to this question: Add ListItem to List Folder using Rest-Api (stackoverflow.com/questions/22174505/…) – Rob Windsor Jun 16 '17 at 12:09
3

As Rob very correctly mentioned, REST API doesn't have complete support for folders. But using the old /_vti_bin/listdata.svc, you can create a list item and add it inside a folder. For that, you need to set the Path property as server-relative url

Try with below code:

$(document).ready(function () {            

    function createListItem(webUrl,listName, itemProperties, success, failure) {

    $.ajax({
        url: webUrl + "/_vti_bin/listdata.svc/" + listName,
        type: "POST",
        processData: false,
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose"
        },
        success: function (data) {
            success(data.d);
        },
        error: function (data) {
            failure(data.responseJSON.error);
        }
    });
}


//Usage: create list item
var itemProperties = {
    'Title': 'Test item inside folder',
    //server relative path 
    'Path': '/sites/Test/Lists/Test/FolderName' 
};

createListItem('https://tenantname.sharepoint.com/sites/Test/','Test',itemProperties,function(item){
    console.log('TestRESTAPI' + item.Title + ' has been created'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

});

Have tested the above code in SPO and its working fine.

enter image description here

enter image description here

Your second option would be to create a list item normally , then get its FileRef and FileDirRef properties and after that move it inside the folder using the MoveTo command. This will involve 3 REST API calls

Vadim, one of SP.SE's top contributor has implemented it. Check this link - Add ListItem to List Folder using Rest-Api

  • hi @sreekuttan, did you try the above REST api ? – Gautam Sheth Jun 23 '17 at 9:21

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.