SharePoint 2010 自定义Ribbon实现文档批量下载为Zip文件[转]

在SharePoint 2010文档库中,结合单选框,在Ribbon中提供了批量处理文档的功能,比如,批量删除、批量签出、批量签入等,但是,很遗憾,没有提供批量下载,默认的只能一个个下载,当选择多个文档时,下载副本就变成了灰色不可用。

在此我们将开发一个Ribbon按钮,实现文档(包括含有文件夹)的批量下载为Zip压缩包的功能。

项目资源结构图

先上传一张项目的资源管理结构

现在开始:打开VS2010,创建一个“空白SharePoint项目”命名DeviantPoint.DownloadZip

创建空白SharePoint项目

指定用于部署的网站

指定用于部署的网站

然后添加一个到ICSharpCode.SharpZipLib.dll(一个.NET类库,用于处理Zip文件)的引用,然后创建一个文件夹Classes用于保存帮助类。

创建类ZipBuilder,该类用于创建zip文件,创建这个帮助类可以使得在别的地方重复使用,而不仅仅针对本项目。
通常,一个类的实例被创建后,你需要转递一个文件流写入zip文件。它可以是任意类型文件流,二进制流,存储流等等。在这个ZipBuilder类中允许你添加文件和文件夹,最终输出到zip文件。因为它要处理文件流,所以该类实现了IDisposable接口。
如下为ZipBuilder类的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using ICSharpCode.SharpZipLib.Zip;
  7. using ICSharpCode.SharpZipLib.Core;
  8. namespace DeviantPoint.DownloadZip
  9. {
  10. public class ZipFileBuilder : IDisposable
  11. {
  12. private bool disposed = false;
  13. ZipOutputStream zipStream = null;
  14. protected ZipOutputStream ZipStream
  15. {
  16. get { return zipStream; }
  17. }
  18. ZipEntryFactory factory = null;
  19. private ZipEntryFactory Factory
  20. {
  21. get { return factory; }
  22. }
  23. public ZipFileBuilder(Stream outStream)
  24. {
  25. zipStream = new ZipOutputStream(outStream);
  26. zipStream.SetLevel(9); //best compression
  27. factory = new ZipEntryFactory(DateTime.Now);
  28. }
  29. public void Add(string fileName, Stream fileStream)
  30. {
  31. //create a new zip entry
  32. ZipEntry entry = factory.MakeFileEntry(fileName);
  33. entry.DateTime = DateTime.Now;
  34. ZipStream.PutNextEntry(entry);
  35. byte[] buffer = new byte[65536];
  36. int sourceBytes;
  37. do
  38. {
  39. sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
  40. ZipStream.Write(buffer, 0, sourceBytes);
  41. }
  42. while (sourceBytes > 0);
  43. }
  44. public void AddDirectory(string directoryName)
  45. {
  46. ZipEntry entry = factory.MakeDirectoryEntry(directoryName);
  47. ZipStream.PutNextEntry(entry);
  48. }
  49. public void Finish()
  50. {
  51. if (!ZipStream.IsFinished)
  52. {
  53. ZipStream.Finish();
  54. }
  55. }
  56. public void Close()
  57. {
  58. Dispose(true);
  59. GC.SuppressFinalize(this);
  60. }
  61. public void Dispose()
  62. {
  63. this.Close();
  64. }
  65. protected virtual void Dispose(bool disposing)
  66. {
  67. if (!disposed)
  68. {
  69. if (disposing)
  70. {
  71. if (ZipStream != null)
  72. ZipStream.Dispose();
  73. }
  74. }
  75. disposed = true;
  76. }
  77. }
  78. }

接下来,写一个SPExtensions.cs的类,用于向Microsoft.SharePoint对象添加一些扩展方法。这个类基本就是添加一些简单的方法到SPListItem类和SPList类。针对SPListItem类,我添加了一个方法用于判断是否SPListItem实例是一个文件夹,SPList类用于判断这个列表仅仅是一个文档。

