2010为我们带来了很多新功能,这些新功能使得SharePoint更像是一个Office客户端应用程序,这无疑是令人振奋的。提到Office就不能不提到Office 2007带来的Ribbon,Ribbon的出现彻底改变了以往Office客户端的操作方式,初用时或许会觉得不习惯,但时间长了就会发现确实是非常好用啊。下面我们来看看怎样在SharePoint 2010的Ribbon中添加一个我们自定义的按钮吧。
我们创建一个sharepoint 2010 的项目,如下图所示
填写Features 的标题以及说明
接下来我们编辑Feature1.Template.xml 文件
<xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="697d76a1-f5d0-47da-ae56-12367685d434"
Scope="Site"
Title="CustomRibbonButton"
Description="CustomRibbonButton"
Version="1.0.0.0"
>
<ElementManifests>
<ElementManifest Location="CustomRibbonElement/Elements.xml" />
</ElementManifests>
</Feature>
如下图所示
我们在项目中添加一个空元素,如下图所示
编写Elements.xml文件,如下图所示
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="CustomRibbonButton"
RegistrationId="101"
RegistrationType="List"
Location="CommandUI.Ribbon"
Sequence="5"
Title="Move Documents">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
<Button
Id="Ribbon.Documents.New.TestButton"
Alt="点击我"
Sequence="5"
Command="Test_Button"
Image32by32="/_layouts/images/SharePointTest/FeatureIcon.gif"
Image16by16="/_layouts/images/SharePointTest/FeatureIcon.gif"
LabelText="Move Documents"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="Test_Button"
CommandAction="javascript:alert('Welcome To SharePoint 2010 !');" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>
接下来我们部署该项目,部署成功之后,我们打开网站,进入共享文档。如下图所示
我们发现在管理栏哪里多了一个功能按钮,到此为止,我们的扩展Ribbon功能按钮工作就完成了。
备注说明:
"CustomAction“部分:
“Sequence” 属性,这个属性指定了我们要添加的按钮所在组中的位置。
“Location” 属性,这里指定了这个按钮应该被添加到哪个Tab下的哪个组中,其格式为:Ribbon.[Tab].[Group].Controls._children
"Command” 属性,这个属性将“CommandUIDefinition” 与“CommandUIHandler” 关联起来。
“TemplateAlias” 属性,定义了这个按钮在页面中是32X32还是16X16,“o1”是32,“o2”是16。
“CommandUIHandler” 部分:
“Command” 属性与第19行的“Command” 相对应,以将Button与其操作关联起来。
“CommandAction” 标记,在这个标记中我们将定义按钮被按下时将要执行的Javascript脚本。SharePoint 2010为我们提供了丰富的Javascript API,通过这些API我们可以做更多的事情。