Click here to Skip to main content
15,867,568 members
Articles / Web Development / IIS

Download SharePoint Zipped List Items

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
25 Oct 2008GPL31 min read 48.7K   481   31   4
This custom UI Action for SharePoint extends the lists action menu to allow users to zip document library items and download all of them with or without version.

Introduction

This custom UI Actions for SharePoint extends the lists action menu to allow users to zip document library items and download all of them with or without version.

Features

  • Download all document library items
  • Versions: if you are care about document versions, you can download them as well
  • Ability to download only the selected view items instead of all list items

The new menu added to the SharePoint document library Actions menu:

Image 1

After clicking over the menu item:

downloadbox_resize.jpg

the compressed file is downloaded:

ZipExplorer_resize.jpg

The compressed file downloaded with the file versions:

ZipExplorer_versions_resize.jpg

Code description

First, we need to create a new feature that defines a new SharePoint Custom UI Action as below:

XML
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomAction
 Id="DownloadZippedItems.MenuLink"
 Location="Microsoft.SharePoint.StandardMenu"
 GroupId="ActionsMenu"
 ControlAssembly="MZaki.CustomActions.DownloadZippedItems, 
                  Version=1.0.0.0, Culture=neutral, PublicKeyToken=da6289be64eaeba3"
 ControlClass="MZaki.CustomActions.DownloadZippedItems">
</CustomAction>

</Elements>

In this custom action, we use a Control Assembly instead of redirecting to another URL. This assembly is responsible for rendering the menu and its sub menus and handling the postback event:

C#
protected override void CreateChildControls()
{
    if (!this.ChildControlsCreated)
    {
        base.CreateChildControls();

        // Create the sub menu item
        SubMenuTemplate mnuZipListItems = new SubMenuTemplate();
        mnuZipListItems.Text = "Zip List Items";
        mnuZipListItems.ImageUrl = "/_layouts/images/TBSPRSHT.GIF";
        mnuZipListItems.Description = "Zip and download List Items";
        //mnuZipListItems.ID = "downloadZipped";

        //Add zip and download all 
        PostBackMenuItemTemplate mnuListItem = 
                        new PostBackMenuItemTemplate();
        mnuListItem.ID = "menu1";
        mnuListItem.Text = "All Items";
        //mnuListItem.ID = "mymenulistitemid";
        mnuListItem.Description = "Zip and Download All Items";
        mnuListItem.OnPostBack += 
          new EventHandler<eventargs>(mnuListItem_OnPostBack);

        //Add zip and download all with versions
        PostBackMenuItemTemplate mnuListItem2 = new PostBackMenuItemTemplate();
        mnuListItem2.Text = "All Items with Versions";
        mnuListItem2.Description = "Zip and Download All Items";
        mnuListItem2.ID = "menu2";
        mnuListItem2.OnPostBack += 
          new EventHandler<eventargs>(mnuListItem2_OnPostBack);

        //Separator
        MenuSeparatorTemplate separator = new MenuSeparatorTemplate();

        //Current View only
        PostBackMenuItemTemplate mnuListItemCurrentView = 
                                 new PostBackMenuItemTemplate();
        mnuListItemCurrentView.Text = "Items In Current View";
        mnuListItemCurrentView.Description = "Zip and Download All Items";
        mnuListItemCurrentView.ID = "menu3";
        mnuListItemCurrentView.OnPostBack += 
          new EventHandler<eventargs>(mnuListItemCurrentView_OnPostBack);

        //Current View only with versions
        PostBackMenuItemTemplate mnuListItemCurrentViewVersions = 
                                 new PostBackMenuItemTemplate();
        mnuListItemCurrentViewVersions.Text = 
                   "Items In Current View With Versions";
        mnuListItemCurrentViewVersions.Description = 
                   "Zip and Download All Items";
        mnuListItemCurrentViewVersions.ID = "menu4";
        mnuListItemCurrentViewVersions.OnPostBack += 
          new EventHandler<eventargs>(mnuListItemCurrentViewVersions_OnPostBack);

        mnuZipListItems.Controls.Add(mnuListItem);
        mnuZipListItems.Controls.Add(mnuListItem2);

        mnuZipListItems.Controls.Add(separator);
        mnuZipListItems.Controls.Add(mnuListItemCurrentView);
        mnuZipListItems.Controls.Add(mnuListItemCurrentViewVersions);

        this.Controls.Add(mnuZipListItems);
    }
}

Now, in the postback handler, we simply retrieve the list item files and compress them to a temp directory, and force the browser to download the file.

C#
void PushFileToDownload(string FilePath,string FileName)
{
    FileInfo fInfo = new FileInfo(FilePath);
    HttpContext.Current.Response.ContentType = "application/x-download";
    HttpContext.Current.Response.AppendHeader("Content-Disposition", 
                        "attachment; filename=" + FileName);
    HttpContext.Current.Response.AddHeader("Content-Length", fInfo.Length.ToString());
    HttpContext.Current.Response.WriteFile(FilePath);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
}

Here is the project page on CodePlex: http://codeplex.com/mzakicustomactions.

Known issues

  • Arabic file names: Arabic file names are not downloaded correctly
  • The tool uses the system temporary directory to export the list items and compress them, so you need to watch this folder and manage a cleanup mechanism

Thank you

  • The compression library I've used to compress the list items is SharpZipLib.
  • I have also used a code sample posted on Peter Bromberg's blog to compress the entire folder. Thank you Peter :) The post and the code sample were great.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
Egypt Egypt
MVP Sharepoint

Comments and Discussions

 
GeneralMy vote of 4 Pin
Ramesh.kbvr27-Feb-11 21:51
Ramesh.kbvr27-Feb-11 21:51 
GeneralAdding/Customizing action menu in SharePoint Pin
elizas8-Feb-10 0:43
elizas8-Feb-10 0:43 
QuestionCan use your code to add Submenu to Document Library Context menu ?? Pin
Binhluong15-Apr-09 23:21
Binhluong15-Apr-09 23:21 
GeneralWhere I have to Use the Above code Pin
Member 472080512-Jan-09 1:26
Member 472080512-Jan-09 1:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.