Moving Document Set From One Document Library To Another, In Same Site

Problem

Moving document set from one document library to another document library in same site [SharePoint Online]/SharePoint 2016, using CSOM is an issue. OOTB solution is not available to move the document set from one library to another library. Move / copy action from "Site Content and Feature" does not work for document set. Move/Copy Items are only supported only for file and items.

Resolution

Microsoft latest CSOM Package Version 16.1.4727.1200 has addressed the issues given below and the latest features.

Released Date Updated on 18th of Dec 2015



URL to download.

Using this new DLL, we can export the document set and from the source document library to the target library.

USING CSOM (.NET)

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Microsoft.SharePoint.Client;  
  8. using Microsoft.SharePoint.Client.DocumentSet;  
  9. namespace DocSetCSOM {  
  10.     class Program {  
  11.         static void Main(string[] args) {  
  12.             if (args.Length != 2) {  
  13.                 Console.WriteLine("Usage: DocSetCSOM.exe <newDocSetName> <docSetContentTypeName>");  
  14.                 return;  
  15.             }  
  16.             ClientContext context = new ClientContext("URL");  
  17.             ContentTypeCollection contentTypes = context.Web.ContentTypes;  
  18.             context.Load(contentTypes);  
  19.             context.ExecuteQuery();  
  20.             ContentType docSetContentType = null;  
  21.             foreach(ContentType ct in contentTypes) {  
  22.                 if (ct.Name == args[1]) {  
  23.                     docSetContentType = ct;  
  24.                     break;  
  25.                 }  
  26.             }  
  27.             List list = context.Web.Lists.GetByTitle("Documents");  
  28.             List target = context.Web.Lists.GetByTitle("target");  
  29.             Folder folder = context.Web.GetFolderByServerRelativeUrl("target/xyz");  
  30.             ListItem listItem = list.GetItemById(2);  
  31.             DocumentSet docSet = DocumentSet.GetDocumentSet(context, listItem.Folder);  
  32.             ClientArrayResult < byte > data = docSet.ExportDocumentSet();  
  33.             context.ExecuteQuery();  
  34.             DocumentSet newDocSet = DocumentSet.ImportDocumentSet(context, data.Value, args[0], folder, docSetContentType.Id, context.Web.CurrentUser);  
  35.             context.ExecuteQuery();  
  36.             Console.WriteLine("Done");  
  37.         }  
  38.     }  
  39. }  
Using Server Side Code

 

  1. static void Main(string[] args) {  
  2.     PSite site = new SPSite("Site")) //Get the site  
  3. {  
  4.     using(SPWeb web = site.RootWeb) //Get the web  
  5.     {  
  6.         SPList list = web.Lists["My documents"]; //Get the list  
  7.         SPFolder folder = list.RootFolder; //Find the folder to create in  
  8.         SPContentType docsetCT = list.ContentTypes["Document Set"]; //Find the content type to use  
  9.         Hashtable properties = new Hashtable(); //Create the properties hashtable  
  10.         properties.Add("DocumentSetDescription""New Document Set"); //Populate the properties  
  11.         foreach(SPListItem item in list.Items) {  
  12.             if (item.Folder != null) {  
  13.                 DocumentSet newDocset = DocumentSet.GetDocumentSet(item.Folder);  
  14.                 if (newDocset != null) {  
  15.                     //Now we'll export it so we can create an exact copy of it somewhere else  
  16.                     byte[] compressedFile = newDocset.Export();  
  17.                     //Then we get the target list  
  18.                     SPList targetList = web.Lists["NewDocumentLib"];  
  19.                     SPContentType secondCt = targetList.ContentTypes["Document Set"];  
  20.                     SPFolder targetFolder = targetList.RootFolder;  
  21.                     DocumentSet.Import(compressedFile, item.Name + "Backup", targetFolder, secondCt.Id, properties, web.CurrentUser);  
  22.                 }  
  23.             }  
  24.         }  
  25.     }  
  26. }  
Ebook Download
View all
Learn
View all