如何:自定义文档集功能区

上次修改时间: 2010年7月7日

适用范围: SharePoint Server 2010

文档设置功能包括其自身的功能区选项卡。本主题介绍如何向文档设置功能区添加按钮。通过向功能区添加按钮,可创造条件来实现使用功能区执行特定操作的功能。

使用两个 XML 文件来创建功能区按钮。将这些文件放置在 Features 文件夹(默认位置为 Program Files\common files\Microsoft shared\web server extensions\14\template\features 文件夹)下自己的文件夹中。将此文件夹命名为 addzipribbon。Element.xml 和 Feature.xml 这两个文件分别定义功能区自定义元素和功能注册元素。

用户可能想要下载所有的文档集内容,而不是单独地下载每个文件。文档集使用一个导出框架,以便通过使用"发送至"将文档集发送到内容组织者,但是您也可使用导出框架来导出 .zip 文件,以便用户可下载这些文件。通过创建一个使用文档集对象模型的 .dll 文件可完成这一操作。

创建并添加按钮

  • 创建 Element.xml 文件。

    Element.xml

    <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
      <CustomAction
        Id="Ribbon.Documents.DocsetZip"
        Title="Download Document Set as ZIP"
        RegistrationType="ContentType"
        RegistrationId="0x0120D520" 
        Location="CommandUI.Ribbon"
        >
        <CommandUIExtension>
          <CommandUIDefinitions>
            <CommandUIDefinition
              Location="Ribbon.ManageDocumentSet.MDS.Manage.Controls._children"> 
      <Button
    Id="Ribbon.ManageDocumentSet.MDS.Manage.Controls.DownasZip"
                        Sequence="20"
                        Command="Ribbon.ManageDocumentSet.MDS.Manage.DownZip"
                        Alt="Download as ZIP"
                        Image16by16="/_layouts/images/zipfile16x.png"
                        Image32by32="/_layouts/images/zipfile32x.png"
                        LabelText="Download as ZIP file"
        ToolTipTitle="Download as ZIP file"
        ToolTipDescription="Compress the document set and download"
        TemplateAlias="o1"
    />
            </CommandUIDefinition>
          </CommandUIDefinitions>
          <CommandUIHandlers>
            <CommandUIHandler
              Command="Ribbon.ManageDocumentSet.MDS.Manage.DownZip"
              CommandAction="javascript:__doPostBack('DownloadZipDelegateEvent', '')" />
          </CommandUIHandlers>
        </CommandUIExtension>
      </CustomAction>
    </Elements> 
    

    RegistrationID 应是您想要为其显示此功能区按钮的文档集内容类型的 ID。0x0120D520 是基文档集内容类型 ID;将为所有文档集内容类型显示此功能区按钮。

    Location 指定文档集功能区选项卡。

    Image16by16 和 Image32by32 指向位于服务器上的 /_layouts/images 文件夹中的图像。您可指定您自己的图像文件。

    LabelText、ToolTipTitle 和 ToolTipDescription 是用于功能区标题和工具提示的文本字符串。

    CommandAction 指定功能区按钮实际执行的操作。在此示例中,ECMAScript(JavaScript、JScript) 使用 DownloadZipDelegateEvent 中的一个回发事件。

    Feature.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <!-- _lcid="1033" _version="14.0.3427" _dal="1" -->
    <!-- _LocalBinding -->
    <!-- Copyright (c) Microsoft Corporation. All rights reserved. -->
    <Feature xmlns="https://schemas.microsoft.com/sharepoint/" 
    Id="{CC06B38F-B9AB-4227-841C-DC9F438345A7}"
    Title="Add new button"
    Description="Add new button to ribbon" 
    Version="1.0.0.0"
    Scope="Web"
    Hidden="TRUE">
        <ElementManifests>
            <ElementManifest Location="element.xml" />
        </ElementManifests>
    </Feature>
     
    

    向文件夹中添加这些文件之后,通过运行以下命令来激活功能。

    stsadm.exe –o installfeature –filename adddocsetribbon\feature.xml
    stsadm.exe –o activatefeature –filename adddocsetribbon\feature.xml –url "http://<server>"
    

    使用您想要在其中激活您的功能的网站的位置来替换 http://<server>。在安装该功能之后,导航到某个文档集,然后单击文档集功能区选项卡,以查看您的按钮。

将文档集作为 .ZIP 文件进行下载

  1. 使用 Microsoft.Office.DocumentManagement.DocumentSets 对象模型可以创建一个用于获取当前 SPList 项和文档集的委托事件。本示例使用 MyRibbonDelegate 事件。

    using System;
    using Microsoft.SharePoint;
    using Microsoft.Office.DocumentManagement.DocumentSets;
    using System.Web.UI.WebControls;
     
    namespace MyRibbonDelegate
    {
        public class MyRibbonDelegateClass : WebControl
        { 
            protected override void OnLoad(EventArgs e)
            {
                this.EnsureChildControls();
                base.OnLoad(e);
    if (this.Page.Request["__EVENTTARGET"] == "DownloadZipDelegateEvent")
                {
    SPListItem currentItem = SPContext.Current.ListItem;
    DocumentSet currentDocset = DocumentSet.GetDocumentSet(currentItem.Folder);
    this.Page.Response.ContentType = "application/zip";
    currentDocset.Export(this.Page.Response.OutputStream, 1024);
    this.Page.Response.End();
                }
            }
        }
    }
    
    

    当页面请求事件目标相当于委托事件时,代码将执行并获取当前的 SPListItem,同时获取文档集。文档集的 Export() 方法将由页面的 OutputSteam 参数调用和指定,从而获取文档集中的文件。对于导出而言,另外还有一个设置为 1024 的 int 参数。通过以 1024 为增量设置此 int 参数,可以指定可导出的 MB 值。如果文档集中的文件超过 1 GB,则此事件将失败。

  2. 单击您在第一个过程中创建的自定义按钮以调用此代码。此时将出现一个对话框,提示您打开文档集或将文档集保存为一个 .zip 包。

请参阅

概念

文档集

管理文档