DocumentSet.Create method

Gets the DocumentSet object from the SPFolder object.

This topic includes three code examples:

  • Create and import a DocumentSet

  • Import properties

  • Get a DocumentSet and send it to an OfficialFile

using System;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;
using System.Collections;
using System.IO;

namespace CreateNewDocset
{
class Program
{
static void Main(string[] args)
{
//This sample will create a new Document Set and
//then copy it to a second location
using (SPSite site = new SPSite("http://localhost"))//Get the site
{
using (SPWeb web = site.RootWeb)//Get the web
{
SPList list = web.Lists["Shared 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
DocumentSet newDocset = DocumentSet.Create(folder, "Docset1", docsetCT.Id, properties, true);

//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["Shared Documents Backup"];
SPContentType secondCt = targetList.ContentTypes["Document Set"];
SPFolder targetFolder = targetList.RootFolder;

DocumentSet.Import(compressedFile, "Docset1 Backup", targetFolder, secondCt.Id, properties, web.CurrentUser);
}
}
}
}
}
using System;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;
using System.Collections;
using System.IO;

namespace CreateNewDocset
{
class Program
{
static void Main(string[] args)
{
//This sample will get all of the existing document sets
//in a list and show some of its properties
using (SPSite site = new SPSite("http://localhost"))//Get the site
{
using (SPWeb web = site.RootWeb)//Get the web
{
SPList list = web.Lists["Shared Documents"];//Get the list
foreach (SPListItem item in list.Items)
{
if (DocumentSetTemplate.Id.IsParentOf(item.ContentTypeId))
{
DocumentSet docset = DocumentSet.GetDocumentSet(item.Folder);
Console.WriteLine("DocumentSet: " + docset.Item.Name);
Console.WriteLine(String.Format("Contains {0} files", docset.Folder.Files.Count));
Console.WriteLine(String.Format("Has {0} versions", docset.VersionCollection.Count));
Console.WriteLine(String.Format("Has {0} shared fields", docset.ContentTypeTemplate.SharedFields.Count));
}
}
}
}
}
}
}
using System;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;
using Microsoft.SharePoint.Administration;

namespace SendToOfficialFile
{
class Program
{
//this sample will get all of the existing document sets on a list
//and send it to a content organizer via the 
//officialfile web service
static void Main(string[] args)
{
using (SPSite source = new SPSite("http://localhost/sites/documents"), 
target = new SPSite("http://localhost/sites/records"))// get both the source and target site
{
using (SPWeb srcweb = source.RootWeb, tgtweb = target.RootWeb)// open the webs
{
SPList srclist = srcweb.Lists["Documents"]; //open the source list
SPContentType docsetCT = srclist.ContentTypes["Document Set"]; //find the docset content type
SPFolder folder = srclist.RootFolder;
SPOfficialFileHost tgthost = null;
//find the official file host
foreach (SPOfficialFileHost host in target.WebApplication.OfficialFileHosts)
{
if (host.OfficialFileName == "Send To Records")
{
tgthost = host;
break;
}
}
//iterate through all items in the source list
foreach (SPFolder subFolder in folder.SubFolders)
{
if (subFolder.Item == null)
continue;
if (subFolder.Item.ContentType.Id == docsetCT.Id) //take action on ducment sets
{
DocumentSet docset = DocumentSet.GetDocumentSet(subFolder); //get the docset
Console.WriteLine(docset.Item.Name);
string outstring = null;
Console.WriteLine(docset.SendToOfficialFile(String.Empty, out outstring, tgthost, srcweb.CurrentUser.LoginName, SPOfficialFileSubmissionMode.Workflow)); //send it to official file
Console.WriteLine(outstring);
}
}
}
}
}
}
}
Show: