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!!!

3 Answers 3

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

August 2020 Update:

In the three years since I posted my original answer, I've discovered that there are ways to add a list item to a folder using the REST API. I wrote a blog post, SharePoint REST API and Lists with Folders, that shows three options to do so.

One of these options, that didn't exist back in 2017, is to use the AddValidateUpdateItemUsingPath method. This is the method recommended by Microsoft in the REST API documentation. See Create list item in a folder.

POST https://{site_url}/_api/web/lists/GetByTitle('Test')/AddValidateUpdateItemUsingPath
Authorization: "Bearer " + accessToken
Accept          "application/json;odata=nometadata"
Content-Type    "application/json;odata=nometadata"
X-RequestDigest "The appropriate digest for current site"

{
  "listItemCreateInfo": {
    "FolderPath": {
      "DecodedUrl": "https://{site_url}/lists/Test/Folder/SubFolder"
    },
    "UnderlyingObjectType": 0
  },
  "formValues": [
    {
      "FieldName": "Title",
      "FieldValue": "Item"
    }
  ],
  "bNewDocumentUpdate": false
}
3
  • 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..? Jun 16, 2017 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 Jun 16, 2017 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/…) Jun 16, 2017 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

1
  • hi @sreekuttan, did you try the above REST api ? Jun 23, 2017 at 9:21
0

You can send a POST request to this endpoint:

https://{site_url}/_api/web/lists/GetByTitle('Test')/AddValidateUpdateItemUsingPath

with these headers:

Authorization: "Bearer " + accessToken
Accept          "application/json;odata=nometadata"
Content-Type    "application/json;odata=nometadata"
X-RequestDigest "The appropriate digest for current site"

and payload:

{
  "listItemCreateInfo": {
    "FolderPath": {
      "DecodedUrl": "https://{site_url}/lists/Test/Folder/SubFolder"
    },
    "UnderlyingObjectType": 0
  },
  "formValues": [
    {
      "FieldName": "Title",
      "FieldValue": "Item"
    }
  ],
  "bNewDocumentUpdate": false
}

Here MS documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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