Managing Document Sets Programmatically in SharePoint 2010 using Visual Studio

Steps
     Open Visual Studio in your system.
  • Select Console Application template and give as name.
  • Add a Microsoft.Client Assembly reference file in right side reference tab in visual studio.
  • Replace Program.cs with the source code.
  1. using System;   
  2. using Microsoft.Office.DocumentManagement.DocumentSets;   
  3. using Microsoft.SharePoint;   
  4. using Microsoft.SharePoint.WebControls;   
  5. namespace ManageDocumentSetTemplate    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)     
  10.         {    
  11.              int count = 0;   
  12.              string resultLabel="";  
  13.             //Get the Shared Documents document libary   
  14.   
  15.             SPWeb currentWeb = SPContext.Current.Web;   
  16.             SPDocumentLibrary sharedDocuments = (SPDocumentLibrary)currentWeb.Lists["Shared Documents"];   
  17.             //Loop though all the folders. Some of them will be document sets   
  18.             //NOTE: don't use the sharedDocuments.Folders collection! Use sharedDocuments.RootFolder.SubFolders   
  19.   
  20.             foreach(SPFolder currentFolder in sharedDocuments.RootFolder.SubFolders)   
  21.             {   
  22.                 //Get the corresponding document set   
  23.   
  24.                 DocumentSet currentDocSet = DocumentSet.GetDocumentSet(currentFolder);   
  25.                 //Strangely the previous line always returns a document set object   
  26.                 //Even if the current folder is not a document set. So we use the   
  27.                 //following test to find out if the current folder is a document set   
  28.                 if (currentDocSet.Item != null)   
  29.                 {   
  30.                     //This folder is a document set. Increase the count   
  31.                     count += 1;   
  32.                     //Read some properties of the document set   
  33.                     resultLabel += "Name: " + currentFolder.Name + "<br />";   
  34.                     resultLabel += "Content Type: " + currentDocSet.ContentType.Name + "<br />";   
  35.                     resultLabel += "Document Count: " + currentDocSet.Folder.ItemCount + "<br />";   
  36.                     resultLabel += "Welcome Page: " + currentDocSet.WelcomePageUrl + "<br /><br />";   
  37.                 }   
  38.             }   
  39.             //Display the count   
  40.             Console.WriteLine(count.ToString());   
  41.         }   
  42.     }  

Hit F5 and check the output..
 
Hope you hve enjoyed my articles.
Ebook Download
View all
Learn
View all