在WSSv3中通过Javascript和创建Feature扩展站点的动作菜单

一、示例应用
1.1、定义Site Actionss
      在SharePoint中通过创建一个带CustomAction元素的功能(Feature)添加一个自定义菜单项到默认的站点动作(Site Actions)菜单中。

  Feature.xml

<Feature
Id="AA929AFF-4602-4d7f-A501-B80AC9A4BB52"
Title
="A Sample Feature: Item Auditing"
Description
="A sample feature with an ECB menu item"
Scope
="Site"
xmlns
="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>

   Elements.xml  

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="ApplicationPage1"
GroupId
="SiteActions"
Location
="Microsoft.SharePoint.StandardMenu"
Sequence
="2000"
Title
="Hello World Application Page"
Description
="Getting up and going with inline code"
RequireSiteAdministrator
="True" >
<UrlAction Url="~site/_layouts/Litware/ApplicationPage1.aspx"/>
</CustomAction>
</Elements>

      当添加了RequireSiteAdministrator属性后,如果用户不具有管理权限时SharePoint就不会显示该菜单项了。对于一个在网站集范围内的CustomAction元素,该菜单项就会仅在当前用户是网站集所有者或管理员时才会出现。对于一个在网站范围内的Feature指向的CustomAction元素,这个菜单项只能在具有当前网站管理权限的用户页面中出现。 

 1.2、其他位置的菜单

二、Elements中元素说明
Location、GroupID

三、Javascript定制列表文档库菜单
3.1、定义列表库下拉菜单
Custom_AddListMenuItems方法:

代码
<script language='javascript'>
function Custom_AddListMenuItems(m, ctx){
  var menuOption;
  strDisplayText="Show Hoho";
  strAction="javascript:alert('hoho')";
  strImagePath=ctx.imagesPath+"exptitem.gif";
  menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 550);
  menuOption.id="ID_Custom";
  return true;
}
<script>

3.2、 自定义文档库下拉菜单
Custom_AddDocLibMenuItems方法:

代码
<script type="text/javascript">
function Custom_AddDocLibMenuItems(m, ctx)
{
strDisplayText
= "我的菜单";
var rootMenu = CASubM(m,strDisplayText,"","",500);

strDisplayText
= "我的菜单还是我的菜单";
strAction
= "STSNavigate('http://www.cnblogs.com')";
strImagePath
=ctx.imagesPath+"oisweb.gif";
menuOption
= CAMOpt(rootMenu,strDisplayText,strAction,strImagePath);
menuOption.id
= "ID_MySubMenu";
return false;
}

function Custom_AddDocLibMenuItems(m, ctx)
{
strDisplayText
= "我的菜单";
rootMenuImagePath
=ctx.imagesPath+"REPLY.GIF";
var rootMenu = CASubM(m,strDisplayText,rootMenuImagePath,"",500);
strDisplayText
= "嵌套菜单";
strAction
= "STSNavigate('javascript:windows.alert('windpole!')')";
strImagePath
=ctx.imagesPath+"DOC32.GIF";
menuOption
= CAMOpt(rootMenu,strDisplayText,strAction,strImagePath);
menuOption.id
="ID_TheSubMenu";
strDisplayText
= "我的菜单还是我的菜单";
strAction
= "STSNavigate('http://windpole:7777?ItemIndex="+currentItemID+"&ListGuid="+ctx.listName+"&HttpRootUrl="+ctx.HttpRoot+"')";
strImagePath
=ctx.imagesPath+"Repair.ico";
menuOption
= CAMOpt(m,strDisplayText,strAction,strImagePath);
menuOption.id
= "ID_MyMenu";
return false;
}

function Custom_AddDocLibMenuItems(m, ctx) {
var otype = currentItemFSObjType = GetAttributeFromItemTable(itemTable, "OType", "FSObjType");
if (otype != 1) {
var itemId = GetAttributeFromItemTable(itemTable, "ItemId", "Id");
var listId = ctx.listName;

var action = 'Go_To_Page("' + ctx.HttpRoot + '/_layouts/custom/PAGES/mycustompage.aspx?ListId=' + listId + '&ListItemID=' + itemId +

');';
CAMOpt(m,
'Custom Menu Item', action, '/_layouts/custom/IMAGES/action.gif', '', 110);
CAMSep(m);
}
return false;
}

function Go_To_Page(page) {
window.location
= page;
}
</script>

 3.3、js介绍

这个方法会去掉下面webpart的默认菜单项, 并且添加一个Show Hoho菜单项, 点击后alert
CAMOpt是内置的创建菜单项的方法
第二个参数指定名称, 第三个指定一个action(javascript函数), 第三个指定image(没有写"")
第四个指定image的alt属性(我猜), 第五个是菜单项的顺序编号(我继续猜)
如果Custom_AddListMenuItems返回true, 那么不会创建默认的那些菜单项
如果返回false或者不些return语句, 默认的那些菜单项还在

这样我们就可以几乎完全自己定制这个菜单了
有几个地方目前我没想好怎么解决:
1. 这个只适用于普通列表和文档库
    如果是文档或者列表条目的版本的话, 好像没有接口
    如果列表是Meeting(template=200), 好像也没有接口
2. 通过feature加的菜单项没有提供接口把它去掉
3. 如果页面中有多个webpart, 会比较麻烦(其实也能区分出来,见后文)

创建菜单项还有很多乱七八糟的东西
有几个内置的js文件可以参考一下, 它们都在layouts/2052下
ows.js: 创建菜单项主要是在这里完成的, 在4400行左右的那一大片函数
menu.js: 实际创建出菜单项是在这里, 比如上面那个CAMOpt方法
init.js: 这里面包含context的定义(1308行), 这个context就是Custom_AddListMenuItems参数的那个ctx, 里面有很多可以用到的属性, 比如列表, 视图等(但是是GUID而不是名称), 可以根据这两个东西把页面上的多个webpart区分出来, 另外还包含当前用户的id,查看编辑页面的Url等等

如果需要在创建过程中判断权限, 可以使用HasRights方法
参数是权限的高4字节和低4字节(这个数字我没有核实, 但是我猜和SPBasePermission枚举里的数字是一致的)
权限这个数字是long(8字节)类型的, full permission mask是0x7fffffffffffffff
需要注意的是, 当前用户在这个列表条目上的permission mask是直接写在页面的html里的, 在菜单容器那个table的Perm属性
这个table还包括了很多很多属性, 比如用的是哪一个context, 它的内容类型, 列表条目的id, 它的url, 还有一些我不知道是什么东西的东西
 

四、通过SharePoint Designer定制

 SharePoint 2010

posted on 2010-12-02 23:32  欣静赏悦  阅读(253)  评论(0编辑  收藏  举报