添加功能区按钮Ribbon Button到SP2010特定列表或库

添加功能区按钮到SP2010某一列表或库

        有时候你需要给列表/库的功能区添加新按钮--没有什么比这更简单的了。你只需要新建一个SP项目,添加一个feature,添加一个空白元素,编辑它的Elements.xml文件。
        大多说时候它看起来像这样:
        Elements.xml:
<?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 
<CustomAction
 Id="{41C23DD2-6FEB-4636-AE4F-41B8E2A2D415}"
<strong> RegistrationId="100"</strong>
<strong> RegistrationType="List"</strong>
 Location="CommandUI.Ribbon"
 Sequence="5"
 Title="Custom Lists Button">
 
<CommandUIExtension>
 <CommandUIDefinitions>
 <CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
 <Button
 Id="{09A51440-C3A6-4103-874A-383747042E75}"
 Alt="Custom Lists Button"
 Sequence="5"
 Command="{42844423-382B-4e87-BEC4-34B0601DA98F}"
 Image32by32="/_layouts/images/menulistsettings.gif"
 Image16by16="/_layouts/images/itdcl.gif"
 LabelText="Custom Lists Button"
 TemplateAlias="o1" />
 </CommandUIDefinition>
 </CommandUIDefinitions>
 
<CommandUIHandlers>
 <CommandUIHandler
 Command="{42844423-382B-4e87-BEC4-34B0601DA98F}"
 EnabledScript=""
 CommandAction="javascript: alert('Custom Lists Button!');" />
 </CommandUIHandlers>
 
</CommandUIExtension>
 </CustomAction>
</Elements>
        部署解决方案,激活feature结果是:

        这个方法的缺点是按钮会出现在每个现有自定义列表中,就算你新建自定义列表也会出现。原因是RegistrationId="100"。这里100是自定义列表的类型。如果要给某个列表实例提供按钮的话,你可以使用一下方法:

创建列表定义并分配RegistrationId到列表定义的Type

        首先需要新创建列表定义:

        打开列表定义的Elements.xml,复制Type值:

        粘贴该值到按钮的Elements.xml中RegistrationId:
<CustomAction
Id="{67A1EB46-A49B-4aff-B456-068909C39599}"
RegistrationId="10000"
RegistrationType="List"
Location="CommandUI.Ribbon"
Sequence="5"
Title="List Definition Button">

<CommandUIExtension>
 <CommandUIDefinitions>
 <CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
 <Button
 Id="{3F083F8B-95D6-436b-A130-3EF1E8C04E3C}"
 Alt="List Definition Button"
 Sequence="5"
 Command="{AF90D558-00DA-4ccf-B4F4-169CD9162CD0}"
 Image32by32="/_layouts/images/menulistsettings.gif"
 Image16by16="/_layouts/images/itdcl.gif"
 LabelText="List Definition Button"
 TemplateAlias="o1" />
 </CommandUIDefinition>
 </CommandUIDefinitions>

<CommandUIHandlers>
 <CommandUIHandler
 Command="{AF90D558-00DA-4ccf-B4F4-169CD9162CD0}"
 EnabledScript=""
 CommandAction="javascript: alert('List Definition Button!');" />
 </CommandUIHandlers>

</CommandUIExtension>
 </CustomAction>
         最后,添加列表定义到feature,这样它能随着解决方案一起部署:
        激活feature后,你应该可以给予新建列表定义创建新的列表:

        当你从定义创建列表时,你应该能看到按钮:

        另外,为了阻止用户使用新列表定义创建列表,你可以将定义设置为隐藏(这样用户看不到),然后你可以在FeatureActivated方法的feature event receiver中创建列表实例。

用程序添加按钮

        添加一个event receiver到feature:

        添加如下代码到FeatureActivated方法中:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
 {
 //Depending from the feature scope - can be SPSite
 var web = properties.Feature.Parent as SPWeb;
 
//Get list instance - here You can also create a new list
 var list = web.Lists.TryGetList("RibbonButtonList");
 
if (list != null)
 {
 //Add new user custom action to the list
 var userAction = list.UserCustomActions.Add();
 //Set action's location
 userAction.Location = "CommandUI.Ribbon";
 //This one is Optional as we are adding the same later
 userAction.ImageUrl = "/_layouts/images/menulistsettings.gif";
 //Command body
 userAction.CommandUIExtension = @"<CommandUIExtension>
 <CommandUIDefinitions>
 <CommandUIDefinition Location=""Ribbon.List.Settings.Controls._children"">
 <Button
 Id=""{5557CC45-324E-41bb-9E88-A535DBF1BF6B}""
 Alt=""Programmatically Added Button""
 Sequence=""5""
 Command=""{234F6E9E-80A3-4f70-9683-02067515801E}""
 Image32by32=""/_layouts/images/menulistsettings.gif""
 Image16by16=""/_layouts/images/itdcl.gif""
 LabelText=""Programmatically Added Button""
 TemplateAlias=""o1"" />
 </CommandUIDefinition>
 </CommandUIDefinitions>
 
<CommandUIHandlers>
 <CommandUIHandler
 Command=""{234F6E9E-80A3-4f70-9683-02067515801E}""
 EnabledScript=""""
 CommandAction=""javascript: alert('Programmatically Added Button!');"" />
 </CommandUIHandlers>
 
</CommandUIExtension>";
 
userAction.Update();
 }
 }
        部署解决方案时要确保我们要添加按钮的列表已经存在并激活feature。你应该能在列表功能区看到新按钮:

        这就是全部了,现在你应该可以通过以上方法添加功能区按钮到一个列表了吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值