SPExtensions的代码如下:
SPExtensions.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.CompilerServices;
  6. using Microsoft.SharePoint;
  7. namespace DeviantPoint.DownloadZip
  8. {
  9. public static class SPExtensions
  10. {
  11. public static bool IsFolder(this SPListItem item)
  12. {
  13. return (item.Folder != null);
  14. }
  15. public static bool IsDocumentLibrary(this SPList list)
  16. {
  17. return (list.BaseType == SPBaseType.DocumentLibrary);
  18. }
  19. }
  20. }

接下来要做的是添加一个“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 应用程序页的后置代码 :

 

  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using Microsoft.SharePoint;
  5. using Microsoft.SharePoint.WebControls;
  6. using ICSharpCode.SharpZipLib.Zip;
  7. namespace DeviantPoint.DownloadZip.Layouts.DeviantPoint.DownloadZip
  8. {
  9. public partial class DownloadZip : LayoutsPageBase
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. string fullDocLibSourceUrl = Request.Params["sourceUrl"];
  14. if (string.IsNullOrEmpty(fullDocLibSourceUrl)) return;
  15. string docLibUrl = fullDocLibSourceUrl.Replace(SPContext.Current.Site.Url, "");
  16. SPList list = SPContext.Current.Web.GetList(docLibUrl);
  17. if (!list.IsDocumentLibrary()) return;
  18. string pItemIds = Request.Params["itemIDs"];
  19. if (string.IsNullOrEmpty(pItemIds)) return;
  20. SPDocumentLibrary library = (SPDocumentLibrary)list;
  21. string[] sItemIds = pItemIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  22. int[] itemsIDs = new int[sItemIds.Length];
  23. for (int i = 0; i < sItemIds.Length; i++)
  24. {
  25. itemsIDs[i] = Convert.ToInt32(sItemIds[i]);
  26. }
  27. if (itemsIDs.Length > 0)
  28. {
  29. using (MemoryStream ms = new MemoryStream())
  30. {
  31. using (ZipFileBuilder builder = new ZipFileBuilder(ms))
  32. {
  33. foreach (int id in itemsIDs)
  34. {
  35. SPListItem item = library.GetItemById(id);
  36. if (item.IsFolder())
  37. AddFolder(builder, item.Folder, string.Empty);
  38. else
  39. AddFile(builder, item.File, string.Empty);
  40. }
  41. builder.Finish();
  42. WriteStreamToResponse(ms);
  43. }
  44. }
  45. }
  46. }
  47. private static void AddFile(ZipFileBuilder builder, SPFile file, string folder)
  48. {
  49. using (Stream fileStream = file.OpenBinaryStream())
  50. {
  51. builder.Add(folder + "\\" + file.Name, fileStream);
  52. fileStream.Close();
  53. }
  54. }
  55. private void AddFolder(ZipFileBuilder builder, SPFolder folder, string parentFolder)
  56. {
  57. string folderPath = parentFolder == string.Empty ? folder.Name : parentFolder + "\\" + folder.Name;
  58. builder.AddDirectory(folderPath);
  59. foreach (SPFile file in folder.Files)
  60. {
  61. AddFile(builder, file, folderPath);
  62. }
  63. foreach (SPFolder subFolder in folder.SubFolders)
  64. {
  65. AddFolder(builder, subFolder, folderPath);
  66. }
  67. }
  68. private void WriteStreamToResponse(MemoryStream ms)
  69. {
  70. if (ms.Length > 0)
  71. {
  72. string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
  73. Response.Clear();
  74. Response.ClearHeaders();
  75. Response.ClearContent();
  76. Response.AddHeader("Content-Length", ms.Length.ToString());
  77. Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
  78. Response.ContentType = "application/octet-stream";
  79. byte[] buffer = new byte[65536];
  80. ms.Position = 0;
  81. int num;
  82. do
  83. {
  84. num = ms.Read(buffer, 0, buffer.Length);
  85. Response.OutputStream.Write(buffer, 0, num);
  86. }
  87. while (num > 0);
  88. Response.Flush();
  89. }
  90. }
  91. }
  92. }

在创建完应用程序页后,我添加一个SharePoint 2010 空白元素DownloadZip到项目中,打开Elements.xml

