Creating a folder in SharePoint provides an easy way to manage and structure the documents in a library. It also helps to partition the document library and grant the final permission levels, within the library. Moreover, in Explorer view, the folders will work best in organizing and finding the contents. If the folder creation is enabled for a list, we can create the folders for the list as well.

folder

Change the Radio button, shown above, to Yes to enable the folder creation in SharePoint lists. We can create the folders out of the box as well as via the programming model. As a best practice, it is recommended not to create deep levels of nested folder structures as finding the content will become difficult, which defeats the purpose of the folders.

In this article, we will see, how to work with the folders in SharePoint 2016 and Office 365, using JavaScript object model.

Internal Implementation

Scope of the article is:

All these operations will be done, using JavaScript object model.

Create Folder

Let’s see, how we can create a folder instance in SharePoint programmatically from the client side:

Output: Output from the console and UI is shown below:

Output

Output

Full Code

The full code for the creation of a new folder, which uses JavaScript object model is given below:

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function() {
  4. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createFolder);
  5. });
  6. var oListItem;
  7. function createFolder() {
  8. //Get Client Context , web and list object
  9. var clientContext = new SP.ClientContext();
  10. var oWebsite = clientContext.get_web();
  11. var oList = oWebsite.get_lists().getByTitle('Demo Library');
  12. //Create Folder using SP.FileSystemObjectType.folder type
  13. var oListItemCretionInformation = new SP.ListItemCreationInformation();
  14. oListItemCretionInformation.set_underlyingObjectType(SP.FileSystemObjectType.folder);
  15. oListItemCretionInformation.set_leafName("NewFolder");
  16. oListItem = oList.addItem(oListItemCretionInformation);
  17. oListItem.update();
  18. //Load Client Context and execute the batch
  19. clientContext.load(oListItem);
  20. clientContext.executeQueryAsync(Success, Failure);
  21. }
  22. function Success() {
  23. console.log("Newly Created Folder's Title: " + oListItem.get_item('FileLeafRef'));
  24. }
  25. function Failure(sender, args) {
  26. console.log('Request failed with error message - ' + args.get_message() + ' . Stack Trace - ' + args.get_stackTrace());
  27. }
  28. </script>
Delete Folder

Folder deletion can be done in a similar manner.

Output: Output from the console and UI is given below:

Output

Output

Full Code

The full code to remove the folder object is shown below:

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function() {
  4. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', deleteFolder);
  5. });
  6. var oListItem, collListItem, clientContext;
  7. function deleteFolder() {
  8. //Get the client context,Web and Library object
  9. clientContext = new SP.ClientContext();
  10. oWeb = clientContext.get_web();
  11. var oList = oWeb.get_lists().getByTitle('Demo Library');
  12. //Format the caml query to get the folder object
  13. var camlQuery = new SP.CamlQuery();
  14. camlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>NewFolder</Value></Eq><Eq><FieldRef Name='FSObjType'/><Value Type='Text'>1</Value></Eq></And></Where></Query></View>");
  15. collListItem = oList.getItems(camlQuery)
  16. //Load the client context and execute the batch
  17. clientContext.load(collListItem);
  18. clientContext.executeQueryAsync(Success, Failure);
  19. }
  20. function Success() {
  21. //get the folder collection enumerator and loop through it
  22. var listItemEnumerator = collListItem.getEnumerator();
  23. var itemsToDelete = new Array();
  24. //Get the folder collection
  25. while (listItemEnumerator.moveNext()) {
  26. var oListItem = listItemEnumerator.get_current();
  27. itemsToDelete.push(oListItem);
  28. }
  29. //Delete the folder
  30. for (var i = itemsToDelete.length - 1; i >= 0; i--) {
  31. itemsToDelete[i].deleteObject();
  32. }
  33. //Execute the batch
  34. clientContext.executeQueryAsync(CallSuccess, CallFailure);
  35. }
  36. function Failure(sender, args) {
  37. console.log('Request failed with error message - ' + args.get_message() + ' . Stack Trace - ' + args.get_stackTrace());
  38. }
  39. function CallSuccess() {
  40. console.log("Folder has been Deleted !");
  41. }
  42. function CallFailure(sender, args) {
  43. console.log("Folder Deletion failed with error - " + args.get_message());
  44. }
  45. </script>
Summary

Thus, we have seen, how to work with the folders, create and delete the folder object in SharePoint, using JavaScript object model. This has been tried and tested in both SharePoint 2016 and Office 365.