sharepoint 2010 document sets

Document sets are a new concept in SharePoint 2010. They allow grouping of multiple documents into single entity. In this post I will provide helper functions which will show how to work with document sets programmatically. 


 /// <summary>
 /// Returns a document set with the specified name if it exists, 
 /// otherwise creates it.
 /// </summary>
 /// <param name="folderToInsertIn">Folder inside the document library.</param>
 /// <param name="documentSetName">Name of the document set.</param>
 /// <param name="dsCtId">Document set content type associated with 
 /// the library.</param>
 /// <param name="properties">Metadata of the documemnt set.</param>
 /// <returns></returns>
        public static DocumentSet EnsureDocumentSet(SPFolder folderToInsertIn, string documentSetName, SPContentTypeId dsCtId, Hashtable properties)
        {
            Guid listGuid = folderToInsertIn.ParentListId;
            SPList list = folderToInsertIn.ParentWeb.Lists[listGuid];
            DocumentSet ds = null;
            ds = GetDocumentSet(list, documentSetName);
            if (ds == null)
            {
                try
                {
                    ds = DocumentSet.Create(folderToInsertIn, documentSetName, dsCtId, properties, true);

//If the document set is created using "SPWeb.AvailableContentTypes", 
//the document set created shows folder icon and also 
//lacks the associated document set page.

                    if (ds.Folder.Item.ProgId == string.Empty)
                    {
                        ds.Folder.Item.ProgId = "SharePoint.DocumentSet";
                        ds.Folder.Item.Update();
                    }
                }
                catch (Exception ex)
                {
                    //Log exception
                }
            }
            return ds;
        }


        /// <summary>
        /// Gets the document set.
        /// </summary>
        /// <param name="list">Name of the list.</param>
        /// <param name="documentSetName">Name of the document set.</param>
        /// <returns>DocumentSet</returns>
        public static DocumentSet GetDocumentSet(SPList list, string documentSetName)
        {
            DocumentSet ds = null;
            SPListItemCollection folders = list.Folders;
            foreach (SPListItem item in folders)
            {
                SPFolder folder = item.Folder;
                if (folder.Name.Equals(documentSetName))
                {
                    ds = DocumentSet.GetDocumentSet(folder);
                    break;
                }
            }
            return ds;
        }


profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+