Hi folks,
I have been requested to create lots of document sets in replacement of folders recently. It is easy to create document sets manually, but when you need to create lots of them, the best solution is to do it programmatically.
Document sets are more than simple folders. They hold metadata, which is changing the way users are working and they are taking advantage of it.
As a quick note, in this post I will show how to create document sets programmatically, by exploring the assembly Microsoft.Office.DocumentManagement.dll, available at the directory \14\ISAPI\.
The Solution
Basically this solution shows how to create a document set and add it to a Document Library.
Note: 2 content types were created manually for demonstration purposes: Word Template and Excel Spreadsheet Template.
Check this out:
Code Snippet
- static void Main(string[] args)
- {
- var url = ConfigurationManager.AppSettings["Url"].ToString();
- var sub = ConfigurationManager.AppSettings["Sub"].ToString();
- var library = ConfigurationManager.AppSettings["Library"].ToString();
-
- using (SPSite site = new SPSite(url))
- {
- using (SPWeb web = (sub == "*") ? site.RootWeb : site.OpenWeb(sub))
- {
- string docsetName = "Document Set Test";
- SPContentType ctype = null;
-
- if (web.ContentTypes[docsetName] == null)
- {
- // Creating the document set (content type)
- ctype = new SPContentType(web.ContentTypes["Document Set"], web.ContentTypes, "Document Set Test");
- ctype.FieldLinks.Add(new SPFieldLink(web.Fields["Author"]));
- ctype.Group = "Test Content Types";
- web.ContentTypes.Add(ctype);
-
- // Getting the document set (content type)
- DocumentSetTemplate docsetTemplate = DocumentSetTemplate.GetDocumentSetTemplate(ctype);
-
- // Setting the content types
- docsetTemplate.AllowedContentTypes.Remove(web.ContentTypes["Document"].Id);
- docsetTemplate.AllowedContentTypes.Add(web.ContentTypes["Word Template"].Id);
- docsetTemplate.AllowedContentTypes.Add(web.ContentTypes["Excel Spreadsheet Template"].Id);
-
- // Sharing fields
- docsetTemplate.SharedFields.Add(web.Fields["Author"]);
-
- // Displaying fields
- docsetTemplate.WelcomePageFields.Add(web.Fields["Author"]);
-
- // Adding default document
- FileStream wordFile = File.OpenRead(Path.GetFullPath(@"DocumentSet\Default.dotx"));
- byte[] binWordFile = new byte[wordFile.Length];
- wordFile.Read(binWordFile, 0, binWordFile.Length);
-
- docsetTemplate.DefaultDocuments.Add("Default.dotx", web.ContentTypes["Word Template"].Id, binWordFile);
-
- // Updating the document set (content type),
- docsetTemplate.Update(true);
- ctype.Update();
- }
-
- ctype = ctype jQuery15207137137458194047_1344344305332 web.ContentTypes[docsetName];
-
- if (web.Lists.TryGetList(library) == null)
- {
- // Creating document library
- Guid libraryGuid = web.Lists.Add(library, "", SPListTemplateType.DocumentLibrary);
- SPDocumentLibrary list = (SPDocumentLibrary)web.Lists[libraryGuid];
-
- // Setting properties
- list.OnQuickLaunch = true;
- list.ContentTypesEnabled = true;
- list.EnableFolderCreation = false;
-
- // Defining content types
- list.ContentTypes.Delete(list.ContentTypes["Document"].Id);
- list.ContentTypes.Add(ctype);
- list.Update();
-
- System.Collections.Hashtable properties = new System.Collections.Hashtable();
- properties.Add("DocumentSetDescription", "Just an example"); //InternalName
- properties.Add("_Author", "MM"); //InternalName
-
- // Creating the document set
- DocumentSet.Create(list.RootFolder, "DocSet1", list.ContentTypes.BestMatch(ctype.Id), properties, true);
- }
- }
- }
- }
Note: A Document Library is created dynamically for demonstration purposes.
Download the solution here.
I hope it helps.
References:
http://technet.microsoft.com/en-us/library/ff603637.aspx
http://office.microsoft.com/en-us/sharepoint-server-help/CH010372625.aspx
http://msdn.microsoft.com/en-us/library/ee574540.aspx
Cheers,
Marcel Medina
Click here to read the same content in Portuguese.