Advertisement

Responsive Advertisement

SharePoint Hosted App in office 365 to create a Document Set in SharePoint 2013 using JSOM


In this article I would like to explain about how to create aDocument in SharePoint 2013 programmatically using javascript object model.
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.
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 publisheButton.
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:
'use strict';
var hostweburl;
var appweburl;
var docSetContentTypeID ="0x0120D520";


(function () {


   // This code runs when the DOM is ready and creates a context object which is
   // needed to use the SharePoint object model
   $(document).ready(function () {
      //Get the URI decoded URLs.


   hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));


   appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
   });
})();


function getQueryStringParameter(paramToRetrieve) {


   var params = document.URL.split("?")[1].split("&");


   for (var i = 0; i < params.length; i = i + 1) {


       var singleParam = params[i].split("=");


       if (singleParam[0] == paramToRetrieve) return singleParam[1];


   }


}


function createdocumentset(){


var ctx = new SP.ClientContext(hostweburl);
var parentFolder;
var newDocSetName = "Test"
var web = ctx.get_web();
var list = web.get_lists().getByTitle('Documents');
ctx.load(list);


parentFolder = list.get_rootFolder();
ctx.load(parentFolder);


var docSetContentType =ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);
ctx.load(docSetContentType);


 ctx.executeQueryAsync(function (){
       SP.DocumentSet.DocumentSet.create(ctx, parentFolder, newDocSetName, docSetContentType.get_id());
       ctx.executeQueryAsync(success,error);
   },
   error);
}


function error(message) {
   return function (sender, args) {
       alert(message + ": " + args.get_message());
   }
}
function success(message) {
   return function () {
       alert(message);
   }
}

Post a Comment

0 Comments