3

I have a custom page to upload file to document library. For example, if there are 10 person, so inside root folder, I have 10 folder for each person, Named as per the ID of the person. So while uploading, there is a textbox where I enter the ID of that person.

Now when I click on upload button, I need to check if the folder for that person exist in root folder. If it exist, I need to upload it there.

there are many root folders, so I need to check it in all those folders for the folder of that person exist.

function getFolderToUpload()
{

$.ajax({
async: false,
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('EmployeeDocumentList')/folders?$select=Name",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
         $(data.d.results).each(function(){          {
             if(this.Name!="Forms")
             {
             CheckFolderExist();             
             }
             }
    });
},
        error: function (data) {
            alert("error checking folder exist");
        }
    });
}

By the code above, i get all the root folders, Now I need to check if a folder named test exist in any of those folders. So I wanted to ask if there is any lambda expression kind of code that I can write to search this test folder in all those folders.

4 Answers 4

3

This query will let you know if the folder exists or not:

/_api/web/GetFolderByServerRelativeUrl('<FolderName>/<NestedFolderName>')/Exists

The endpoint will return the 'Exists' property with a boolean value. Hope that helps!

1
  • 1
    This is the best answer to check if the folder exists inside a document library or not. Thanks for sharing !!
    – DvG
    Jun 7, 2021 at 10:12
1

You can specify the full path to get the folder, i.e. GetFolderByServerRelativeUrl('EmployeeDocumentList/folder/test') instead of just grabbing the root and looping through. If you don't know where the folder is you could use a CAML query or even search to find it and then get it's URL.

0

/GetFolderByServerRelativeUrl('basepath/folderName')/ListItemAllFields

...if the above returns record, folder exists, otherwise it doesn't.

0

that one worked for me

 let path = "the folder path";
    let url = _spPageContextInfo.webAbsoluteUrl+`/_api/web/GetFolderByServerRelativeUrl('${path}')/exists`;
    $.ajax({
      url,
      method: "GET",
      headers: { "Accept": "application/json; odata=verbose" },
      success: function (data) {
        console.log(data);
        if (data.d.Exists) {
          console.log("Folder exists");
        } else {
          console.log("Folder does not exist");
        }
      },
      error: function (data) {
        console.log(data);
      }
    })

Your Answer

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

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