Many of you must
have come across situations where you have to move the already created Document
sets (with a whole bunch of documents in it) from one location to another due
to organization re-structure or may be introduction of some new ways of
working.
I did come across
similar situation where I had to move 5000 Document Sets with documents in them
counting 60,000 in total from one library to another. In my case the document
sets are within a folder in the source library. Since I am not a folder-loving
developer, I tend not to use them frequently.
So here is my code
to copy the document sets from source library and put it into the destination
library without folders.
Please note:
Please note:
- Metadata columns (for Document Set and Document) should read same
in the destination library and the source library if you want them to be
copied along the process
- Make sure you have set up Document Set content types in the
destination library
I am using a console application so
add reference to
- Microsoft.SharePoint
- Microsoft.Office.DocumentManagement.DocumentSets
using (SPSite site = new SPSite("http://sharepointsite")) { using (SPWeb web = site.OpenWeb()) { SPFolder folder = web.GetFolder("folderURL"); // sharepointsite/library/folder SPList sourceList = web.Lists["sourcelistname"]; SPQuery query = new SPQuery(); query.Folder = folder; SPListItemCollection itemColl = sourceList.GetItems(query); foreach (SPListItem sourceItem in itemColl) { if (sourceItem.ContentType.Name == "Document Set") { DocumentSet documentSet = DocumentSet.GetDocumentSet(sourceItem.Folder); SPList targetList = web.Lists["targetListName"]; SPContentTypeId contentTypeId = targetList.ContentTypes["destination document library"].Id; byte[] documentSetData = documentSet.Export(); string documentSetName = documentSet.Item.Name; SPFolder targetFolder = targetList.RootFolder; Hashtable properties = sourceItem.Properties; DocumentSet.Import(documentSetData, documentSetName, targetFolder, contentTypeId, properties, web.CurrentUser); } } } }