7

I'm trying to create a Folder in a SharePoint Image library. I found an answer here but I can't seem to get it to work

var web = 'my relative path to the current web';
var lib = 'name of the image library';
var foldername = 'jasonscript';

var postData = JSON.stringify({ 
    '__metadata'        : { 'type': 'SP.Folder' }, 
    'ServerRelativeUrl' : lib + '/' + foldername 
});

var pCreate = jQuery.ajax({
    'url'        : web + '/_api/Web/Folders',
    'type'       : 'POST',
    'data'       : postData,
    'headers'    : { 
                    'accept'          : 'application/json; odata=verbose', 
                    'content-type'    : 'application/json; odata=verbose',
                    'X-RequestDigest' : $('#__REQUESTDIGEST').val()
                   }
}); 

jQuery.when(pCreate).always(function (data){
    console.log(data);
});

This returns the following error:

{
    "error":{
        "code":"-2130247139, Microsoft.SharePoint.SPException",
        "message":{
            "lang":"en-US",
            "value":"(null) \"██████/██████/██████\" not found."
        }
    }
}

where the redacted values are the web, lib and foldername values.

I know the folder doesn't exist. I'm trying to create it!

Update

I'm trying to run this code in a Content Editor Web Part (i.e. Not an app)

Any ideas?

2 Answers 2

11

What about using the Add method:

If for example using SiteCollectionDocuments in a Site Collection

'url' : _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Folders/add('SiteCollectionDocuments/newfolder')"

This shouldn't require the data payload 'postData'.

See more here: http://msdn.microsoft.com/en-us/library/office/dn450841(v=office.15).aspx#bk_FolderCollectionAdd


Complete code used:

var pCreate = jQuery.ajax({
"url"        : _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Folders/add('SiteCollectionDocuments/newfolder')",
"type"       : "POST",
"headers"    : { 
                "accept"          : "application/json; odata=verbose", 
                "content-type"    : "application/json; odata=verbose",
                "X-RequestDigest" : $("#__REQUESTDIGEST").val()
               }
}); 

jQuery.when(pCreate).always(function (data){
   console.log(data);
});
7
  • Is this function only for SharePoint apps? I'm just running JavaScript within a Content Editor web part and it doesn't seem to work Jul 30, 2014 at 5:34
  • It works. You should be able to use in a console command on the page not restricted to Apps. But the path was incorrect, needed to remove leading /. Have edited post.
    – z.rahui
    Jul 30, 2014 at 7:42
  • 2
    This works -- there is no siteAbsoluteUrl property in _spPageContextInfo for 2010, but there is in 2013. Just ensure the folder URL parameter is correct as in Vadim's answer.
    – John-M
    Jul 30, 2014 at 12:59
  • Good to know RE: 2010 property!
    – z.rahui
    Jul 31, 2014 at 0:18
  • 1
    It's strange. The Add method will only accept values wrapped in single quotes. Wrapping them in double quotes (as I had tried originally) doesn't work Aug 1, 2014 at 2:54
6

Make sure the correct ServerRelativeUrl has been provided, the following formats could be specified:

  • Server-relative URL: /<web url>/<list url>/<folder url>

  • Site-relative URL: <list url>/<folder url>

For example, to create a folder named Orders in a Documents library in a sub site Projects, you could specify:

'ServerRelativeUrl' : 'Shared Documents/Orders'

or

'ServerRelativeUrl' : '/project/Shared Documents/Orders'
2
  • 1
    This answer was the correct one. My image library was called "MyImages" but I had renamed it to "My Images" (with a space). Once I corrected this, it started working Jul 30, 2014 at 7:38
  • 1
    This is something I glossed over in my answer that was linked to in the original question -- I've added a bit about it and linked here as well because I think it makes the answer I gave in the other thread more complete!
    – John-M
    Jul 30, 2014 at 13:07

Your Answer

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

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