Subscribe
E-mail
Download View Codeplex Project Site
Powered by: newtelligence dasBlog 1.9.7174.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Rich Finn
dasBlog MOSS template
So, The Mossman commented on my post yesterday saying that a little source around the PSMP would be nice, so just for him, here it is. I figured it would be good to build on Andrew Connell's Telerik MOSS Integration Guide, so this example shows how to create your own top-level menu in a custom server-control using the Telerik RadMenu and the PortalSiteMapProvider. I know not everybody will have the license for the Telerik RadMenu, so if you don't, hopefully you'll be able to take this example and pull out of it what you need.
Also, I posted this post on top of the one yesterday, whoops. There's a good lesson on locally saving your postings. Sorry if it's shown up new a couple of times for some of you. Wish I could figure out why...
This solution shows how you could add custom attributes on each RadMenuItem from SPField values in the PublishingPage along with a custom client-side script event handler.
Download the source for the complete control in a text file, if you'd like it.
The menu created will look like the following and will pop a JavaScript alert with the relevant PublishingPage and SPWeb GUIDs when clicked:
protected override void CreateChildControls() { //Get the root publishingweb web for the site PublishingWeb rootWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Site.RootWeb); //Get the URL of the default page in the web string defaultPageUrl = rootWeb.DefaultPage.ServerRelativeUrl; //find the item in the PSMP PortalListItemSiteMapNode smnHome = (PortalListItemSiteMapNode)PortalSiteMapProvider. CurrentNavSiteMapProviderNoEncode.FindSiteMapNode(defaultPageUrl); //initialize the RadMenu RadMenu menu = new RadMenu(); menu.ID = "MainMenu"; menu.RadControlsDir = "/_wpresources/RadMenu.Net2/4.3.2.0__bbe59a8ad3533e68/RadControls"; menu.Skin = "Outlook"; //Set the client-side clicked event handler menu.OnClientItemClicked = "MainMenu_RadMenuItemClicked"; //create the first menu item for the default (home) page RadMenuItem rmiHome = createMenuItem(smnHome); menu.Items.Add(rmiHome); //iterate through each one of the pages and subsites and create menu items for them foreach (SiteMapNode smnTopLevelItem in smnHome.ParentNode.ChildNodes) { RadMenuItem rmiTopLevelItem = createMenuItem(smnTopLevelItem); //if the current sitemap has children, create a submenu for it if (smnTopLevelItem.HasChildNodes) { foreach (SiteMapNode smnChildItem in smnTopLevelItem.ChildNodes) { RadMenuItem rmiChildItem = createMenuItem(smnChildItem); rmiTopLevelItem.Items.Add(rmiChildItem); } } menu.Items.Add(rmiTopLevelItem); } this.Controls.Add(menu); //create the client-side script for the RadMenuItemClicked event handler StringBuilder sbScript = new StringBuilder(); sbScript.Append(Environment.NewLine + "<script language=\"javascript\">"); sbScript.Append(Environment.NewLine + "function MainMenu_RadMenuItemClicked(sender, eventArgs)"); sbScript.Append(Environment.NewLine + "{"); sbScript.Append(Environment.NewLine + "var pageGuid = eventArgs.Item.GetAttribute(\"PageGuid\");"); sbScript.Append(Environment.NewLine + "var webGuid = eventArgs.Item.GetAttribute(\"WebGuid\");"); sbScript.Append(Environment.NewLine + "alert(\"WebGuid:\" + webGuid + \" PageGuid:\" + pageGuid);"); sbScript.Append(Environment.NewLine + "}"); sbScript.Append(Environment.NewLine + "</script>"); this.Page.ClientScript.RegisterStartupScript(typeof(string), "RadMenu_MainMenu_RadMenuItemClicked", sbScript.ToString()); } /// <summary> /// Create a RadMenuItem from a SiteMapNode /// </summary> /// <param name="siteMapNode">SiteMapNode used to create RadMenuItem </param> /// <returns>RadMenuItem</returns> private RadMenuItem createMenuItem(SiteMapNode siteMapNode) { RadMenuItem menuItem = new RadMenuItem(); menuItem.NavigateUrl = siteMapNode.Url; menuItem.Text = siteMapNode.Title; SPListItem listItem = null; //if the SiteMapNode is the default page, it will be a PortalWebSiteMapNode if (siteMapNode is PortalWebSiteMapNode) listItem = getListItemFromPortalSiteMapNode((PortalWebSiteMapNode)siteMapNode); if (siteMapNode is PortalListItemSiteMapNode) listItem = getListItemFromPortalSiteMapNode((PortalListItemSiteMapNode)siteMapNode); //create the custom attributes on the list item if (listItem != null) { menuItem.Attributes.Add("PageGuid", listItem.UniqueId.ToString("N")); menuItem.Attributes.Add("WebGuid", listItem.Web.ID.ToString("N")); menuItem.ToolTip = listItem.Fields["MenuItemToolTip"].GetFieldValueAsText(listItem["MenuItemToolTip"]); } else { menuItem.Attributes.Add("PageGuid", string.Empty); menuItem.Attributes.Add("WebGuid", string.Empty); menuItem.ToolTip = siteMapNode.Title; } return menuItem; } private SPListItem getListItemFromPortalSiteMapNode(PortalListItemSiteMapNode siteMapNode) { SPWeb web = SPContext.Current.Site.AllWebs[siteMapNode.WebId]; PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); SPListItem pageItem = pubWeb.PagesList.Items[siteMapNode.UniqueId]; return pageItem; } private SPListItem getListItemFromPortalSiteMapNode(PortalWebSiteMapNode siteMapNode) { SPWeb web = SPContext.Current.Site.AllWebs[siteMapNode.WebId]; PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); SPListItem pageItem = null; if(pubWeb.DefaultPage != null) pageItem = pubWeb.PagesList.Items[pubWeb.DefaultPage.Item.UniqueId]; return pageItem; }
protected override void CreateChildControls() { //Get the root publishingweb web for the site PublishingWeb rootWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Site.RootWeb); //Get the URL of the default page in the web string defaultPageUrl = rootWeb.DefaultPage.ServerRelativeUrl; //find the item in the PSMP PortalListItemSiteMapNode smnHome = (PortalListItemSiteMapNode)PortalSiteMapProvider.
CurrentNavSiteMapProviderNoEncode.FindSiteMapNode(defaultPageUrl); //initialize the RadMenu RadMenu menu = new RadMenu(); menu.ID = "MainMenu"; menu.RadControlsDir = "/_wpresources/RadMenu.Net2/4.3.2.0__bbe59a8ad3533e68/RadControls"; menu.Skin = "Outlook"; //Set the client-side clicked event handler menu.OnClientItemClicked = "MainMenu_RadMenuItemClicked"; //create the first menu item for the default (home) page RadMenuItem rmiHome = createMenuItem(smnHome); menu.Items.Add(rmiHome); //iterate through each one of the pages and subsites and create menu items for them foreach (SiteMapNode smnTopLevelItem in smnHome.ParentNode.ChildNodes) { RadMenuItem rmiTopLevelItem = createMenuItem(smnTopLevelItem); //if the current sitemap has children, create a submenu for it if (smnTopLevelItem.HasChildNodes) { foreach (SiteMapNode smnChildItem in smnTopLevelItem.ChildNodes) { RadMenuItem rmiChildItem = createMenuItem(smnChildItem); rmiTopLevelItem.Items.Add(rmiChildItem); } } menu.Items.Add(rmiTopLevelItem); } this.Controls.Add(menu); //create the client-side script for the RadMenuItemClicked event handler StringBuilder sbScript = new StringBuilder(); sbScript.Append(Environment.NewLine + "<script language=\"javascript\">"); sbScript.Append(Environment.NewLine + "function MainMenu_RadMenuItemClicked(sender, eventArgs)"); sbScript.Append(Environment.NewLine + "{"); sbScript.Append(Environment.NewLine + "var pageGuid = eventArgs.Item.GetAttribute(\"PageGuid\");"); sbScript.Append(Environment.NewLine + "var webGuid = eventArgs.Item.GetAttribute(\"WebGuid\");"); sbScript.Append(Environment.NewLine + "alert(\"WebGuid:\" + webGuid + \" PageGuid:\" + pageGuid);"); sbScript.Append(Environment.NewLine + "}"); sbScript.Append(Environment.NewLine + "</script>"); this.Page.ClientScript.RegisterStartupScript(typeof(string), "RadMenu_MainMenu_RadMenuItemClicked", sbScript.ToString()); } /// <summary> /// Create a RadMenuItem from a SiteMapNode /// </summary> /// <param name="siteMapNode">SiteMapNode used to create RadMenuItem </param> /// <returns>RadMenuItem</returns> private RadMenuItem createMenuItem(SiteMapNode siteMapNode) { RadMenuItem menuItem = new RadMenuItem(); menuItem.NavigateUrl = siteMapNode.Url; menuItem.Text = siteMapNode.Title; SPListItem listItem = null; //if the SiteMapNode is the default page, it will be a PortalWebSiteMapNode if (siteMapNode is PortalWebSiteMapNode) listItem = getListItemFromPortalSiteMapNode((PortalWebSiteMapNode)siteMapNode); if (siteMapNode is PortalListItemSiteMapNode) listItem = getListItemFromPortalSiteMapNode((PortalListItemSiteMapNode)siteMapNode); //create the custom attributes on the list item if (listItem != null) { menuItem.Attributes.Add("PageGuid", listItem.UniqueId.ToString("N")); menuItem.Attributes.Add("WebGuid", listItem.Web.ID.ToString("N")); menuItem.ToolTip = listItem.Fields["MenuItemToolTip"].GetFieldValueAsText(listItem["MenuItemToolTip"]); } else { menuItem.Attributes.Add("PageGuid", string.Empty); menuItem.Attributes.Add("WebGuid", string.Empty); menuItem.ToolTip = siteMapNode.Title; } return menuItem; } private SPListItem getListItemFromPortalSiteMapNode(PortalListItemSiteMapNode siteMapNode) { SPWeb web = SPContext.Current.Site.AllWebs[siteMapNode.WebId]; PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); SPListItem pageItem = pubWeb.PagesList.Items[siteMapNode.UniqueId]; return pageItem; } private SPListItem getListItemFromPortalSiteMapNode(PortalWebSiteMapNode siteMapNode) { SPWeb web = SPContext.Current.Site.AllWebs[siteMapNode.WebId]; PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); SPListItem pageItem = null; if(pubWeb.DefaultPage != null) pageItem = pubWeb.PagesList.Items[pubWeb.DefaultPage.Item.UniqueId]; return pageItem; }