Retrieve Files From A Document Set in SharePoint 2016 Using Java Script Object Model

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.   
  13.     function createDocumentSet() {  
  14.         //Get the client context,web and library object.   
  15.         clientContext = new SP.ClientContext.get_current();  
  16.         oWeb = clientContext.get_web();  
  17.         var oList = oWeb.get_lists().getByTitle("Demo Library");  
  18.         clientContext.load(oList);  
  19.         //Get the root folder of the library   
  20.         oLibraryFolder = oList.get_rootFolder();  
  21.         var documentSetFolder = "/sites/Playground/Demo%20Library/Long Term Execution   
  22.         Planning ";   
  23.             //Get the document set files using CAML query   
  24.         var camlQuery = SP.CamlQuery.createAllItemsQuery();  
  25.         camlQuery.set_folderServerRelativeUrl(documentSetFolder);  
  26.         docSetFiles = oList.getItems(camlQuery);  
  27.         //Load the client context and execute the batch   
  28.         clientContext.load(docSetFiles, 'Include(File)');  
  29.         clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
  30.     }  
  31.   
  32.     function QuerySuccess() {  
  33.         //Loop through the document set files and get the display name   
  34.         var docSetFilesdocSetFilesEnumerator = docSetFiles.getEnumerator();  
  35.         while (docSetFilesEnumerator.moveNext()) {  
  36.             var oDoc = docSetFilesEnumerator.get_current().get_file();  
  37.             console.log("Document Name : " + oDoc.get_name());  
  38.         }  
  39.     }  
  40.   
  41.     function QueryFailure() {  
  42.         console.log('Request failed - ' + args.get_message());  
  43.     }  
  44. </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.