user2405717
user2405717

Reputation: 31

Sharepoint 2010 DocumentSets - How to Manage Programatically?

I am new to Sharepoint 2010 but not new to .Net programming. Here is a my situation, i have a large set of files to be uploaded into Sharepoint 2010 with metadata. I have decided to write a C# class library to handle the documentsets programatically. I have to use to DocumentSets and i was able to successfully create a documentset. Now i am stuck with the following:

  1. How do i check if a documentset already exists?
  2. How do i remove a documentSet?

Here is my code to create the documentset:

using (SPSite site = new SPSite(spURL))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList docs = web.Lists["Documents"];

        if (docs != null)
        {
            SPContentType docSetCT = docs.ContentTypes["Document Set"];

            if (docSetCT != null)
            {
                Hashtable docsetProps = new Hashtable();
                docsetProps.Add("New Docset", "New Docset");
                DocumentSet docSet = DocumentSet.Create(docs.RootFolder, documentSetName, docSetCT.Id, docsetProps, true);
                docs.Update();
            }
        }
    }
}

Upvotes: 1

Views: 1741

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

The list of helper methods for working with Document Sets:

How do I check if a document set already exists?

private static bool IsDocumentSetExist(SPList list,string docSetName)
{
    var folderUrl = SPUrlUtility.CombineUrl(list.RootFolder.ServerRelativeUrl, docSetName);
    var folder = list.ParentWeb.GetFolder(folderUrl);
    return folder.Exists;
}

Usage:

var docSetExists = IsDocumentSetExist(docs, "New Docset");

How do I remove a document set?

private static void DeleteDocumentSet(DocumentSet docSet)
{
    docSet.Folder.Delete();
}

Upvotes: 4

Related Questions