显示/隐藏 Site Actions 菜单项

SharePoint 2010 使用“Site Actions" 菜单替换了原来的MOSS 2007 Site Settings”菜单,它默认的子菜单项有很多,比如“New Page”,“More Options”,“View All Site Content”等等,当然我们也可以通过Feature创建自己的Site Actions 菜单项,这不是我们所有讨论的问题,本文主要讨论如何通过简单的方式(用户权限/Javascript脚本)来显示/隐藏某些Site Actions菜单项。 

Site Actions 菜单项构成 

首先我们来了解一下SharePoint 默认Site Actions 菜单项,我们可以通过查看母版页的源代码看到这些菜单项,这里简单贴一下部分代码:

查看Site Action 代码
<asp:Content ID="Content1" ContentPlaceHolderID="SPNavigation" runat="server">
<sharepoint:uiversionedcontent uiversion="4" runat="server">
<ContentTemplate>

<div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle">
<div id="s4-ribboncont">
<SharePoint:SPRibbon
runat="server"
CssFile
= ""
PlaceholderElementId
="RibbonContainer"
FixedPositioningEnabled
="true"
PermissionsString
="EditListItems, AddAndCustomizePages"
PermissionMode
="Any"
ApplyPermissionsToRibbonOnly
="false">
<SharePoint:SPRibbonPeripheralContent
runat="server"
Location
="TabRowLeft"
CssClass
="ms-siteactionscontainer s4-notdlg">

<span class="ms-siteactionsmenu" id="siteactiontd">

<SharePoint:SiteActions
runat="server"
accesskey
="<%$Resources:wss,tb_SiteActions_AK%>"
id
="SiteActionsMenuMain"
PrefixHtml
=""
SuffixHtml
=""
MenuNotVisibleHtml
="&amp;nbsp;">
<CustomTemplate>
<SharePoint:FeatureMenuTemplate runat="server"
FeatureScope
="Site"
Location
="Microsoft.SharePoint.StandardMenu"
GroupId
="SiteActions"
UseShortId
="true">

<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_EditPage"
Text
="<%$Resources:wss,siteactions_editpage%>"
Description
=
"<%$Resources:wss,siteactions_editpagedescriptionv4%>"

ImageUrl
="/_layouts/images/ActionsEditPage.png"
MenuGroupId
="100"
Sequence
="110"
ClientOnClickNavigateUrl
=
"javascript:ChangeLayoutMode(false);"
/>
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_TakeOffline"
Text
="<%$Resources:wss,siteactions_takeoffline%>"
Description
=
"<%$Resources:wss,siteactions_takeofflinedescription%>"

ImageUrl
="/_layouts/images/connecttospworkspace32.png"
MenuGroupId
="100"
Sequence
="120" />

<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_CreatePage"
Text
="<%$Resources:wss,siteactions_createpage%>"
Description
="<%$Resources:wss,siteactions_createpagedesc%>"
ImageUrl
="/_layouts/images/NewContentPageHH.png"
MenuGroupId
="200"
Sequence
="210"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="if (LaunchCreateHandler('Page')) {
OpenCreateWebPageDialog('~site/_layouts/createwebpage.aspx') }"

PermissionsString
="AddListItems, EditListItems"
PermissionMode
="All" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_CreateDocLib"
Text
="<%$Resources:wss,siteactions_createdoclib%>"
Description
="<%$Resources:wss,siteactions_createdoclibdesc%>"
ImageUrl
="/_layouts/images/NewDocLibHH.png"
MenuGroupId
="200"
Sequence
="220"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="if (LaunchCreateHandler('DocLib')) {
GoToPage('~site/_layouts/new.aspx?FeatureId={00bfea71-e717-4e80-aa17-d0c71b360101}
&amp;ListTemplate=101') }"

PermissionsString
="ManageLists"
PermissionMode
="Any"
VisibilityFeatureId
="00BFEA71-E717-4E80-AA17-D0C71B360101" />
<SharePoint:MenuItemTemplate runat="server" id="MenuItem_CreateSite"
Text
="<%$Resources:wss,siteactions_createsite%>"
Description
="<%$Resources:wss,siteactions_createsitedesc%>"
ImageUrl
="/_layouts/images/newweb32.png"
MenuGroupId
="200"
Sequence
="230"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="if (LaunchCreateHandler('Site')) {
STSNavigate('~site/_layouts/newsbweb.aspx') }"

