Click here to Skip to main content
6,306,412 members and growing! (18,776 online)
Email Password   helpLost your password?
Enterprise Systems » SharePoint Server » Custom Controls     Intermediate License: The GNU General Public License (GPL)

Download SharePoint Zipped List Items

By Mohamed Zaki

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.
C# 2.0, Windows, Office, .NET 2.0, ASP.NET, Visual Studio, SQL 2005, IIS 6, Dev
Version:2 (See All)
Posted:25 Oct 2008
Views:6,672
Bookmarked:8 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
2 votes for this article.
Popularity: 1.20 Rating: 4.00 out of 5

1

2
1 vote, 50.0%
3

4
1 vote, 50.0%
5

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:

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 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:

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.

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 (GPL)

About the Author

Mohamed Zaki


Member
MVP Sharepoint
Occupation: Architect
Location: Egypt Egypt

Other popular SharePoint Server articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralCan use your code to add Submenu to Document Library Context menu ?? PinmemberBinhluong0:21 16 Apr '09  
GeneralWhere I have to Use the Above code PinmemberMember 47208052:26 12 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Oct 2008
Editor: Smitha Vijayan
Copyright 2008 by Mohamed Zaki
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project