sharepoint 2013 添加ribbon并单选可用
如果仅仅是添加一个Ribbon菜单的话,那么网上一搜一大把。比如霖雨童鞋的博客http://www.cnblogs.com/jianyus/archive/2013/05/04/3059022.html
但是很多情况是需要控制这个菜单是否可用的。比如我单选的时候才可用,不选或者多选的时候,不可用,这个很少有说的。还是自己来吧。
假设大家都已经参照霖雨童鞋的博客完成了如下代码
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction Id="Ribbon.CustomGroup" RegistrationId="101" RegistrationType="List" Title="MyCustomerRibbon" Location="CommandUI.Ribbon"> <CommandUIExtension> <CommandUIDefinitions> <CommandUIDefinition Location="Ribbon.Documents.New.Controls._children"> <Button Id="Ribbon.Documents.New.Baidu" Command="Baidu" Image32by32="/_layouts/images/test/du.png" LabelText="百度首页" TemplateAlias="o1" /> </CommandUIDefinition> </CommandUIDefinitions> <CommandUIHandlers> <CommandUIHandler Command="Baidu" CommandAction="javascript:window.open ('http://www.baidu.com') ;" /> </CommandUIHandlers> </CommandUIExtension> </CustomAction> </Elements>
这里,我们要在<CommandUIHandler Command="Baidu" CommandAction="javascript:window.open ('http://www.baidu.com') ;" />
</CommandUIHandlers>这个节点加一个EnabledScript属性,并且写一些脚本。完善之后的脚本是这个样子的
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> 4 5 <CustomAction Id="Ribbon.CustomGroup" RegistrationId="101" RegistrationType="List" Title="MyCustomerRibbon" Location="CommandUI.Ribbon"> 6 7 <CommandUIExtension> 8 9 <CommandUIDefinitions> 10 11 <CommandUIDefinition Location="Ribbon.Documents.New.Controls._children"> 12 13 <Button Id="Ribbon.Documents.New.Baidu" 14 15 Command="Baidu" 16 17 Image32by32="/_layouts/images/test/du.png" 18 19 LabelText="百度首页" 20 21 TemplateAlias="o1" /> 22 23 </CommandUIDefinition> 24 25 </CommandUIDefinitions> 26 27 <CommandUIHandlers> 28 29 <CommandUIHandler 30 31 Command="Baidu" 32 33 CommandAction="javascript:window.open ('http://www.baidu.com') ;" 34 35 EnabledScript="javascript: 36 function EnableDisableButton() { 37 var enabled = false; 38 var selectedItems = SP.ListOperation.Selection.getSelectedItems(); 39 40 if (CountDictionary(selectedItems) == 1) { 41 if (this.itemId != selectedItems[0]['id']) { 42 this.itemId = selectedItems[0]['id']; 43 GetItemStatus(this.itemId); 44 } 45 else if (this.StatusValue == false) { 46 enabled = false; 47 } 48 else if ((this.StatusValue == true) && (this.itemId == selectedItems[0]['id'])) { 49 enabled = true; 50 } 51 } 52 else { 53 enabled = false; 54 } 55 56 return enabled; 57 } 58 59 function GetItemStatus(itemId) { 60 var _clientContext = SP.ClientContext.get_current(); 61 var _web = _clientContext.get_web(); 62 var _currentList = _web.get_lists().getById(SP.ListOperation.Selection.getSelectedList()); 63 this.singleItem = _currentList.getItemById(itemId); 64 _clientContext.load(this.singleItem, 'ContentType'); 65 66 _clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed)); 67 } 68 69 function onQuerySucceeded(sender, args) { 70 71 this.StatusValue = true ; 72 73 RefreshCommandUI(); 74 75 } 76 77 function onQueryFailed(sender, args) { 78 alert('Request Failed ' + args.get_message() + '\n' + args.get_stackTrace()); 79 } 80 81 EnableDisableButton(); 82 " /> 83 84 </CommandUIHandlers> 85 86 </CommandUIExtension> 87 88 </CustomAction> 89 90 </Elements>
这个新添加的这段代码我是参照C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\FEATURES\PPSRibbon\elements.xml来的,做了一下修改。至于为什么要这么修改,我不想多说了,自己去研究吧,现在已经凌晨两点半,我想静静。