首页 > 代码库 > SharePoint开发 - 自定义导航菜单(二)母版页的菜单应用
SharePoint开发 - 自定义导航菜单(二)母版页的菜单应用
接上篇点击打开链接
本篇叙述在母版页中应用之前的配置信息生成菜单,主要涉及到母版页的自定义,并应用了第三方控件库DevExpress,感兴趣的可以看看,这套东西很强大,戳这里
新建一个SharePoint项目,添加一个模块Module。Module会自动附带一个sample.txt的文件和一个Elements.xml的定义文件。我们删除掉没用的sample.txt,修改Elements.xml为如下所示
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="MasterPageModule" Url="_catalogs/masterpage"> <File Path="Module\KBMaster.master" Url="KBMaster.master" Type="GhostableInLibrary"/> </Module> </Elements>
母版页我们命名为KBMaster.master,这里需要说明的是,在2010中,添加项里是没有母版页的,我们可以通过VS创建一个web应用程序,在这个project里写母版页,写完之后再拷贝到Module下面去。母版页修改一般根据SP默认的最简母版页或者默认母版页进行修改(不要直接修改默认的东西),修改后的母版页核心代码如下(主要是添加菜单控件)
<div id="s4-workspace"> <div id="s4-bodyContainer"> <div id="s4-titlerow" class="s4-pr s4-notdlg s4-titlerowhidetitle"> <div class="s4-title s4-lp"> <div class="s4-title-inner"> <table class="s4-titletable" cellspacing="0"> <tbody> <tr> <td class="s4-titlelogo"> <SharePoint:SPLinkButton runat="server" NavigateUrl="~site/" ID="onetidProjectPropertyTitleGraphic"> <SharePoint:SiteLogoImage name="onetidHeadbnnr0" ID="onetidHeadbnnr2" LogoImageUrl="/_layouts/images/siteIcon.png" runat="server" /> </SharePoint:SPLinkButton> </td> <td class="s4-titletext"> <h1 name="onetidProjectPropertyTitle"> <asp:ContentPlaceHolder ID="PlaceHolderSiteName" runat="server"> <SharePoint:SPLinkButton runat="server" NavigateUrl="~site/" ID="onetidProjectPropertyTitle"> <SharePoint:ProjectProperty ID="ProjectProperty1" Property="Title" runat="server" /> </SharePoint:SPLinkButton> </asp:ContentPlaceHolder> </h1> <span id="onetidPageTitleSeparator" class="s4-nothome s4-bcsep s4-titlesep"> <SharePoint:ClusteredDirectionalSeparatorArrow ID="ClusteredDirectionalSeparatorArrow1" runat="server" /> </span> <h2> <asp:ContentPlaceHolder ID="PlaceHolderPageTitleInTitleArea" runat="server" /> </h2> <div class="s4-pagedescription" tabindex="0"> <asp:ContentPlaceHolder ID="PlaceHolderPageDescription" runat="server" /> </div> </td> <td class="s4-socialdata-notif"> <SharePoint:DelegateControl ID="DelegateControl3" ControlId="GlobalSiteLink3" Scope="Farm" runat="server" /> </td> </tr> </tbody> </table> </div> </div> <div id="s4-topheader2" class="s4-pr s4-notdlg"> <a name="startNavigation"></a> <div id="s4-searcharea" class="s4-search s4-rp"> <asp:ContentPlaceHolder ID="PlaceHolderSearchArea" runat="server"> <SharePoint:DelegateControl ID="DelegateControl4" runat="server" ControlId="SmallSearchInputBox" Version="4" /> </asp:ContentPlaceHolder> <span class="s4-help"><span style="height: 17px; width: 17px; position: relative; display: inline-block; overflow: hidden;" class="s4-clust"><a href="#" style="height: 17px; width: 17px; display: inline-block;" onclick="TopHelpButtonClick('HelpHome');return false" accesskey="<%$Resources:wss,multipages_helplink_accesskey%>" id="TopHelpLink" title="<%$Resources:wss,multipages_helplinkalt_text%>" runat="server"> <img id="Img1" src="/_layouts/images/fgimg.png" alt="<%$Resources:wss,multipages_helplinkalt_text%>" style="left: -0px !important; top: -309px !important; position: absolute;" align="absmiddle" border="0" runat="server" /> </a></span></span> </div> <div class="s4-rp s4-app"> </div> <div class="s4-lp s4-toplinks"> <asp:ContentPlaceHolder ID="PlaceHolderTopNavBar" runat="server"> <h2 class="ms-hidden"> <SharePoint:EncodedLiteral ID="EncodedLiteral5" runat="server" Text="<%$Resources:wss,topnav_pagetitle%>" EncodeMethod="HtmlEncode" /></h2> <asp:ContentPlaceHolder ID="PlaceHolderHorizontalNav" runat="server"> <SharePoint:AspMenu ID="TopNavigationMenuV4" runat="server" EnableViewState="false" DataSourceID="topSiteMap" AccessKey="<%$Resources:wss,navigation_accesskey%>" UseSimpleRendering="true" UseSeparateCSS="false" Orientation="Horizontal" StaticDisplayLevels="1" MaximumDynamicDisplayLevels="2" SkipLinkText="" CssClass="s4-tn" /> <SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource" ID="topNavigationDelegate"> <Template_Controls> <asp:SiteMapDataSource ShowStartingNode="False" SiteMapProvider="SPNavigationProvider" ID="topSiteMap" runat="server" StartingNodeUrl="sid:1002" /> </Template_Controls> </SharePoint:DelegateControl> </asp:ContentPlaceHolder> </asp:ContentPlaceHolder> </div> </div> </div> <div id="s4-statusbarcontainer"> <div id="pageStatusBar" class="s4-status-s1"> </div> </div> <SharePoint:VisualUpgradePreviewStatus ID="VisualUpgradePreviewStatus1" runat="server" /> <div id="s4-mainarea" class="s4-pr s4-widecontentarea"> <div id="s4-leftpanel" class="s4-notdlg"> <div> <dx:ASPxNavBar ID="leftNav" runat="server" Width="100%" Font-Names="微软雅黑" Font-Size="12px" AutoCollapse="True"> <GroupDataFields HeaderImageUrlField="imgurl" NameField="title" NavigateUrlField="url" TextField="title" /> </dx:ASPxNavBar> </div> <div id="s4-leftpanel-content"> <asp:ContentPlaceHolder ID="PlaceHolderLeftNavBarDataSource" runat="server" /> <asp:ContentPlaceHolder ID="PlaceHolderCalendarNavigator" runat="server" /> <asp:ContentPlaceHolder ID="PlaceHolderLeftActions" runat="server"> </asp:ContentPlaceHolder> <asp:ContentPlaceHolder ID="PlaceHolderLeftNavBarTop" runat="server" /> <asp:ContentPlaceHolder ID="PlaceHolderLeftNavBar" runat="server"> <div class="ms-quicklaunchouter"> <div class="ms-quickLaunch"> <SharePoint:UIVersionedContent ID="UIVersionedContent1" runat="server" UIVersion="4"> <contenttemplate> <h2 style="display:inline;" class="ms-hidden"><SharePoint:EncodedLiteral ID="EncodedLiteral6" runat="server" text="<%$Resources:wss,quiklnch_pagetitle%>" EncodeMethod="HtmlEncode"/></h2> </contenttemplate> </SharePoint:UIVersionedContent> <asp:ContentPlaceHolder ID="PlaceHolderQuickLaunchTop" runat="server"> <SharePoint:UIVersionedContent ID="UIVersionedContent2" UIVersion="3" runat="server"> <contenttemplate> <h3 class="ms-standardheader"><label class="ms-hidden"><SharePoint:EncodedLiteral ID="EncodedLiteral7" runat="server" text="<%$Resources:wss,quiklnch_pagetitle%>" EncodeMethod="HtmlEncode"/></label> <Sharepoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControl1" runat="server" PermissionsString="ViewFormPages"> <div class="ms-quicklaunchheader"><SharePoint:SPLinkButton id="idNavLinkViewAll" runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" Text="<%$Resources:wss,quiklnch_allcontent%>" accesskey="<%$Resources:wss,quiklnch_allcontent_AK%>"/></div> </SharePoint:SPSecurityTrimmedControl> </h3> </contenttemplate> </SharePoint:UIVersionedContent> </asp:ContentPlaceHolder> <SharePoint:SPNavigationManager ID="QuickLaunchNavigationManager" runat="server" QuickLaunchControlId="QuickLaunchMenu" ContainedControl="QuickLaunch" EnableViewState="false" CssClass="ms-quicklaunch-navmgr"> <div> <SharePoint:DelegateControl ID="DelegateControl5" runat="server" ControlId="QuickLaunchDataSource"> <Template_Controls> <asp:SiteMapDataSource SiteMapProvider="SPNavigationProvider" ShowStartingNode="False" ID="QuickLaunchSiteMap" StartingNodeUrl="sid:1025" runat="server" /> </Template_Controls> </SharePoint:DelegateControl> <SharePoint:UIVersionedContent ID="UIVersionedContent3" UIVersion="3" runat="server"> <contenttemplate> <SharePoint:AspMenu id="QuickLaunchMenu" runat="server" DataSourceId="QuickLaunchSiteMap" Orientation="Vertical" StaticDisplayLevels="2" ItemWrap="true" MaximumDynamicDisplayLevels="0" StaticSubMenuIndent="0" SkipLinkText="" CssClass="s4-die" > <LevelMenuItemStyles> <asp:MenuItemStyle CssClass="ms-navheader"/> <asp:MenuItemStyle CssClass="ms-navitem"/> </LevelMenuItemStyles> <LevelSubMenuStyles> <asp:SubMenuStyle CssClass="ms-navSubMenu1"/> <asp:SubMenuStyle CssClass="ms-navSubMenu2"/> </LevelSubMenuStyles> <LevelSelectedStyles> <asp:MenuItemStyle CssClass="ms-selectednavheader"/> <asp:MenuItemStyle CssClass="ms-selectednav"/> </LevelSelectedStyles> </SharePoint:AspMenu> </contenttemplate> </SharePoint:UIVersionedContent> <SharePoint:UIVersionedContent ID="UIVersionedContent4" UIVersion="4" runat="server"> <contenttemplate> <SharePoint:AspMenu id="V4QuickLaunchMenu" runat="server" EnableViewState="false" DataSourceId="QuickLaunchSiteMap" UseSimpleRendering="true" UseSeparateCss="false" Orientation="Vertical" StaticDisplayLevels="2" MaximumDynamicDisplayLevels="0" SkipLinkText="" CssClass="s4-ql" /> </contenttemplate> </SharePoint:UIVersionedContent> </div> </SharePoint:SPNavigationManager> <SharePoint:UIVersionedContent ID="UIVersionedContent5" runat="server" UIVersion="3"> <contenttemplate> <Sharepoint:SPNavigationManager id="TreeViewNavigationManager" runat="server" ContainedControl="TreeView" > <table class="ms-navSubMenu1" cellpadding="0" cellspacing="0" border="0"> <tr> <td> <table class="ms-navheader" width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td nowrap="nowrap" id="idSiteHierarchy"> <SharePoint:SPLinkButton runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" id="idNavLinkSiteHierarchy" Text="<%$Resources:wss,treeview_header%>" accesskey="<%$Resources:wss,quiklnch_allcontent_AK%>"/> </td> </tr> </table> </td> </tr> </table> <div class="ms-treeviewouter"> <SharePoint:DelegateControl ID="DelegateControl6" runat="server" ControlId="TreeViewAndDataSource"> <Template_Controls> <SharePoint:SPHierarchyDataSourceControl runat="server" id="TreeViewDataSource" RootContextObject="Web" IncludeDiscussionFolders="true" /> <SharePoint:SPRememberScroll runat="server" id="TreeViewRememberScroll" onscroll="javascript:_spRecordScrollPositions(this);" style="overflow: auto;height: 400px;width: 150px; "> <Sharepoint:SPTreeView id="WebTreeView" runat="server" ShowLines="false" DataSourceId="TreeViewDataSource" ExpandDepth="0" SelectedNodeStyle-CssClass="ms-tvselected" NodeStyle-CssClass="ms-navitem" NodeStyle-HorizontalPadding="2" SkipLinkText="" NodeIndent="12" ExpandImageUrl="/_layouts/images/tvplus.gif" CollapseImageUrl="/_layouts/images/tvminus.gif" NoExpandImageUrl="/_layouts/images/tvblank.gif" > </Sharepoint:SPTreeView> </Sharepoint:SPRememberScroll> </Template_Controls> </SharePoint:DelegateControl> </div> </Sharepoint:SPNavigationManager> </contenttemplate> </SharePoint:UIVersionedContent> <SharePoint:UIVersionedContent ID="UIVersionedContent6" runat="server" UIVersion="4"> <contenttemplate> <Sharepoint:SPNavigationManager id="TreeViewNavigationManagerV4" runat="server" ContainedControl="TreeView" CssClass="s4-treeView" > <SharePoint:SPLinkButton runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" id="idNavLinkSiteHierarchyV4" Text="<%$Resources:wss,treeview_header%>" accesskey="<%$Resources:wss,quiklnch_allcontent_AK%>" CssClass="s4-qlheader" /> <div class="ms-treeviewouter"> <SharePoint:DelegateControl ID="DelegateControl7" runat="server" ControlId="TreeViewAndDataSource"> <Template_Controls> <SharePoint:SPHierarchyDataSourceControl runat="server" id="TreeViewDataSourceV4" RootContextObject="Web" IncludeDiscussionFolders="true" /> <SharePoint:SPRememberScroll runat="server" id="TreeViewRememberScrollV4" onscroll="javascript:_spRecordScrollPositions(this);" style="overflow: auto;height: 400px;width: 155px; "> <Sharepoint:SPTreeView id="WebTreeViewV4" runat="server" ShowLines="false" DataSourceId="TreeViewDataSourceV4" ExpandDepth="0" SelectedNodeStyle-CssClass="ms-tvselected" NodeStyle-CssClass="ms-navitem" SkipLinkText="" NodeIndent="12" ExpandImageUrl="/_layouts/images/tvclosed.png" ExpandImageUrlRtl="/_layouts/images/tvclosedrtl.png" CollapseImageUrl="/_layouts/images/tvopen.png" CollapseImageUrlRtl="/_layouts/images/tvopenrtl.png" NoExpandImageUrl="/_layouts/images/tvblank.gif" > </Sharepoint:SPTreeView> </Sharepoint:SPRememberScroll> </Template_Controls> </SharePoint:DelegateControl> </div> </Sharepoint:SPNavigationManager> </contenttemplate> </SharePoint:UIVersionedContent> <asp:ContentPlaceHolder ID="PlaceHolderQuickLaunchBottom" runat="server"> <SharePoint:UIVersionedContent UIVersion="3" runat="server" ID="PlaceHolderQuickLaunchBottomV3"> <contenttemplate> <table width="100%" cellpadding="0" cellspacing="0" border="0" class="s4-die"> <tr><td> <table class="ms-recyclebin" width="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td nowrap="nowrap"> <SharePoint:SPLinkButton runat="server" NavigateUrl="~site/_layouts/recyclebin.aspx" id="v3idNavLinkRecycleBin" ImageUrl="/_layouts/images/recycbin.gif" Text="<%$Resources:wss,StsDefault_RecycleBin%>" PermissionsString="DeleteListItems" /> </td></tr> </table> </td></tr> </table> </contenttemplate> </SharePoint:UIVersionedContent> <SharePoint:UIVersionedContent UIVersion="4" runat="server" ID="PlaceHolderQuickLaunchBottomV4"> <contenttemplate> <ul class="s4-specialNavLinkList"> <li> <SharePoint:ClusteredSPLinkButton runat="server" NavigateUrl="~site/_layouts/recyclebin.aspx" ImageClass="s4-specialNavIcon" ImageUrl="/_layouts/images/fgimg.png" ImageWidth=16 ImageHeight=16 OffsetX=0 OffsetY=428 id="idNavLinkRecycleBin" Text="<%$Resources:wss,StsDefault_RecycleBin%>" CssClass="s4-rcycl" PermissionsString="DeleteListItems" /> </li> <li> <SharePoint:ClusteredSPLinkButton id="idNavLinkViewAllV4" runat="server" PermissionsString="ViewFormPages" NavigateUrl="~site/_layouts/viewlsts.aspx" ImageClass="s4-specialNavIcon" ImageUrl="/_layouts/images/fgimg.png" ImageWidth=16 ImageHeight=16 OffsetX=0 OffsetY=0 Text="<%$Resources:wss,quiklnch_allcontent_short%>" accesskey="<%$Resources:wss,quiklnch_allcontent_AK%>"/> </li> </ul> </contenttemplate> </SharePoint:UIVersionedContent> </asp:ContentPlaceHolder> </div> </div> </asp:ContentPlaceHolder> </div> </div> <div class="s4-ca s4-ca-dlgNoRibbon" id="MSO_ContentTable"> <div class="s4-die"> <asp:ContentPlaceHolder ID="PlaceHolderPageImage" runat="server" Visible="false" /> <asp:ContentPlaceHolder ID="PlaceHolderTitleLeftBorder" runat="server" Visible="false"> </asp:ContentPlaceHolder> <div id="onetidPageTitleAreaFrame" class='ms-pagetitleareaframe s4-pagetitle'> </div> <asp:ContentPlaceHolder ID="PlaceHolderMiniConsole" runat="server" Visible="false" /> <span class="s4-die"> <asp:ContentPlaceHolder ID="PlaceHolderTitleRightMargin" runat="server" Visible="false" /> </span> <asp:ContentPlaceHolder ID="PlaceHolderTitleAreaSeparator" runat="server" Visible="false" /> <asp:ContentPlaceHolder ID="PlaceHolderNavSpacer" runat="server" Visible="false"> <img src="/_layouts/images/blank.gif" width='138' height='1' alt="" /></asp:ContentPlaceHolder> <asp:ContentPlaceHolder ID="PlaceHolderLeftNavBarBorder" runat="server" Visible="false"> </asp:ContentPlaceHolder> <asp:ContentPlaceHolder ID="PlaceHolderBodyLeftBorder" runat="server" Visible="false"> </asp:ContentPlaceHolder> </div> <SharePoint:VersionedPlaceHolder UIVersion="3" ID="DesignModeConsoleV3" runat="server"> <asp:ContentPlaceHolder ID="WSSDesignConsole" runat="server"> <wssuc:DesignModeConsole ID="IdDesignModeConsole" runat="server" /> </asp:ContentPlaceHolder> </SharePoint:VersionedPlaceHolder> <div class='s4-ba'> <div class='ms-bodyareacell'> <div id="MSO_ContentDiv" runat="server"> <a name="mainContent"></a> <asp:ContentPlaceHolder ID="PlaceHolderMain" runat="server"> </asp:ContentPlaceHolder> </div> </div> </div> <div class="s4-die"> <asp:ContentPlaceHolder ID="PlaceHolderBodyRightMargin" runat="server"> </asp:ContentPlaceHolder> </div> </div> </div> <SharePoint:DeveloperDashboard ID="DeveloperDashboard1" runat="server" /> </div> </div>后台主要做的就是从配置XML里面获取到数据,然后让菜单控件根据这个配置信息生成菜单
public partial class KBMaster : MasterPage { protected void Page_Load(object sender, EventArgs e) { this.prepareTopNav(); this.prepareLeftNav(); } private void prepareLeftNav() { XmlDataSource dataSource = Config.GetDataSource(SPContext.Current.Web, Config.NavType.Left); if (string.IsNullOrEmpty(dataSource.Data)) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "LeftNav", "<script>document.getElementById('s4-leftpanel-content').style.display = 'block';</script>"); } else { int num; bool isAdmin = SPContext.Current.Web.CurrentUser.IsSiteAdmin; if (!isAdmin) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "LeftNav", "<script>document.getElementById('s4-leftpanel-content').style.display = 'none';</script>"); } else { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "LeftNav", "<script>document.getElementById('s4-leftpanel-content').style.display = 'block';</script>"); } this.leftNav.DataSource = dataSource; this.leftNav.DataBind(); for (num = 0; num < this.leftNav.Groups.Count; num++) { this.leftNav.Groups[num].ContentTemplate = new LeftNavGroupTemplate(dataSource.Data, num); DevExpress.Web.ASPxTreeView.ASPxTreeView view = (DevExpress.Web.ASPxTreeView.ASPxTreeView)this.leftNav.Groups[num].FindControl("tv" + num); if (view.SelectedNode != null) { view.ExpandToNode(view.SelectedNode); this.leftNav.Groups[num].Expanded = true; } } } } private void prepareTopNav() { if (this.TopNavigationMenuV4 != null) { try { XmlDataSource dataSource = Config.GetDataSource(SPContext.Current.Web, Config.NavType.Top); if (!string.IsNullOrEmpty(dataSource.Data)) { this.TopNavigationMenuV4.DataSource = dataSource; this.TopNavigationMenuV4.DataSourceID = ""; this.TopNavigationMenuV4.DataBindings.Clear(); MenuItemBinding binding = new MenuItemBinding { DataMember = "SiteMapNode", TextField = "title", ValueField = "title", NavigateUrlField = "url" }; this.TopNavigationMenuV4.DataBindings.Add(binding); } else { this.TopNavigationMenuV4.DataSource = null; this.TopNavigationMenuV4.DataSourceID = "topSiteMap"; } this.TopNavigationMenuV4.DataBind(); } catch (Exception exception) { SPMIPTrace.WriteError("SPMIPNavigation", exception); this.TopNavigationMenuV4.DataSource = null; this.TopNavigationMenuV4.DataSourceID = "topSiteMap"; this.TopNavigationMenuV4.DataBindings.Clear(); this.TopNavigationMenuV4.DataBind(); } } } }篇幅有限,其他代码将在下一篇附上
接下来处理feature的事件,在激活时应用母版页,在取消激活时还原默认母版页
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite parent = (SPSite)properties.Feature.Parent; SPWeb rootWeb = parent.RootWeb; rootWeb.MasterUrl = "/_catalogs/masterpage/KBMaster.master"; rootWeb.Update(); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSite parent = (SPSite)properties.Feature.Parent; SPWeb rootWeb = parent.RootWeb; rootWeb.MasterUrl = "/_catalogs/masterpage/v4.master"; rootWeb.Update(); }
SharePoint开发 - 自定义导航菜单(二)母版页的菜单应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。