在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除、批量签出、批量签入等,但是,很遗憾,没有提供批量下载,默认的只能一个个下载,当选择多个文档时,下载副本就变成了灰色不可用。
在此我们将开发一个Ribbon按钮,实现文档(包括含有文件夹)的批量下载为Zip压缩包的功能。
先上传一张项目的资源管理结构
现在开始:打开VS2010,创建一个“空白SharePoint项目”命名DeviantPoint.DownloadZip
指定用于部署的网站
然后添加一个到ICSharpCode.SharpZipLib.dll(一个.NET类库,用于处理Zip文件)的引用,然后创建一个文件夹Classes用于保存帮助类。
创建类ZipBuilder,该类用于创建zip文件,创建这个帮助类可以使得在别的地方重复使用,而不仅仅针对本项目。
通常,一个类的实例被创建后,你需要转递一个文件流写入zip文件。它可以是任意类型文件流,二进制流,存储流等等。在这个ZipBuilder类中允许你添加文件和文件夹,最终输出到zip文件。因为它要处理文件流,所以该类实现了IDisposable接口。
如下为ZipBuilder类的代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using ICSharpCode.SharpZipLib.Zip;
- using ICSharpCode.SharpZipLib.Core;
-
- namespace DeviantPoint.DownloadZip
- {
- public class ZipFileBuilder : IDisposable
- {
- private bool disposed = false;
-
- ZipOutputStream zipStream = null;
- protected ZipOutputStream ZipStream
- {
- get { return zipStream; }
-
- }
-
- ZipEntryFactory factory = null;
- private ZipEntryFactory Factory
- {
- get { return factory; }
- }
-
-
- public ZipFileBuilder(Stream outStream)
- {
- zipStream = new ZipOutputStream(outStream);
- zipStream.SetLevel(9);
-
- factory = new ZipEntryFactory(DateTime.Now);
- }
-
- public void Add(string fileName, Stream fileStream)
- {
-
- ZipEntry entry = factory.MakeFileEntry(fileName);
- entry.DateTime = DateTime.Now;
- ZipStream.PutNextEntry(entry);
-
- byte[] buffer = new byte[65536];
-
- int sourceBytes;
- do
- {
- sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
- ZipStream.Write(buffer, 0, sourceBytes);
- }
- while (sourceBytes > 0);
-
-
- }
-
- public void AddDirectory(string directoryName)
- {
- ZipEntry entry = factory.MakeDirectoryEntry(directoryName);
- ZipStream.PutNextEntry(entry);
- }
-
- public void Finish()
- {
- if (!ZipStream.IsFinished)
- {
- ZipStream.Finish();
- }
- }
-
- public void Close()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
- public void Dispose()
- {
- this.Close();
- }
-
- protected virtual void Dispose(bool disposing)
- {
- if (!disposed)
- {
- if (disposing)
- {
- if (ZipStream != null)
- ZipStream.Dispose();
- }
- }
-
- disposed = true;
- }
- }
- }
接下来,写一个SPExtensions.cs的类,用于向Microsoft.SharePoint对象添加一些扩展方法。这个类基本就是添加一些简单的方法到SPListItem类和SPList类。针对SPListItem类,我添加了一个方法用于判断是否SPListItem实例是一个文件夹,SPList类用于判断这个列表仅仅是一个文档。
SPExtensions的代码如下:
SPExtensions.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.CompilerServices;
- using Microsoft.SharePoint;
-
- namespace DeviantPoint.DownloadZip
- {
- public static class SPExtensions
- {
- public static bool IsFolder(this SPListItem item)
- {
- return (item.Folder != null);
- }
-
- public static bool IsDocumentLibrary(this SPList list)
- {
- return (list.BaseType == SPBaseType.DocumentLibrary);
- }
- }
- }
接下来要做的是添加一个“SharePoint映射文件夹”到项目,映射到Layouts目录(位于SharePoint根目录下)。在添加完SharePoint的“Layouts”映射文件夹后,我添加了一个SharePoint 2010 应该程序页DownloadZip.aspx到DeviantPoint.DownloadZip子文件夹,该页面负责处理来自客户端的请求并生成对应的zip文件返回到客户端。它的功能如同已知的在SharePoint 2010 Ribbon菜单中的“下载副本”的功能。来自客户端的POST请求会被发送到我的DownloadZip.aspx页面,然后这个页面把一些文档进行打包压缩到一个zip文件然后发送到客户端浏览器。这个页面需要两个参数:
• sourceUrl –完整的文档(文件夹,包含子文件夹),请求的来源
• itemIDs – 列表项ID ,用分号隔开的一个SPListItem ID项目集,作为zip文件的一部分。注意:一个文件夹也有IDs如果一个文件夹被选中的话,自然其ID值也会被传递。
该应用程序页的基本功能是根据传递过来的id检索对应的SharePoint中的文档项,使用ZipBuilder 类把检索到的文档打包成一个zip文件。如果是一个文件夹的id,则会创建文件夹(最终保存到zip文件中),并依次检索文件夹下的所有文件
下面是DownloadZip.aspx 应用程序页的后置代码 :
- using System;
- using System.IO;
- using System.Web;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.WebControls;
- using ICSharpCode.SharpZipLib.Zip;
-
- namespace DeviantPoint.DownloadZip.Layouts.DeviantPoint.DownloadZip
- {
- public partial class DownloadZip : LayoutsPageBase
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string fullDocLibSourceUrl = Request.Params["sourceUrl"];
- if (string.IsNullOrEmpty(fullDocLibSourceUrl)) return;
-
- string docLibUrl = fullDocLibSourceUrl.Replace(SPContext.Current.Site.Url, "");
-
- SPList list = SPContext.Current.Web.GetList(docLibUrl);
- if (!list.IsDocumentLibrary()) return;
-
- string pItemIds = Request.Params["itemIDs"];
- if (string.IsNullOrEmpty(pItemIds)) return;
-
- SPDocumentLibrary library = (SPDocumentLibrary)list;
-
- string[] sItemIds = pItemIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- int[] itemsIDs = new int[sItemIds.Length];
- for (int i = 0; i < sItemIds.Length; i++)
- {
- itemsIDs[i] = Convert.ToInt32(sItemIds[i]);
- }
-
- if (itemsIDs.Length > 0)
- {
- using (MemoryStream ms = new MemoryStream())
- {
- using (ZipFileBuilder builder = new ZipFileBuilder(ms))
- {
- foreach (int id in itemsIDs)
- {
- SPListItem item = library.GetItemById(id);
- if (item.IsFolder())
- AddFolder(builder, item.Folder, string.Empty);
- else
- AddFile(builder, item.File, string.Empty);
- }
-
- builder.Finish();
- WriteStreamToResponse(ms);
- }
- }
- }
-
- }
-
- private static void AddFile(ZipFileBuilder builder, SPFile file, string folder)
- {
- using (Stream fileStream = file.OpenBinaryStream())
- {
- builder.Add(folder + "\\" + file.Name, fileStream);
- fileStream.Close();
- }
- }
-
- private void AddFolder(ZipFileBuilder builder, SPFolder folder, string parentFolder)
- {
- string folderPath = parentFolder == string.Empty ? folder.Name : parentFolder + "\\" + folder.Name;
- builder.AddDirectory(folderPath);
-
- foreach (SPFile file in folder.Files)
- {
- AddFile(builder, file, folderPath);
- }
-
- foreach (SPFolder subFolder in folder.SubFolders)
- {
- AddFolder(builder, subFolder, folderPath);
- }
- }
-
- private void WriteStreamToResponse(MemoryStream ms)
- {
- if (ms.Length > 0)
- {
- string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
- Response.Clear();
- Response.ClearHeaders();
- Response.ClearContent();
- Response.AddHeader("Content-Length", ms.Length.ToString());
- Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
- Response.ContentType = "application/octet-stream";
-
- byte[] buffer = new byte[65536];
- ms.Position = 0;
- int num;
- do
- {
- num = ms.Read(buffer, 0, buffer.Length);
- Response.OutputStream.Write(buffer, 0, num);
- }
-
- while (num > 0);
-
- Response.Flush();
- }
- }
- }
- }
在创建完应用程序页后,我添加一个SharePoint 2010 空白元素DownloadZip到项目中,打开Elements.xml
代码如下
- <?xml version="1.0" encoding="utf-8"?>
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
- <CustomAction Id="DeviantPoint.DownloadZip" Location="CommandUI.Ribbon">
- <CommandUIExtension>
- <CommandUIDefinitions>
- <CommandUIDefinition Location="Ribbon.Documents.Copies.Controls._children">
- <Button Id="Ribbon.Documents.Copies.DownloadZip"
- Command="DownloadZip"
- Sequence="15"
- Image16by16="/_layouts/images/DeviantPoint.DownloadZip/zip_16x16.png"
- Image32by32="/_layouts/images/DeviantPoint.DownloadZip/zip_32x32.png"
- Description="Download zip" LabelText="Download as Zip"
- TemplateAlias="o1"/>
- </CommandUIDefinition>
- </CommandUIDefinitions>
- <CommandUIHandlers>
- <CommandUIHandler
- Command="DownloadZip"
- CommandAction="javascript:downloadZip();"
- EnabledScript="javascript:enable();"/>
- </CommandUIHandlers>
- </CommandUIExtension>
- </CustomAction>
- <CustomAction Id="Ribbon.Library.Actions.Scripts"
- Location="ScriptLink"
- ScriptSrc="/_layouts/DeviantPoint.DownloadZip/CustomActions.js" />
- </Elements>
对此CommandUIDefinition, 我设置 Location attribute 为"Ribbon.Documents.Copies.Controls._children”. 我还希望它能够显示在“下载副本”功能的右边,因此需要设置按钮元素序列号属性,设置为15(这个“下载副本按钮的序列号为10,发送到按钮的序列号为20,自然我需要设置它的序列号为它们之间),想要知道每个功能按钮的序列号你可能查看C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\GLOBAL\XML\CMDUI.xml 文件。我同时也指定了功能按钮的图像(创建一个SharePoint的“Images”映射文件夹),同时指定TemplateAlias为“o1”以便我的图标显示的大些,如同“下载副本”的一样。我也定义按钮的command处理事件在Elements.xml文件中通过添加一个CommandUIHandler元素。这CommandAction 属性用于指定按钮被期望的动作,EnabledScript属性用于决定是否按钮功能被启用。这两个属性值都指向一个javascript函数(放在一个单独的文件中,稍后讨论)因为我使用了一个单独的javascript文件,我还必须在Elements文件中添加另一个CustomAction元素,用以指名我的javascript文件的位置。
最后,创建CustomActions.js文件,这个文件用于定义ribbon按钮的功能/行为。enable()函数用于决定按钮是否可用。如果至少有一个文档项被选中的话,那么我的按钮就是可用的。 downloadZip() 函数用于开始下载过程
如下为CustomActions.js 的源码:
- function enable() {
- var items = SP.ListOperation.Selection.getSelectedItems();
- var itemCount = CountDictionary(items);
- return (itemCount > 0);
-
- }
-
- function downloadZip() {
-
- var context = SP.ClientContext.get_current();
- this.site = context.get_site();
- this.web = context.get_web();
- context.load(this.site);
- context.load(this.web);
- context.executeQueryAsync(
- Function.createDelegate(this, this.onQuerySucceeded),
- Function.createDelegate(this, this.onQueryFailed)
- );
- }
-
- function onQuerySucceeded() {
-
- var items = SP.ListOperation.Selection.getSelectedItems();
- var itemCount = CountDictionary(items);
-
- if (itemCount == 0) return;
-
- var ids = "";
- for (var i = 0; i < itemCount; i++) {
- ids += items[i].id + ";";
- }
-
-
- var form = document.createElement("form");
- form.setAttribute("method", "post");
- form.setAttribute("action", this.site.get_url() + this.web.get_serverRelativeUrl() + "/_layouts/deviantpoint.downloadzip/downloadzip.aspx");
-
- var hfSourceUrl = document.createElement("input");
- hfSourceUrl.setAttribute("type", "hidden");
- hfSourceUrl.setAttribute("name", "sourceUrl");
- hfSourceUrl.setAttribute("value", location.href);
- form.appendChild(hfSourceUrl);
-
- var hfItemIds = document.createElement("input")
- hfItemIds.setAttribute("type", "hidden");
- hfItemIds.setAttribute("name", "itemIDs");
- hfItemIds.setAttribute("value", ids);
- form.appendChild(hfItemIds);
-
- document.body.appendChild(form);
- form.submit();
- }
-
- function onQueryFailed(sender, args) {
- this.statusID = SP.UI.Status.addStatus("Download as Zip:",
- "Downloading Failed: " + args.get_message() + " <a href='#' onclick='javascript:closeStatus();return false;'>Close</a>.", true);
- SP.UI.Status.setStatusPriColor(this.statusID, "red");
- }
-
- function closeStatus() {
- SP.UI.Status.removeStatus(this.statusID);
- }
到此结束,生成并部署,即可实现文档(文件夹)的批量下载为压缩包。
如果你出现“找不到文件的错误”,请看下一篇文章,这是因为没有把ICSharpCode.SharpZipLib.dll注册到GAC的原因。
本文翻译于:http://www.deviantpoint.com/post/2010/05/08/SharePoint-2010-Download-as-Zip-File-Custom-Ribbon-Action.aspx