Introduction
In this blog we will learn about how to check if a particular folder is present in a Sharepoint list using JSOM.
Code Snippet
- function FolderExists(sitecollectionURL,ListNameTitle,folderName)
- {
- var deferred = $.Deferred();
- var ClientContext = new SP.ClientContext(sitecollectionURL);
- var web = ClientContext.get_web();
- var ListName = web.get_ListNames().getByTitle(ListNameTitle);
- var FolderCamlQuery = "<View Scope='RecursiveAll'><Query><Where><And><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>1</Value></Eq>"+
- "<Eq><FieldRef Name='Title'/><Value Type='Text'>" + folderName + "</Value></Eq></And></Where></Query></View>";
- var query = new SP.CamlQuery();
- query.set_viewXml(FolderCamlQuery);
- allItems = ListName.getItems(query);
- ClientContext.load(allItems);
-
- ClientContext.executeQueryAsync(
- Function.createDelegate(this,
- function () { deferred.resolve(allItems); }),
- Function.createDelegate(this,
- function (sender, args) { deferred.reject(sender, args); }));
- return deferred.promise();
- }
-
-
-
- FolderExists(url,ListNameName,folderName).then(function(FolderFound)
- {
- if(FolderFound.get_count() == 0)
- {
- console.log("No folder found");
-
- }
- else
- {
- console.log("folder found");
-
- }
- });
-
Use the necessary Jquery.js and Jsom imports before using this code.
This code is very straightforward if the condition FolderFound.get_count() >0 folder exists; otherwise the folder is not available.
That's it. Happy Coding :)