Migrating Existing Content into Document Sets

Document sets are so great I am sure you have thought up lots of different ways to use them, but there is just one problem. You have been using previous versions of SharePoint and already have tons of content that should be turned into document sets. Well there are a few ways you can accomplish this. You could manually create new document sets for existing content and then copy the appropriate items over. Of course this is time consuming and not feasible for a large amount of content. You could also use workflows to create new document sets, although this would also be fairly manual. Another option is to use SharePoint copy functionality to copy items and the Document Set object model to create new document sets.

Here is some sample code for a tool that can be used to copy existing content into Document Sets. This is a command line tool that when run will access a hard coded site. It will give you options of document libraries that you can select as a source and then options of document libraries that you can select as a destination. Then the tool will create a document set for each folder and copy just the documents that are inside that folder to the new document set. Because SPCopy is used, if there are columns that are on the folder and the document set content type then those properties will be copied over.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;
using System.Collections;

namespace FoldersToDocsets
{
 class Program
 {
  static void Main(string[] args)
  {
   using (SPSite site = new SPSite("https://localhost"))
   {
    using (SPWeb web = site.RootWeb)
    {
     Console.WriteLine("Select source list:");
     SPList source = ChooseList(web);
     Console.WriteLine("Select target list:");
     SPList target = ChooseList(web);
     
     SPFolder rootFolder = source.RootFolder;
     foreach (SPListItem folderItem in source.Folders)
     {
      SPFolder folder = folderItem.Folder;
      if (folder.ParentFolder.Item == null && folder.SubFolders.Count == 0)
      {
       ConvertToDocset(folder, target);
      }
     }
     Console.WriteLine("END");
     Console.ReadLine();
    }
   }
  }

  private static void ConvertToDocset(SPFolder folder, SPList targetList)
  {
   SPContentType docsetCT = targetList.ContentTypes["Document Set"];
   DocumentSet newDocset = DocumentSet.Create(targetList.RootFolder, folder.Name, docsetCT.Id, new Hashtable());
   string newUrl = newDocset.Item.Url;
   foreach (SPFile file in folder.Files)
   {
    file.CopyTo(newUrl + "/" + file.Name);
   }
  }

  

  private static SPList ChooseList(SPWeb parentWeb)
  {
   Dictionary<int, Guid> lists = new Dictionary<int, Guid>();
   int index = 0;
   foreach (SPList list in parentWeb.Lists)
   {
    if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
    {
     Console.WriteLine(index.ToString() + " - " + list.Title);
     lists.Add(index,list.ID);
     index++;
    }
   }
   Console.Write("Selection: ");
   index = int.Parse(Console.ReadLine());
   return parentWeb.Lists[lists[index]];
  }
 }
}

That was easy. Thanks for reading, now go make yourself some document sets!

Quentin Christensen, Program Manager