PermissionsString
="ManageSubwebs"
PermissionMode
="Any" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_Create"
Text
="<%$Resources:wss,siteactions_create%>"
Description
="<%$Resources:wss,siteactions_createdesc%>"
MenuGroupId
="200"
Sequence
="240"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="if (LaunchCreateHandler('All')) {
STSNavigate('~site/_layouts/create.aspx') }"

PermissionsString
="ManageLists, ManageSubwebs"
PermissionMode
="Any" />

<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_ViewAllSiteContents"
Text
="<%$Resources:wss,quiklnch_allcontent%>"
Description
="<%$Resources:wss,siteactions_allcontentdescription%>"
ImageUrl
="/_layouts/images/allcontent32.png"
MenuGroupId
="300"
Sequence
="302"
UseShortId
="true"
ClientOnClickNavigateUrl
="~site/_layouts/viewlsts.aspx"
PermissionsString
="ViewFormPages"
PermissionMode
="Any" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_EditSite"
Text
="<%$Resources:wss,siteactions_editsite%>"
Description
="<%$Resources:wss,siteactions_editsitedescription%>"
ImageUrl
="/_layouts/images/SharePointDesigner32.png"
MenuGroupId
="300"
Sequence
="304"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="EditInSPD('~site/',true);"
PermissionsString
="AddAndCustomizePages"
PermissionMode
="Any" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_SiteWorkflow"
Text
="<%$Resources:wss,siteactions_siteworkflow%>"
Description
="<%$Resources:wss,siteactions_siteworkflowescription%>"
ImageUrl
="/_layouts/images/ManageWorkflow32.png"
MenuGroupId
="300"
Sequence
="305"
UseShortId
="true"
ClientOnClickNavigateUrl
="~site/_layouts/workflow.aspx"
PermissionsString
="EditListItems, AddAndCustomizePages"
PermissionMode
="Any" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_SitePermissions"
Text
="<%$Resources:wss,people_sitepermissions%>"
Description
="<%$Resources:wss,siteactions_sitepermissiondescriptionv4%>"
ImageUrl
="/_layouts/images/Permissions32.png"
MenuGroupId
="300"
Sequence
="310"
UseShortId
="true"
ClientOnClickNavigateUrl
="~site/_layouts/user.aspx"
PermissionsString
="EnumeratePermissions"
PermissionMode
="Any" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_Settings"
Text
="<%$Resources:wss,settings_pagetitle%>"
Description
="<%$Resources:wss,siteactions_sitesettingsdescriptionv4%>"
ImageUrl
="/_layouts/images/settingsIcon.png"
MenuGroupId
="300"
Sequence
="320"
UseShortId
="true"
ClientOnClickNavigateUrl
="~site/_layouts/settings.aspx"
PermissionsString
="EnumeratePermissions,ManageWeb,ManageSubwebs,
AddAndCustomizePages,ApplyThemeAndBorder,ManageAlerts,
ManageLists,ViewUsageData"

PermissionMode
="Any" />
<SharePoint:MenuItemTemplate
runat="server"
id
="MenuItem_CommitNewUI"
Text
="<%$Resources:wss,siteactions_commitnewui%>"
Description
="<%$Resources:wss,siteactions_commitnewuidescription%>"
ImageUrl
="/_layouts/images/visualupgradehh.png"
MenuGroupId
="300"
Sequence
="330"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="GoToPage('~site/_layouts/prjsetng.aspx')"
PermissionsString
="ManageWeb"
PermissionMode
="Any"
ShowOnlyIfUIVersionConfigurationEnabled
="true" />

</SharePoint:FeatureMenuTemplate>
</CustomTemplate>

</SharePoint:SiteActions></span>

</SharePoint:SPRibbonPeripheralContent>

</SharePoint:SPRibbon>
</div>
</div>
<div id="notificationArea" class="s4-noti">
</div>
<SharePoint:DelegateControl runat="server" ControlId="PublishingConsole">
</SharePoint:DelegateControl>
<div>
<WebPartPages:WebPartAdder ID="WebPartAdder" runat="server" />
</div>
</ContentTemplate>
</sharepoint:uiversionedcontent>
</asp:Content>


从代码中我们可以看出,Site Actions 的菜单项是通过一个个
<SharePoint:MenuItemTemplate />来是实现的,而且我们发现MenuItemTemplate的两个重要的属性PermissionsStringPermissionMode,通过对这两个属性的设置可以简单实现菜单的显示和隐藏。首先我们来了解下官方对这两个属性的定义。

PermissionsString:设置和获取用户权限,比如“ADDLIstItem”,“Manage Site” (用户对站点的权限)

