Wednesday, April 30, 2014

Creating Document Set Based Containers

SharePoint 2010 and 2013 provide a content type called a DocumentSet. To add this content type to a site collection you must activate the feature “Document Sets”, or by code using FeatureID “3bae86a2-776d-499d-9db8-fa4cdc7884f8”. A document set is like a folder in a library, in that you can create or upload documents under the document set. It is also similar to a SPListItem because the Document Set can have custom site columns assigned to it.

For example, you could create a new content type based on the Document Set content type for presentations; maybe call it “Presentation”. You might create custom site columns such as “Presentation Date”, “Presenter Name”, “Presenter Email”, “Presentation Summary”, etc. and add them to the “Presentation” content type and it’s Welcome Page.

Then you can create new Presentation document sets with the Title, Presentation Date, Presenter Name, etc. The presentation materials, such as; slides, pictures, agenda, etc. can then all be uploaded and stored within this presentation document set as a related set of documents.

This post is about how to create a new document set container from a document set based content type, set its properties and add files to the document set. I do not cover the creation of Document Set based content types or the customization in this post.

PowerShell (sample code)
# Make sure the SharePoint namespace is loaded
add-pssnapin Microsoft.SharePoint.PowerShell –erroraction silentlycontinue;

# Set some sample variables
$weburl = "http://contoso.local/sites/Classes/USHistory";
$libraryname = "Materials";
$presentationtitle = "Civil War Weapons";
$files = ("E:\Civil War Weapons\Weapons.pptx", "E:\Civil War Weapons\Syllabus.pdf");

# Get the SPWeb object
$web = get-spweb $weburl;

# Get the content id of the presentation document set
$ctId = $web.contenttypes["Presentation"].id.tostring();

# Get the SPList object
$list = $web.lists.trygetlist($libraryname);
if ($list –eq $null) {
    # List not found, do error processing
    throw new-object ArgumentNullException($libraryname, "Library was not found!")
}
$folder = $list.rootfolder;

# Create a hash table for the custom property values
$hash = $null;
$hash = @{};
$hash.add("Presentation Date", get-date "5/12/2014 11:00 AM");
$hash.add("Presentater Name", "Joseph Assif");
$hash.add("Presentater Email", "jassif@university.edu");
$hash.add("Presentation Summary", "Lecture on the various weapons that were used during the US Civil War");

# Create the new presentation document set container
$newPresentation = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($list.rootfolder, $presentationtitle, $list.contenttypes.bestmatch($ctId), $hash);
$newPresentation.provision();

# If needed, you can capture the id
$newId = $newPresentation.item.id;

# Process the files for the document set
foreach ($file in $files) {

    # Read the file
    $f = get-childitem $file;
    $filestream = ([System.IO.FileInfo] (get-item $f.fullname)).openread();

    # Create the file in the document set
    $target = "{0}/{1}/{2}" –f $folder, $presentationtitle, $f.name;
    [Microsoft.SharePoint.SPFile]$spFile = $folder.files.add($target, [System.IO.Stream]$filestream, $true);
    $filestream.close();

    # Set any file properties you may have defined
    $spFile.item["Title"] = "Your document title text");
    $spFile.item.update();

    # Other document processes needed (checkin, etc.)

} # end foreach file

# Release SPWeb from memory
$web.dispose();


1 comment: