SharePoint Hosted App In Office 365 To Create A Document Set In SharePoint 2013 Using JSOM

Document Set is a hybrid between a folder and a list item. Document Sets are a feature in SharePoint Server 2013 that enables an organization to manage a single deliverable, or work product, which can include multiple documents or files. A Document Set is a special kind of folder that combines unique Document Set attributes, the attributes and behavior of folders and documents, and provides a user interface (UI), metadata, and object model elements to help manage all aspects of the work product.

More details.

Before creating Document Set we need to activate the Document Set feature in site collection level. I already explained how to work on this document in my article.

There is no limit on the number of documents that can exist in a Document Set. However, display load times may be limited by the list view threshold which by default is set at 5,000 items.

Create Simple Hosted app in office 365 ,follow the below steps to configure this.

Click the NAPA Tool in SharePoint developer site and choose SharePoint 2013 Template,



Then click the save button. This the default .aspx page in the app.



Check this default App.js page in that ADD-IN.





Copy the attached code and paste it in the app.js file.

Then select the General tab like below,



Choose permissions on the table .And give full access to the list.



After saved the App Package. Click publish Button.



After you published the App it will be available in App packages place.



Click Deploy App button to deploy this,





Click Trust it button to accept this app,





Click Create Document Set button and check this library.Documetn set has been created.



Code:

  1. 'use strict';  
  2. varhostweburl;  
  3. varappweburl;  
  4. vardocSetContentTypeID = "0x0120D520";  
  5. (function()  
  6. {  
  7.     // This code runs when the DOM is ready and creates a context object which is   
  8.     // needed to use the SharePoint object model  
  9.     $(document).ready(function()  
  10.     {  
  11.         //Get the URI decoded URLs.  
  12.         hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  13.         appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  14.     });  
  15. })();  
  16. functiongetQueryStringParameter(paramToRetrieve)  
  17. {  
  18.     varparams = document.URL.split("?")[1].split("&");  
  19.     for (var i = 0; i < params.length; i = i + 1)  
  20.     {  
  21.         varsingleParam = params[i].split("=");  
  22.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  23.     }  
  24. }  
  25. functioncreatedocumentset()  
  26. {  
  27.     varctx = new SP.ClientContext(hostweburl);  
  28.     varparentFolder;  
  29.     varnewDocSetName = "Test"  
  30.     var web = ctx.get_web();  
  31.     var list = web.get_lists().getByTitle('Documents');  
  32.     ctx.load(list);  
  33.     parentFolder = list.get_rootFolder();  
  34.     ctx.load(parentFolder);  
  35.     vardocSetContentType = ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);  
  36.     ctx.load(docSetContentType);  
  37.     ctx.executeQueryAsync(function()  
  38.         {  
  39.             SP.DocumentSet.DocumentSet.create(ctx, parentFolder, newDocSetName, docSetContentType.get_id());  
  40.             ctx.executeQueryAsync(success, error);  
  41.         },  
  42.         error);  
  43. }  
  44.   
  45. function error(message)  
  46. {  
  47.     return function(sender, args)  
  48.     {  
  49.         alert(message + ": " + args.get_message());  
  50.     }  
  51. }  
  52.   
  53. function success(message)  
  54. {  
  55.     return function()  
  56.     {  
  57.         alert(message);  
  58.     }  
  59. }  

Up Next
    Ebook Download
    View all
    Learn
    View all