IIS 6.0Microsoft OfficeIIS.NET 2.0SQL Server 2005C# 2.0中级开发Visual StudioSQL ServerWindows.NETASP.NETC#
下载 SharePoint 压缩列表项






4.75/5 (3投票s)
此 SharePoint 自定义 UI 操作扩展了列表操作菜单,允许用户压缩文档库中的项目并下载所有项目,包括或不包括版本。
引言
此 SharePoint 自定义 UI 操作扩展了列表操作菜单,允许用户压缩文档库中的项目并下载所有项目,包括或不包括版本。
特点
- 下载所有文档库项目
- 版本:如果您关心文档版本,也可以下载它们。
- 能够仅下载选定的视图项,而不是所有列表项
添加到 SharePoint 文档库操作菜单的新菜单
单击菜单项后
压缩文件将被下载
下载的压缩文件包含文件版本
代码描述
首先,我们需要创建一个新的功能,该功能定义一个新的 SharePoint 自定义 UI 操作,如下所示
<?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>
在此自定义操作中,我们使用控件程序集而不是重定向到另一个 URL。此程序集负责呈现菜单及其子菜单以及处理回发事件。
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);
}
}
现在,在回发处理程序中,我们只需检索列表项目文件并将它们压缩到临时目录,然后强制浏览器下载该文件。
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();
}
这是 CodePlex 上的项目页面:http://codeplex.com/mzakicustomactions。
已知问题
- 阿拉伯文件名:阿拉伯文件名无法正确下载
- 该工具使用系统临时目录导出列表项并压缩它们,因此您需要监视此文件夹并管理清理机制
谢谢
- 我用来压缩列表项的压缩库是 SharpZipLib。
- 我还使用了 Peter Bromberg 的博客上发布的一个代码示例 http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-f.aspx 来压缩整个文件夹。 感谢 Peter :) 这篇文章和代码示例都很棒。