Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
    
No One has any idea about this? –  Dinesh Reddy Parne Nov 9 '12 at 0:45

3 Answers 3

up vote 1 down vote accepted

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>
share|improve this answer
    
Thanks eirikb. It work for me. You have any idea how to create a nested folders? –  Labeeb P Nov 28 '12 at 12:04
    
Thanks eirikb and for nested folders I did <Field Name='BaseName'>YourFolder/NestedFolder</Field> –  Dinesh Reddy Parne Dec 4 '12 at 22:01
    
Hey. Sorry I haven't had time to look into it. Great that you found a solution for nested folders :) –  eirikb Dec 5 '12 at 6:02

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));
  }
); 
share|improve this answer

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.

share|improve this answer
1  
I'll flag it for you. –  Robert Kaucher Mar 20 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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