2

I have a GenericList that accepts folders as elements.

I know how to create those folders in Sharepoint's interface. I'd like to be able to generate them with a script, using the REST API.

I know how to create elements for a list using the REST API. Those elements have a 'folder' field, which is null for regular elements, or defer to the corresponding SP.Folder otherwise.

Basically, what kind of queries should I make so that the URL http://sharepoint/_api/Web/Lists(guid'aa82af4c-f6cc-47fb-8fb3-adef934d9183')/Items(29)/Folder stops answering me with 'null' ?

2

Look into Vadim's answer here: Is it possible to create a Document Set using REST API

Instead of using (which is at the bottom of the first code snippet):

createFolder(webUrl,listName,folderName,'0x0120D520', success, error);

Use:

createFolder(webUrl,listName,folderName,'0x0120', success, error); 
  • Thanks. It has a few issues though: it doesn't use the /api thingy, so... the fields actually use the language of the Sharepoint server (things like Path become CheminDacces for me... kinda sucks). – user3387633 Apr 7 '15 at 9:45
4

According to Working with folders and files with REST the following example demonstrates how to create a Folder:

url: http://site url/_api/web/folders
method: POST
body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/document library relative url/folder name'}
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

JavaScript example (jQuery)

function createFolder(webUrl,folderUrl) {

    var folderPayload = { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': folderUrl}; 

    return $.ajax({
        url: webUrl + "/_api/web/folders",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(folderPayload),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
    });
}

Usage

//create a folder named '2015' in Documents library
createFolder(_spPageContextInfo.webAbsoluteUrl,'Documents/2015')
.done(function(data)
{
    var folder = data.d;
    console.log('Folder ' + folder.Name  + ' has been created successfully');
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

JavaScript example (datajs)

The same example using datajs library:

var folderPayload = { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Documents/2016'}; 
OData.request(
   { 
       requestUri: _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders", 
       method: "POST", 
       headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(), "DataServiceVersion": "3.0" },
       data: folderPayload 
   },
   function(folder)
   {
      console.log('Folder ' + folder.Name  + ' has been created successfully');    
   },
   function(error){
      console.log(JSON.stringify(error));
   }); 
  • My request is a bit different though. Just creating the folder isn't enough in the case of list sub-folders: the folders are created, but they don't show as a list entry. From @Jordan's answer, it's possible to create such sub-folders, but by using the /vti_bin interface instead of the /api interface (which brings in other problems, because the /vti_bin interface, for some reason, use translatable strings as field names... so you can't code a script that would work in all sharepoint installations). – user3387633 Apr 10 '15 at 12:27
  • is it possible to create an item within a folder in the list using REST API? – userAZLogicApps Feb 16 '16 at 10:39
0

I found a tricky way to get the folder of listitem. The FieldValueAsText node contains the FileRef field. The FileRef for a listitem is a odd string in the format /<sitecollection>/<web>/Lists/<listname>/<folder>/<id>_.000.

To get the FileRef you have to expand the FieldValuesAsText. A select is strongly recommended. Otherwise you get all fields a second time in the FieldValuesAsText object.

_api/lists/getbyTitle('Title')/items?$expand=FieldValuesAsText&$select=Id,FieldValuesAsText/FileRef`

To get the folder you cant now use a little ugly string manipulation:

function getFolder(fileRef, listName) {
        // fileRef is in the format "/<sitecollection>/<web>/Lists/Infos/Bereiche/<id>_.000"
        var folderUrl = fileRef.substring((_spPageContextInfo.webServerRelativeUrl + "/Lists/" + listName + "/").length);
        var folderParts = folderUrl.split("/");
        folderParts.pop(); // Remove the <id>_.000 part
        return folderParts;
}

This function does not work for folders itself. Folders have an FileRef of /<sitecollection>/<web>/Lists/<listname>/<folder> (without the id part).

To exclude Folders from your result you have to add $filter=FSObjType eq 0. (Thats another odd thing with folders in REST, in the result this is called FileSystemObjectType, but if you filter it is FSObjType)

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.