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)
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using Microsoft.SharePoint.Client.DocumentSet;
- namespace DocSetCSOM {
- class Program {
- static void Main(string[] args) {
- if (args.Length != 2) {
- Console.WriteLine("Usage: DocSetCSOM.exe <newDocSetName> <docSetContentTypeName>");
- return;
- }
- ClientContext context = new ClientContext("URL");
- ContentTypeCollection contentTypes = context.Web.ContentTypes;
- context.Load(contentTypes);
- context.ExecuteQuery();
- ContentType docSetContentType = null;
- foreach(ContentType ct in contentTypes) {
- if (ct.Name == args[1]) {
- docSetContentType = ct;
- break;
- }
- }
- List list = context.Web.Lists.GetByTitle("Documents");
- List target = context.Web.Lists.GetByTitle("target");
- Folder folder = context.Web.GetFolderByServerRelativeUrl("target/xyz");
- ListItem listItem = list.GetItemById(2);
- DocumentSet docSet = DocumentSet.GetDocumentSet(context, listItem.Folder);
- ClientArrayResult < byte > data = docSet.ExportDocumentSet();
- context.ExecuteQuery();
- DocumentSet newDocSet = DocumentSet.ImportDocumentSet(context, data.Value, args[0], folder, docSetContentType.Id, context.Web.CurrentUser);
- context.ExecuteQuery();
- Console.WriteLine("Done");
- }
- }
- }
Using Server Side Code
- static void Main(string[] args) {
- PSite site = new SPSite("Site"))
- {
- using(SPWeb web = site.RootWeb)
- {
- SPList list = web.Lists["My documents"];
- SPFolder folder = list.RootFolder;
- SPContentType docsetCT = list.ContentTypes["Document Set"];
- Hashtable properties = new Hashtable();
- properties.Add("DocumentSetDescription", "New Document Set");
- foreach(SPListItem item in list.Items) {
- if (item.Folder != null) {
- DocumentSet newDocset = DocumentSet.GetDocumentSet(item.Folder);
- if (newDocset != null) {
-
- byte[] compressedFile = newDocset.Export();
-
- SPList targetList = web.Lists["NewDocumentLib"];
- SPContentType secondCt = targetList.ContentTypes["Document Set"];
- SPFolder targetFolder = targetList.RootFolder;
- DocumentSet.Import(compressedFile, item.Name + "Backup", targetFolder, secondCt.Id, properties, web.CurrentUser);
- }
- }
- }
- }
- }
- }