对象模型操作网站设置菜单

SharePoint 对象模型操作网站设置菜单
在SharePoint的开发中,经常会有客户提到网站操作上的栏目过多,其实,我们除了可以通过Feature的方式添加,还可以通过服务器端对象模型的方式添加;下面,让我用简单的例子,给大家介绍一下; 1、操作SiteAction,添加“Add by Linyu”,隐藏“Site Settings”,效果如下图: 2、我是通过webpart的方式,修改当前页面的菜单,代码截图: 简单说一下,Visible属性是隐藏属性,我们可以设置菜单是否显示;Sequence是菜单在组中的顺序,如果一个group里有多个菜单,会按照这个排序;MenuGroupId是组的Id,我这里设置成1,所以显示在最上面了; 除了这些,AddMenuItem方法里面,还可以设置菜单的权限,具体可以自行尝试一下; 3、完整的代码,方便大家复制粘贴进行试验,如下:Microsoft.SharePoint.WebControls.SiteActions siteAction = Microsoft.SharePoint.WebControls.SiteActions.GetCurrent(this.Page); if (siteAction != null && ribbonCollection.Length > 0) { string[] RibbonID = RibbonCollection.Split(';'); for (int i = 0; i < RibbonID.Length; i++) { //Hide Site Action siteAction.GetMenuItem(RibbonID[i]).Visible = false; } //Add Site Action siteAction.AddMenuItem("MenuItem_Custom", "Add by Linyu", "", "A test menu", "/_layouts/15/settings.aspx", ""); siteAction.GetMenuItem("MenuItem_Custom").Sequence = 1; siteAction.GetMenuItem("MenuItem_Custom").MenuGroupId = 1; } 4、查看网站设置菜单Id的小技巧,其实SharePoint很多地方的Id,都是可以这样查找的,我简单的试了一下,发现去掉前缀zz11_即可,Site Settings的Id就是"MenuItem_Settings";