Creating a Pitchbook – Putting a Button in the Document Set Ribbon and Downloading Document Sets as a Zip File (Part 5)
Once again this is Quentin, this time with part five of the pitchbook series of articles demonstrating document set customization. For this post I am teaming up with Carlos, a developer at Microsoft that worked on Document Sets. Today we are going to demonstrate customizing the document set ribbon to add a button that calls the document set object model to download the document set as a zip file. One of the great things about document sets is that they have their own ribbon tab that you can customize. Awesome and super useful, just like my minions that take care of everything from my dry cleaning to writing my blog posts. But you didn't hear that from me.
You may want to look at these other articles if you haven’t already seen them.
Creating a Pitchbook - Customizing Document Set Settings (Part 1)
Creating a Pitchbook – Customizing the Document Set Welcome Page (Part 2)
Creating a Pitchbook – Closing the Pitchbook with Document Set Workflow Actions (Part 3)
If you are not familiar with creating ribbon buttons and commands take a look How to Add a Tab to the Ribbon in SharePoint Foundation first. Ribbon customization is not too difficult to get started with, but it can be a bit complex. Basically two XML files will be needed to create the ribbon button. These two XML files should be placed in their own folder in the Features folder for your SharePoint installation (by default located at: Program files\common files\ Microsoft shared\web server extensions\14\template\features), for this example I will use the folder name addzipribbon.
The first XML file is Element.xml.
Element.xml
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="Ribbon.Documents.DocsetZip"
Title="Download Document Set as ZIP"
RegistrationType="ContentType"
RegistrationId="0x0120D520"
Location="CommandUI.Ribbon"
>
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.ManageDocumentSet.MDS.Manage.Controls._children">
<Button
Id="Ribbon.ManageDocumentSet.MDS.Manage.Controls.DownasZip"
Sequence="20"
Command="Ribbon.ManageDocumentSet.MDS.Manage.DownZip"
Alt="Download as ZIP"
Image16by16="/_layouts/images/zipfile16x.png"
Image32by32="/_layouts/images/zipfile32x.png"
LabelText="Download as Zip"
ToolTipTitle="Download as Zip"
ToolTipDescription="Compress the document set and download"
TemplateAlias="o1"
/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="Ribbon.ManageDocumentSet.MDS.Manage.DownZip"
CommandAction="javascript:__doPostBack('DownloadZipDelegateEvent', '')" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>
RegistrationID is the ID of the document set content type that this ribbon button will appear on. 0x0120D520 is the base document set content type ID so this ribbon button will appear on all document set content types. This should be scoped to your specific content type.
Location specifies the document set ribbon tab.
Image16by16 and Image32by32 point to images located in /_layouts/images on the server. You will need to place your own images in a location where the button can access them and then link to them for these attributes.
LabelText, ToolTipTitle, and ToolTipDescription should all be self-explanatory. This is text used for the ribbon title and tool tip.
CommandAction is the important part here; this specifies what the ribbon button actually does. In this example javascript is used to do a post back event for the DownloadZipDelegateEvent, which will be used to download the document set as a zip file.
Feature.xml specifies the feature that this ribbon will be contained in.
Feature.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- _lcid="1033" _version="14.0.3427" _dal="1" -->
<!-- _LocalBinding -->
<!-- Copyright (c) Microsoft Corporation. All rights reserved. -->
<Feature xmlns="https://schemas.microsoft.com/sharepoint/"
Id="{CC06B38F-B9AB-4227-841C-DC9F438345A7}"
Title="Add new button"
Description="Add new button to ribbon"
Version="1.0.0.0"
Scope="Web"
Hidden="TRUE">
<ElementManifests>
<ElementManifest Location="element.xml" />
</ElementManifests>
</Feature>
This file specifies the activation of the feature. In this example the scope is web.
Once these files are in place the feature can be activated by running the following commands:
stsadm.exe –o installfeature –filename adddocsetribbon\feature.xml
stsadm.exe –o activatefeature –filename adddocsetribbon\feature.xml –url “https://server”
https://server should be the location of the web to activate the feature on.
Now that the feature is installed I can go to a document set and click on the Document Set ribbon tab and see the button.
Now that the ribbon button is installed I need to create the code to actually make it work. One thing users may want to do with a document set is download the entire contents of the document set, rather than just each file individually. Document sets have an export framework in place to send document sets to content organizers using send to, but this can also be used to export zip files so users can download them. To do this I create a dll that uses the document set object model to export the document set when the ribbon button is clicked.
Here is the code:
using System;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;
using System.Web.UI.WebControls;
namespace MyRibbonDelegate
{
public class MyRibbonDelegateClass : WebControl
{
protected override void OnLoad(EventArgs e)
{
this.EnsureChildControls();
base.OnLoad(e);
if (this.Page.Request["__EVENTTARGET"] == "DownloadZipDelegateEvent")
{
SPListItem currentItem = SPContext.Current.ListItem;
DocumentSet currentDocset = DocumentSet.GetDocumentSet(currentItem.Folder);
this.Page.Response.ContentType = "application/zip";
currentDocset.Export(this.Page.Response.OutputStream, 1024);
this.Page.Response.End();
}
}
}
}
A reference to Microsoft.Office.DocumentManagement is required to use the DocumentSets object model. In this example there is a MyRibbonDelegateClass that On Load it checks to see if it is the DownloadZipDelegateEvent, if so the current document set is retrieved. The Export method for the document set is then called and specifies the OutPutStream of the page as a parameter (to get the files inside the document set). For export there is a second parameter that is an int set to 1024. This allows you to specify the value in MB that can be exported. If there is more than 1 GB of files inside the document set then this event will fail.
When the ribbon button is clicked users will get a dialog to open or save the contents of the document set as a zip package.
That covers creating a custom button in the document set ribbon and downloading the contents of a document set as a zip file. Now users can easily pack up their sales contract, presentation, and other sales proposal documents for when they go make their sales pitch. Watch for our next blog post, which will be on applying metadata based on content type to documents uploaded to document sets.
Quentin Christensen, Program Manager
Carlos David Argott Hernandez, Software Development Engineer