Monday, July 29, 2013

Create a DocumentSet(Predefined) using JSOM in SharePoint 2013 (on-premise/on-line).



This post explains how we can create a documentset(predefined) in a folder using JSOM. As in my previous post  you can apply a technical solution which can be remote/local. This solution can be applied for on-premise and as well as SharePoint on-line.

There might be business cases like you have to create a set of documents when you create a project. Assuming this business scenario, I have created a SharePoint List called ProjectMgmt and a document library called ProjectLibrary. I have configured the documentset content type with some predefined documents.

I have created a new form for the ProjectMgmt list and in that I have added the following code. I have commented out the default SharePoint Save button and included an HTML input button and named it as “Save”.

<input type="button" value="Save"  id="CreateDocumentSet" name="bCreateDocumentSet" onclick="javacript:CreateFolder();" />

Under the content place holder - placeholder main I have referenced the following scripts
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/SP.DocumentManagement.js"></script>
<script type="text/javascript" src="/layouts/15/app.js"></script>
<script type="text/javascript"src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

The code below, is to create a folder and then the document set inside the folder. I have left out the JSOM code for creating an item in a SharePoint list which is available here.  I have to create a folder with the project name and prefix the project name for the document set. You could see that I have used the documentset content type id "0x0120D520" to create the document set.


<script type="text/javascript">                
                var clientContext;
                var ProjectName;
                var ProjectID;   
                var folderUrl;
                 
                /*
                Function name :CreateFolder
                Parameters      : context , arguments
    Description      : This function is responsible for the creation of the folder. 
                           On success calls the onQuerySucceededCreateDocumentSet.          
                Returns           : None
                */
                function CreateFolder(sender, args)
                {
                      clientContext = new SP.ClientContext.get_current();
                      oList = clientContext.get_web().get_lists().getByTitle('ProjectLibrary');                                  
                      
                       //Acquire the values entered by the user
                       ProjectID =$("input[title='ProjectID']").val();
                       ProjectName=$("input[title='ProjectName']").val();      

                       var web = clientContext.get_web();
                       var newDocSetName =$("input[title='ProjectName']").val();
                       var list = web.get_lists().getByTitle('ProjectLibrary');
                       clientContext.load(list);
                       itemCreateInfo = new SP.ListItemCreationInformation();
                               
                       //Create the folder
                       itemCreateInfo.set_underlyingObjectType                                                     (SP.FileSystemObjectType.folder);
                       itemCreateInfo.set_leafName(newDocSetName);
                       this.oListItem = list.addItem(itemCreateInfo);
                       this.oListItem.update();                   
                       clientContext.load(this.oListItem);         
                       clientContext.executeQueryAsync(
                                    Function.createDelegate(this,                                                  this.onQuerySucceededCreateDocumentSet),
                                    Function.createDelegate(this,                                          this.onQueryFailedFolderCreation)
    );   
                }

                function onQueryFailedFolderCreation(sender, args)
                {             
                                alert('Folder Creation Failed due to the error: ' + args.get_message());
                }

                /*
                Function name                   : onQuerySucceededCreateDocumentSet
                Parameters                        : context
                Description                        : This function is responsible for the creation of the Document Set
                Returns                              : None
                */
                function onQuerySucceededCreateDocumentSet(param)
                {              var docSetContentTypeID = "0x0120D520";
                                clientContext = new SP.ClientContext.get_current();
                                var web = clientContext.get_web();      
                                

                                folderUrl = “YOUR SITE URL” + “/YOUR DOCUMENT LIBRARY/"
                                                   ProjectName;

                                var folder= web.getFolderByServerRelativeUrl(folderUrl);
                                clientContext.load(folder);
                                var newDocSetName =ProjectName + '-Documentset';
                                var docsetContentType = web.get_contentTypes().getById(docSetContentTypeID);     
                                clientContext.load(docsetContentType);             
               
                                clientContext.executeQueryAsync
                                (             
                                                function ()
                                                {
                                                                //Create the document set.
var isCreated = SP.DocumentSet.DocumentSet.create(clientContext, folder , newDocSetName,                                                               docsetContentType.get_id());  
                                                                clientContext.executeQueryAsync(
                                                               
                                                 function onQuerySucceededDocumentSetCreated()
                                                 {
                                                        $("#overlay").css("display","none");
                                                        alert('DocumentSet has been Created');
                                                 },
                                                               
                                                  function onQueryFailedDocumentSetCreation(sender, args)
                                                  {
                                                          $("#overlay").css("display","none");
                                                          alert('Document Set Creation Failed due to the error: ' +                                                                                           args.get_message());
                                                   }

                               );                                                                                                            
                                                },
                                               

                                                function ()
                                                {
                                                                $("#overlay").css("display","none");
                                                                alert('Folder loading failed');
                                                }

                                );            
                }
                </script>


This code works in SharePoint 2013 on-premises and also on SharePoint 2013 on-line. Happy coding!!!

No comments:

Post a Comment