Retrieve Files From A Document Set in SharePoint 2016 Using Java Script Object Model
In this blog, you will learn how to 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.
Let’s try to retrieve all the documents from a document set using JavaScript Object Model.
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function() {
- var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
- $.getScript(scriptbase + "SP.Runtime.js", function() {
- $.getScript(scriptbase + "SP.js", function() {
- $.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
- });
- });
- });
- var docSetFiles;
-
- function createDocumentSet() {
- //Get the client context,web and library object.
- clientContext = new SP.ClientContext.get_current();
- oWeb = clientContext.get_web();
- var oList = oWeb.get_lists().getByTitle("Demo Library");
- clientContext.load(oList);
- //Get the root folder of the library
- oLibraryFolder = oList.get_rootFolder();
- var documentSetFolder = "/sites/Playground/Demo%20Library/Long Term Execution
- Planning ";
- //Get the document set files using CAML query
- var camlQuery = SP.CamlQuery.createAllItemsQuery();
- camlQuery.set_folderServerRelativeUrl(documentSetFolder);
- docSetFiles = oList.getItems(camlQuery);
- //Load the client context and execute the batch
- clientContext.load(docSetFiles, 'Include(File)');
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- }
-
- function QuerySuccess() {
- //Loop through the document set files and get the display name
- var docSetFilesdocSetFilesEnumerator = docSetFiles.getEnumerator();
- while (docSetFilesEnumerator.moveNext()) {
- var oDoc = docSetFilesEnumerator.get_current().get_file();
- console.log("Document Name : " + oDoc.get_name());
- }
- }
-
- function QueryFailure() {
- console.log('Request failed - ' + args.get_message());
- }
- </script>
We can add the above script to a Content Editor Web part and see the output in the Console, as shown below:
Summary
Thus, we saw how to retrieve the documents from a Document Set in SharePoint 2016, using JavaScript Object Model.