Introduction
In this blog, I would like to explain how to create Document Set in Document Library, using CSOM. A Document Set is a group of related documents, which can be created in one step and then managed as a single entity.
Activate Document Set Feature
We need to activate it before you can create or configure new Document Set content types.
- Go to the top-level site in the site collection for which you want to enable Document Sets.
- Site Actions menu--> click Site Settings.
- Site Collection Administration - Site collection features.
- Find Document Sets in the list and then click Activate.
Add Document Set as a Content Type to your Document Library
- Go to Document Settings and change the Advanced Settings to Allow management of content types.
- Go to the Settings- add from existing site content types and find the Document Set content type. Select Document Set and click Add.
- Go to your Document library and select New Document -> Document Set from the drop-down list.
Here, we will see how to create a Document Set programmatically, using csom.
Source Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using Microsoft.SharePoint.Client.DocumentSet;
- namespace DocumentSet
- {
- class Program {
- static void Main(string[] args) {
- string siteURL = "https://gowtham.sharepoint.com";
- ClientContext clientContext = new ClientContext(siteURL);
- List list = clientContext.Web.Lists.GetByTitle("TestLibrary1");
- clientContext(list.RootFolder);
- clientContext.ExecuteQuery();
- var itemInfo = new ListItemCreationInformation();
- itemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
- itemInfo.LeafName = docSetName;
- itemInfo.FolderUrl = list.RootFolder.ServerRelativeUrl;
- var item = list.AddItem(itemInfo);
- item["ContentTypeId"] = "0x0120D520";
- item["HTML_x0020_File_x0020_Type"] = "SharePoint.DocumentSet";
- item.Update();
- clientContext.ExecuteQuery();
- }
- }
- }
Copy the code, shown above, paste it in Visual Studio and run. Document Set created successfully.