代码如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3. <CustomAction Id="DeviantPoint.DownloadZip" Location="CommandUI.Ribbon">
  4. <CommandUIExtension>
  5. <CommandUIDefinitions>
  6. <CommandUIDefinition Location="Ribbon.Documents.Copies.Controls._children">
  7. <Button Id="Ribbon.Documents.Copies.DownloadZip"
  8. Command="DownloadZip"
  9. Sequence="15"
  10. Image16by16="/_layouts/images/DeviantPoint.DownloadZip/zip_16x16.png"
  11. Image32by32="/_layouts/images/DeviantPoint.DownloadZip/zip_32x32.png"
  12. Description="Download zip" LabelText="Download as Zip"
  13. TemplateAlias="o1"/>
  14. </CommandUIDefinition>
  15. </CommandUIDefinitions>
  16. <CommandUIHandlers>
  17. <CommandUIHandler
  18. Command="DownloadZip"
  19. CommandAction="javascript:downloadZip();"
  20. EnabledScript="javascript:enable();"/>
  21. </CommandUIHandlers>
  22. </CommandUIExtension>
  23. </CustomAction>
  24. <CustomAction Id="Ribbon.Library.Actions.Scripts"
  25. Location="ScriptLink"
  26. ScriptSrc="/_layouts/DeviantPoint.DownloadZip/CustomActions.js" />
  27. </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 的源码:

  1. function enable() {
  2. var items = SP.ListOperation.Selection.getSelectedItems();
  3. var itemCount = CountDictionary(items);
  4. return (itemCount > 0);
  5. }
  6. function downloadZip() {
  7. var context = SP.ClientContext.get_current();
  8. this.site = context.get_site();
  9. this.web = context.get_web();
  10. context.load(this.site);
  11. context.load(this.web);
  12. context.executeQueryAsync(
  13. Function.createDelegate(this, this.onQuerySucceeded),
  14. Function.createDelegate(this, this.onQueryFailed)
  15. );
  16. }
  17. function onQuerySucceeded() {
  18. var items = SP.ListOperation.Selection.getSelectedItems();
  19. var itemCount = CountDictionary(items);
  20. if (itemCount == 0) return;
  21. var ids = "";
  22. for (var i = 0; i < itemCount; i++) {
  23. ids += items[i].id + ";";
  24. }
  25. //send a request to the zip aspx page.
  26. var form = document.createElement("form");
  27. form.setAttribute("method", "post");
  28. form.setAttribute("action", this.site.get_url() + this.web.get_serverRelativeUrl() + "/_layouts/deviantpoint.downloadzip/downloadzip.aspx");
  29. var hfSourceUrl = document.createElement("input");
  30. hfSourceUrl.setAttribute("type", "hidden");
  31. hfSourceUrl.setAttribute("name", "sourceUrl");
  32. hfSourceUrl.setAttribute("value", location.href);
  33. form.appendChild(hfSourceUrl);
  34. var hfItemIds = document.createElement("input")
  35. hfItemIds.setAttribute("type", "hidden");
  36. hfItemIds.setAttribute("name", "itemIDs");
  37. hfItemIds.setAttribute("value", ids);
  38. form.appendChild(hfItemIds);
  39. document.body.appendChild(form);
  40. form.submit();
  41. }
  42. function onQueryFailed(sender, args) {
  43. this.statusID = SP.UI.Status.addStatus("Download as Zip:",
  44. "Downloading Failed: " + args.get_message() + " <a href='#' onclick='javascript:closeStatus();return false;'>Close</a>.", true);
  45. SP.UI.Status.setStatusPriColor(this.statusID, "red");
  46. }
  47. function closeStatus() {
  48. SP.UI.Status.removeStatus(this.statusID);
  49. }

到此结束,生成并部署,即可实现文档(文件夹)的批量下载为压缩包。

批量下载为压缩包

如果你出现“找不到文件的错误”,请看下一篇文章,这是因为没有把ICSharpCode.SharpZipLib.dll注册到GAC的原因。

本文翻译于:http://www.deviantpoint.com/post/2010/05/08/SharePoint-2010-Download-as-Zip-File-Custom-Ribbon-Action.aspx

posted @ 2012-02-27 13:38  小师傅  阅读(385)  评论(0编辑  收藏  举报