0

I'm developing an application and a part of it involves storing user submitted documents to a document library in SharePoint. Each folder in the document library is identified by the User ID. And within each folder is a list of files the user submits

Right now I'm using jquery and the SharePoint REST API to upload files and create the folders to the document library.

Naturally, if the User ID folder does not exist, I would have to create one, so my question is, how do I check to see if that folder exists?

For example, if my ID# is 111111, I would have to check the document library for a folder of 111111 and if it's not there, create a folder titled 111111.

1

You can use the client object model for this:

try
{
    var folder = rootweb.GetFolderByServerRelativeUrl("/Shared Documents/111111");
}
catch
{
   // Create folder here
}

Or by REST

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents/11111')
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

then you can check the response for success of failure

  • Thanks! And one more question, how would you recommend going about creating a folder if it doesn't exist? Is it possible to do this through jquery? – prawn Oct 4 '13 at 15:03
  • JQuery per se is not capable of creating items, but you can use both rest and client object model for this, give it a google! :) – Robert Lindgren Oct 4 '13 at 15:07
  • If possible can you plese add whole code using REST – SP007 Jul 29 '14 at 17:54
0

I Know this is a old post, but just adding the complete code to check if not present then create from App web to hostweb

try
{
    var folder = rootweb.GetFolderByServerRelativeUrl("Shared Documents/NewFolder");
}
catch
{
createFolder('NewFolder');
}
function CreateFolder(newFolderName) {
    $.ajax({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('Shared%20Documents/')/folders/add(url=\'" + newFolderName + "\')?@target='" + hostweburl + "'",
        type: 'POST',
        contentType: 'application/json;odata=verbose',
        headers: {
            'Accept': 'application/json;odata=verbose',
            'content-type': 'application/json;odata=verbose',
            'X-RequestDigest': $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            alert('Created Folder');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //handle error... 
            alert('Failed to create folder');
        }
    });
}

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.