4

I am using Java to make calls to the SharePoint REST Interface to get list items of a document library and HTTP methods to upload documents to SharePoint. But, I am having a difficult time trying to figure out how to create a folder programmatically using either REST or HTTP methods. Whenever I try to make a POST with an item data I get the following error

Invalid slug header for BLOBs. Slug headers must be server- or web-relative URL of the file. Server-relative URLs begins with a forward slash (\"/\").

Can anyone please help.

Many thanks in advance.

Dinesh

1
  • No One has any idea about this? Commented Nov 9, 2012 at 0:45

3 Answers 3

4

Get items

To get all items you can either call this url:

http://yourhost/_vti_bin/ListData.svc/YourLib

Or you can call this url:

http://yourhost/_vti_bin/Lists.asmx

with this payload:

<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
  <soap:Body>
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
      <listName>YourLib</listName>
    </GetListItems>
  </soap:Body>
</soap:Envelope>

Create folder

I can't say if this is the correct approach, but it should work.
It uses Batch which I find a bit scary, but I have used this myself with success.
To create a folder you can call this url:

http://yourhost/sites/wf/_vti_bin/Lists.asmx

with this payload:

<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
  <soap:Body>
    <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
      <listName>YourLib</listName>
      <updates>
        <Batch OnError='Continue' PreCalc='TRUE' ListVersion='0' >
          <Method ID='1' Cmd='New'>
            <Field Name='FSObjType'>1</Field>
            <Field Name='BaseName'>YourFolder</Field>
          </Method>
        </Batch>
      </updates>
    </UpdateListItems>
  </soap:Body>
</soap:Envelope>
3
  • Thanks eirikb. It work for me. You have any idea how to create a nested folders? Commented Nov 28, 2012 at 12:04
  • Thanks eirikb and for nested folders I did <Field Name='BaseName'>YourFolder/NestedFolder</Field> Commented Dec 4, 2012 at 22:01
  • Hey. Sorry I haven't had time to look into it. Great that you found a solution for nested folders :) Commented Dec 5, 2012 at 6:02
2

As Tommy pointed out and according to 3.1.4.3 Document:

Inserting new documents to a document library involve sending a POST request containing the contents of the document to the EntitySet representing the document library. The protocol client MUST include the SLUG header (as specified in RFC5023 section 9.7) whose value is the name of the file that is being created in their POST requests.

How to create Folder via REST in SharePoint 2010

function createFolder(webUrl,listName,folderName,folderPath, success, failure) {

    var folderPayload = {
      'ContentType': 'Folder',
      'Title' : folderName,
      'Path' : folderPath
    };

    $.ajax({
        url: webUrl + "/_vti_bin/listdata.svc/" + listName,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(folderPayload),
        headers: {
            "Accept": "application/json;odata=verbose",
            "Slug": folderPath + "/" + folderName + "|0x0120" 
        },
        success: function (data) {
            success(data.d);
        },
        error: function (data) {
            failure(data.responseJSON.error);
        }
    });
}

Examples:

1 Create root folder named Orders in the Documents library

createFolder('https://intranet.contoso.com','Documents','Orders', '/Shared Documents',function(folder){
    console.log('Folder ' + folder.Name + ' has been created succesfully'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
); 

2 Create sub folder named 2014 under the folder Orders in the Documents library

createFolder('https://intranet.contoso.com','Documents','2014', '/Shared Documents/Orders',function(folder){
    console.log('Folder ' + folder.Name + ' has been created succesfully'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
); 
1
  • I've tried same ListData.svc approach on SP 2012 (it supports old interface) and succeed even without folderPayload - just did not send body at all. Unfortunately I have no write-access to SP 2010 sites - it may be different there, but anyway it's quire strange! Commented Dec 30, 2014 at 17:57
1

I don't have high enough reputation to comment, but I already gave the answer to this over here: Folder creation in Document library using listdata.svc

On creating folders in lists, its the same way, but without the "slug" header and you just add "/Lists" to beginning of the "Path" property.

1
  • 1
    I'll flag it for you. Commented Mar 20, 2014 at 15:00

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.