2

I have a query like this:

camlQuery.set_viewXml('<View Scope="RecursiveAll"><Query><Where>
                       <In><FieldRef Name="FileDirRef" />
                         <Values>
                           <Value Type="Text">/sites/folders/folder1</Value>
                         </Values>
                       </In></Where></Query></View>');`

And I would like to put a condition before that executes this if the path exists. How can I do ?

1

You can look into REST API to check if folder exists. You can call below REST API.

http://<SITE NAME>/_api/Web/GetFolderByServerRelativeUrl('<TARGET FOLDER>')/Folders?$filter=Name eq '<NEW FOLDER NAME>'

In you case the query might look something like this.

http://<SITE NAME>/_api/Web/GetFolderByServerRelativeUrl('folders')/Folders?$filter=Name eq 'folder1'

If folder exists you will gets its details in the response, else you will get empty response.

0

the below code snippet (using JSOM) showing how to determine whether a folder exists, not exist or exist but don't have access:

var ctx = SP.ClientContext.get_current();
var folder = ctx.get_web().getFolderByServerRelativeUrl("/folderurl");
ctx.load(folder, "Exists", "Name");
ctx.executeQueryAsync(
   function() {
      if (folder.get_exists()) {
         // Folder exists 
         console.log(folder.get_name());
      }
      else {
         console.log("Folder hidden for security permissions");
      }
   },
   function(s, args) {
      if (args.get_errorTypeName() === "System.IO.FileNotFoundException") {
         //Folder does not exist
         console.log("Folder does not exist.");
      }
      else {
         // An unexpected error occurred.
         console.log("args.get_message());
      }
   }
);

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.