Pages

Search This Blog

Wednesday, January 18, 2012

Creating Document Sets by SharePoint API Code C#


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.
Below code will show you how to create document sets programmatically, 
Add reference of Microsoft.Office.DocumentManagement.dll in your project and use below code, available at the directory \14\ISAPI\.

String LIBRARY_NAME = "Document Set Example";
            String DOCUMENT_SET = "Document Set";

            using (SPSite site = new SPSite("http://br-pc-203"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    string docSetContentTypeName = "My Document Set";
                    SPContentType ctype = null;

                    if (web.ContentTypes[docSetContentTypeName] == null)
                    {
                        // Creating the document set (content type)
                        ctype = new SPContentType(web.ContentTypes[DOCUMENT_SET], web.ContentTypes, docSetContentTypeName);
                        ctype.FieldLinks.Add(new SPFieldLink(web.Fields[SPBuiltInFieldId.Birthday]));
                        ctype.Group = "Custom Content Type";
                        web.ContentTypes.Add(ctype);

                        // Getting the document set (content type)
                        DocumentSetTemplate documentContentType = DocumentSetTemplate.GetDocumentSetTemplate(ctype);

                        // Sharing fields
                        documentContentType.SharedFields.Add(web.Fields[SPBuiltInFieldId.Birthday]);

                        // Displaying fields
                        documentContentType.WelcomePageFields.Add(web.Fields[SPBuiltInFieldId.Birthday]);

                        // Updating the document set (content type),   
                        documentContentType.Update(true);
                        ctype.Update();
                    }

                    ctype = ctype ?? web.ContentTypes[docSetContentTypeName];

                    if (web.Lists.TryGetList(LIBRARY_NAME) == null)
                    {
                        // Creating document library
                        Guid libraryGuid = web.Lists.Add(LIBRARY_NAME, "", 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", "My first Document Set by Code"); //InternalName
                        properties.Add("Birthday", DateTime.Now); //InternalName

                        // Creating the document set
                        DocumentSet.Create(list.RootFolder, "My First Document Set", list.ContentTypes.BestMatch(ctype.Id), properties, true);
                    }
                }
            }


Hope it helps !!!!!

2 comments: