Document sets can be used when you want to group together the documents within a particular library. Document set by itself is a content type, which acts as a folder at a high level. Apart from the logical grouping of the documents, we can also assign the metadata and assign Workflows to run on a group of documents. In addition to it, we can set the default content to a document set, so that whenever a document set is created, the default contents will be created along with it. This is quite helpful because it gives a starting point to the team. Document set was first introduced in SharePoint 2010 and has made its way all the way until SharePoint 2016.

Document sets are part of a site collection feature.

feature

Let’s try to retrieve all the documents from a document set using JavaScript Object Model.

 JavaScript Object Model.
  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. var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
  5. $.getScript(scriptbase + "SP.Runtime.js", function() {
  6. $.getScript(scriptbase + "SP.js", function() {
  7. $.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
  8. });
  9. });
  10. });
  11. var docSetFiles;
  12. function createDocumentSet() {
  13. //Get the client context,web and library object.
  14. clientContext = new SP.ClientContext.get_current();
  15. oWeb = clientContext.get_web();
  16. var oList = oWeb.get_lists().getByTitle("Demo Library");
  17. clientContext.load(oList);
  18. //Get the root folder of the library
  19. oLibraryFolder = oList.get_rootFolder();
  20. var documentSetFolder = "/sites/Playground/Demo%20Library/Long Term Execution
  21. Planning ";
  22. //Get the document set files using CAML query
  23. var camlQuery = SP.CamlQuery.createAllItemsQuery();
  24. camlQuery.set_folderServerRelativeUrl(documentSetFolder);
  25. docSetFiles = oList.getItems(camlQuery);
  26. //Load the client context and execute the batch
  27. clientContext.load(docSetFiles, 'Include(File)');
  28. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  29. }
  30. function QuerySuccess() {
  31. //Loop through the document set files and get the display name
  32. var docSetFilesdocSetFilesEnumerator = docSetFiles.getEnumerator();
  33. while (docSetFilesEnumerator.moveNext()) {
  34. var oDoc = docSetFilesEnumerator.get_current().get_file();
  35. console.log("Document Name : " + oDoc.get_name());
  36. }
  37. }
  38. function QueryFailure() {
  39. console.log('Request failed - ' + args.get_message());
  40. }
  41. </script>
We can add the above script to a Content Editor Web part and see the output in the Console, as shown below:

summary
Summary

Thus, we saw how to retrieve the documents from a Document Set in SharePoint 2016, using JavaScript Object Model.