Coding against a Document Set

3 comments

I’ve just had a 5 day training on development for SharePoint 2010. We’ve seen some cool stuff, were able to do some Hands on Labs and talk to other SharePoint experts about the new stuff that is coming up.

One of the best things though about this training was the ability to try some stuff out for yourself. The fact that no project managers or customers were bothering me for a week allowed me to finally take the time to look around in the object model, the central admin and the new front end interfaces.

Over the next few week I’ll be publishing some post on the things I’ve tried out.I hope to publish a lot more posts about SP2010 once Beta 2 comes available.

DISCLAIMER: The examples are build on and tested against a Beta 1 build of SharePoint 2010 and a Beta 1 build of Visual Studio 2010, so there is no guarantee this will work on later versions.

One of the things I was eager to try out is the new Document Sets feature. Document Sets allow you to group related documents together and share metadata between those docs. When you view a document set in a library, you are presented with a welcome page that shows the metadata of the set and the contents. The welcome page in itself is something you’re able to customize. So you can add web parts and other controls through the interface or SharePoint Designer to the welcome page.

First let me start by giving you some code you can use to show extra information about
the document set in a web part you can place on the welcome page:

try
{
     SPListItem item = SPContext.Current.ListItem;

     DocumentSet set = DocumentSet.GetDocumentSet(item.Folder);

     writer.WriteLine("ContentType: {0}<br/>", item.ContentType.Name);
     writer.WriteLine("Title: {0}<br/>", item.Title);
     writer.WriteLine("WelcomePageUrl: {0}<br/>", set.WelcomePageUrl);
     writer.WriteLine("ItemCount: {0}<br/>", set.Folder.ItemCount);
     writer.WriteLine("Welcomepage Fields:<br/>");
     DocumentSetTemplate template = set.ContentTypeTemplate;
     WelcomePageFieldCollection fields = template.WelcomePageFields;
     foreach (SPField field in fields)
     {
         writer.WriteLine("{0}<br/>", field.Title);
     }
}
catch (Exception)
{ }

First we get a reference to the current List Item through the SPContext. This list item is the main item for the document set that contains the metadata that is pushed into the child documents.

We then can get a reference to the DocumentSet by passing in the SPFolder of the item into a static method of the DocumentSet class. The DocumentSet class is stored in the Microsoft.Office.DocumentManagement.dll in the DocumentSets namespace.

The DocumentSetTemplate in turn contains more information about the fields that are shared or shown on the Welcome page.

In the next post I’ll show you how to provision a document set from a feature. Something that is quite easy to do with the new SharePoint project and item templates for Visual Studio 2010.

Tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted September 15, 2010 at 4:12 pm | Permalink

    I get the error “The object specified does not belong to a list” on line DocumentSetTemplate template = set.ContentTypeTemplate;
    witch is strange because the document set is already created inside a form library.

    Also do you know a way to add child content types to a document set using c# OM?

    Thanks,
    Paulo Fonseca

  2. Peter Gerritsen
    Posted September 15, 2010 at 6:04 pm | Permalink

    Where did you put this code?

    I haven’t tried this out but the DocumentSetTemplate has a property AllowedContentTypes. I assume you can use the Add method on that to add a child content type to the documentset through code.

  3. Paulo
    Posted September 17, 2010 at 3:16 pm | Permalink

    Hi Peter,

    1 – I already tried that and in debug mode I see it adding the additional content type, but then nothing happens in the sharepoint UI. Very strange…

    2 – Also I’ve been experimenting with this and also the instruction DocumentSet.Create that has a properties hashtable parameter does not seem to work, I don’t see in sharepoint UI the properties.

    3 – Do you have code to insert a document inside a document set, I’ve seen a couple in the internet, but nothing works :-(

    Best regards,
    Paulo Fonseca

    By the way here’s the code:

    SPSite site = new SPSite(“http://win-n2bu1ltoqd9/sites/extranet”);
    SPWeb web = site.OpenWeb();

    web.AllowUnsafeUpdates = true;
    //TODO: Get Site Content Types
    //Associar Esses CTs ao Document Set
    string s = String.Empty;
    SPContentType ct = web.ContentTypes["TemplateAlfa"];
    SPContentType ctDocSet = web.ContentTypes["Document Set"];

    SPFolder folder = web.GetFolder(“ListaProcessos/Processos”);

    Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet docSet =
    Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.GetDocumentSet(folder);

    //add the default document to your document set template
    //SPContentType customDocType = web.ContentTypes["Document Set"];

    ////get a an instance of DocumentSetTemplate for the new document set
    DocumentSetTemplate newDocumentSetTemplate =
    DocumentSetTemplate.GetDocumentSetTemplate(ctDocSet);

    ////newDocumentSetTemplate.DefaultDocuments.Add(“texte.txt”, customDocType.Id, documentoBytes);

    ////.Folder.Files.Add(“texte.txt”, documentoBytes);

    ////Add the new content type, make the field shared, and add it to the welcomepage

    newDocumentSetTemplate.AllowedContentTypes.Add(ct.Id);

    newDocumentSetTemplate.Update(true);
    ct.Update();

    web.Update();

    web.Dispose();

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>