PermissionMode: 只有两个值Any or ALL (Any:只要当前用户满足PermissionString其中一个权限就显示菜单,ALL:必须都满足才显示菜单)。

既然了解了这个两个属性,你就已经明白了为啥有的用户能看到Site Settings菜单,有的用户却不能。

顺便贴一下微软官方PermissionsString所有值:

List Permissions Site Permissions Personal Permissions
ManageLists
CancelCheckout
AddListItems
EditListItems
DeleteListItems
ViewListItems
ApproveItems
OpenItems
ViewVersions
DeleteVersions
CreateAlerts
ViewFormPages
ManagePermissions
ViewUsageData
ManageSubwebs
ManageWeb
AddAndCustomizePages
ApplyThemeAndBorder
ApplyStyleSheets
CreateGroups
BrowseDirectories
CreateSSCSite
ViewPages
EnumeratePermissions
BrowseUserInfo
ManageAlerts
UseRemoteAPIs
UseClientIntegration
Open
EditMyUserInfo
ManagePersonalViews
AddDelPrivateWebParts
UpdatePersonalWebParts
 

根据用户权限显示/隐藏菜单项

当开发自定义菜单项时,只要设置相应的PermissionString和PermissionMode就可以控制菜单项的显示或者隐藏。比如我们自定义开发一个菜单项,只有用户有EditList权限才显示,对应的XML应该是:

  

<SharePoint:MenuItemTemplate 
runat="server"
id
="MenuItem_CommitNewUI"
Text
="<%$Resources:wss,siteactions_commitnewui%>"
Description
="<%$Resources:wss,siteactions_commitnewuidescription%>"
ImageUrl
="/_layouts/images/visualupgradehh.png"
MenuGroupId
="300"
Sequence
="330"
UseShortId
="true"
ClientOnClickScriptContainingPrefixedUrl
="GoToPage('~site/_layouts/prjsetng.aspx')"
PermissionsString
="EditListItems"
PermissionMode
="Any"
ShowOnlyIfUIVersionConfigurationEnabled
="true" />


上面的方法解决了如果根据权限显示/隐藏菜单项的问题,那么我们有什么方法不通过权限来隐藏菜单项吗?比如我们对所有的用户都隐藏“View All Site Content”菜单项。答案是肯定的。

 

通过脚本隐藏Site Actions 菜单项

平时在使用SharePoint的时候,我们发现所有的用户都能看到“View All Site Content”菜单,为了安全起见,比如我们不希望用户看到所有站点的内容,我们就必须隐藏“View All Site Content"菜单。

最简单的方式,就是通过SharePoint Designer,直接编辑母版页,删除这个选项。这个不推荐,因为将来可能还需要显示。

第二种方式:通过脚本隐藏:

我们通过一个简单的实例来介绍一个这个方案: 创建一个空的SharePoint项目,添加一个新的Module,用下面代码替换掉Elements.xml中的 <Module Name="Module1">......</Module>,替换代码:

隐藏View ALl site content
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Location="ScriptLink"
ScriptBlock
="

function loadjQueryScripts(src) {

var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
head.appendChild(script);
}

function hideSiteActions() {
var menuItems = document.getElementsByTagName('ie:menuitem');
for (var key in menuItems)
{
if (key.endsWith('ViewAllSiteContents'))
menuItems[key].hidden = true;
}
}

_spBodyOnLoadFunctionNames.push('hideSiteActions');
"

Sequence
="10001">

</CustomAction>
</Elements>

替换好之后直接打包部署,就完成了所有的开发工作,非常简单。

刷新下页面,你就可以看倒”View All Site Content“菜单已经被隐藏了。在结束之前我们来分析一下上面的代码中几个重要的方法:

loadjQueryScripts
这个方法先不用看,这个方法是下一篇文章”如何通过自定义逻辑隐藏菜单项”使用到的。
hideSiteActions
这段Javascript代码就是遍历了所有Site Actions菜单,然后根据菜单的名字来设置隐藏与否,这里为什么不适用菜单项的ID来查找,是因为我们发现菜单项的ID是动态生成的,比较难于把握。
_spBodyOnLoadFunctionNames.push('hideSiteActions');
这个方法是最重要的方法,它保证了 hideSiteActions()方法在页面load的时候就被触发。

到这里基本介绍了如何根据用户权限设置菜单项的显示/隐藏,已经如果通过脚本显示/隐藏菜单项。
下一篇介绍如何通过自定义逻辑来显示/隐藏菜单项。



posted @ 2012-01-10 14:25  懒牛  阅读(2982)  评论(0编辑  收藏  举报