Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Problem
Moving document set from one document Library to another document library in the same site [SharePoint Online]/SharePoint 2016 using CSOM is an issue. The OOTB solution is not available to move document set from one library to another library. Move / copy action from "Site Content and Feature" is not worked for the 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 following issues and latest features.
Released Date: Updated on 18th of Dec 2015
URL to download:
https://dev.office.com/blogs/new-sharepoint-csom-version-released-for-Office-365
Using this new DLL, we can export the document set and from the source document library to target library.
Sample Code
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")) //Get the site
{
using(SPWeb web = site.RootWeb) //Get the web
{
SPList list = web.Lists["My documents"]; //Get the list
SPFolder folder = list.RootFolder; //Find the folder to create in
SPContentType docsetCT = list.ContentTypes["Document Set"]; //Find the content type to use
Hashtable properties = new Hashtable(); //Create the properties hashtable
properties.Add("DocumentSetDescription", "New Document Set"); //Populate the properties
foreach(SPListItem item in list.Items) {
if (item.Folder != null) {
DocumentSet newDocset = DocumentSet.GetDocumentSet(item.Folder);
if (newDocset != null) {
//Now we'll export it so we can create an exact copy of it somewhere else
byte[] compressedFile = newDocset.Export();
//Then we get the target list
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);
}
}
}
}
}
}