/// <summary> /// Create all your controls here for rendering. /// Try to avoid using the RenderWebPart() method. /// </summary> protected override void CreateChildControls() { // Create ASPMenu control. ChromeType = PartChromeType.None; /*string MenuID = _MenuList + "CustomTopNavWebPart";*/ AspMenu _spMenu = new AspMenu { UseSimpleRendering = true, UseSeparateCSS = false, Orientation = Orientation.Horizontal, MaximumDynamicDisplayLevels = 5, CssClass = "s4-tn" }; SPWeb thisWeb = null; try { //Set some defaults if (Set_TopNavigationMenu.ToLower() != "yes" && Set_TopNavigationMenu.ToLower() != "no") { Set_TopNavigationMenu = "yes"; } if (Set_TopNavigationMenu.ToLower() == "yes") { Set_MenuName = "TopNavigationMenu"; Set_MenuOrientation = "horizontal"; } //Set the controls look and feel //For some reason the ID of the Menu matters when using this as a Top Menu, the name must be TopNavigationMenu or the dropdowns are blank. _spMenu.ID = Set_MenuName; _spMenu.EnableViewState = false; SPSite thisSite = null; try { if (string.IsNullOrEmpty(Set_ServerURL)) { //Try to get the ServerURl from the web config Set_ServerURL = ConfigurationManager.AppSettings["topNavWebAppURL"]; if (string.IsNullOrEmpty(Set_ServerURL)) { //If cannot get from web config then set to the current web application Set_ServerURL = SPContext.Current.Site.WebApplication.GetResponseUri(SPContext.Current.Site.Zone).AbsoluteUri; } thisSite = new SPSite(Set_ServerURL); } else { thisSite = new SPSite(Set_ServerURL); } if (Set_WebSite == "" || Set_WebSite == null) { thisWeb = thisSite.OpenWeb("/"); } else { thisWeb = thisSite.OpenWeb(Set_WebSite); } } finally { if (thisSite != null) { thisSite.Dispose(); } } AudienceManager audMgr = new AudienceManager(SPServiceContext.GetContext(thisSite)); AudienceCollection audiences = audMgr.Audiences; AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader(); string target = ""; SPList _spListMenu = thisWeb.Lists[Set_MenuList]; _NewWindowFieldExists = _spListMenu.Fields.ContainsField("OpenNewWindow"); SPQuery _spQuery = new SPQuery(); MenuItem _spMenuItem = new MenuItem(); _spQuery.Query = "<OrderBy><FieldRef Name='LinkOrder' Ascending='True' /><FieldRef Name='Title' Ascending='True' /></OrderBy><Where><And><IsNull><FieldRef Name='ParentMenu' /></IsNull><Eq><FieldRef Name='ShowMenuItem' /><Value Type='Choice'>Yes</Value></Eq></And></Where>"; SPListItemCollection _spListItems = _spListMenu.GetItems(_spQuery); foreach (SPListItem item in _spListItems) { string audienceFieldValue = (string)item["CanBeSeenBy"]; if (string.IsNullOrEmpty(audienceFieldValue) || AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false)) { if (item["Link URL"] == null) { _spMenuItem = new MenuItem(item["Title"].ToString()); } else { if (_NewWindowFieldExists && item["OpenNewWindow"] != null && (Boolean)item["OpenNewWindow"] == true) { target = "_blank"; } _spMenuItem = new MenuItem(item["Title"].ToString(), "", "", SetServerURL(item["LinkURL"].ToString()), target); } GetListItems(item["ID"].ToString(), _spMenuItem, _spListMenu, thisSite); if (Page.Request.Url.AbsoluteUri == item["Link URL"].ToString()) { _spMenuItem.Selected = true; } _spMenu.Items.Add(_spMenuItem); } } Controls.Add(_spMenu); } catch (Exception ex) { ULSLoggingService myULS = new ULSLoggingService("TopNavProvider", SPContext.Current.Site.WebApplication.Farm); if (myULS != null) { SPDiagnosticsCategory cat = new SPDiagnosticsCategory("CreateChildControls", TraceSeverity.High, EventSeverity.Verbose); cat = myULS[CategoryId.WebPart]; myULS.WriteTrace(1, cat, TraceSeverity.Verbose, String.Format("{0};{1}", ex.Message, ex.StackTrace), myULS.TypeName); } Controls.Add(new LiteralControl(String.Format("An error has occured with this web part. Please contact your system administrator and relay this error message: {0} sub:CreateChildControls ", ex.Message))); } finally { if (thisWeb != null) { thisWeb.Dispose(); } } }
protected override void CreateChildControls() { this.ChromeType = PartChromeType.None; string text = this._MenuList + "spCustomMenu"; AspMenu aspMenu = new AspMenu(); //aspMenu.Orientation = Orientation.Vertical; SPWeb sPWeb = null; try { aspMenu.UseSimpleRendering = true; aspMenu.CssClass = "spNavigation"; if (this.Set_TopNavigationMenu.ToLower() != "yes" && this.Set_TopNavigationMenu.ToLower() != "no") { this.Set_TopNavigationMenu = "yes"; } if (this.Set_TopNavigationMenu.ToLower() == "yes") { this.Set_MenuName = "TopNavigationMenu"; this.Set_MenuOrientation = "horizontal"; } aspMenu.ID = this.Set_MenuName; aspMenu.EnableViewState = false; if (this.Set_MenuOrientation.ToLower() == "horizontal") { aspMenu.Orientation = Orientation.Horizontal; } else { aspMenu.Orientation = Orientation.Vertical; } SPSite sPSite; if (this.Set_ServerURL == "" || this.Set_ServerURL == null) { sPSite = SPControl.GetContextSite(this.Context); this.Set_ServerURL = sPSite.Url; } else { sPSite = new SPSite(this.Set_ServerURL); } sPWeb = sPSite.OpenWeb("/"); SPList sPList = sPWeb.Lists[this.Set_MenuList]; this._NewWindowFieldExists = sPList.Fields.ContainsField("OpenNewWindow"); SPQuery sPQuery = new SPQuery(); MenuItem menuItem = new MenuItem(); this.Controls.Add(new LiteralControl("made it this far")); sPQuery.Query = "<OrderBy><FieldRef Name='LinkOrder' Ascending='True' /><FieldRef Name='Title' Ascending='True' /></OrderBy><Where><And><IsNull><FieldRef Name='ParentMenu' /></IsNull><Eq><FieldRef Name='ShowMenuItem' /><Value Type='Choice'>Yes</Value></Eq></And></Where>"; SPListItemCollection items = sPList.GetItems(sPQuery); //SPListItemCollection items = sPList.GetItems(); foreach (SPListItem sPListItem in items) { if (sPListItem["Link URL"] == null) { menuItem = new MenuItem(sPListItem["Title"].ToString()); } /* else * { * menuItem = new MenuItem(sPListItem["Title"].ToString(), "", "", this.SetServerURL(sPListItem["Link URL"].ToString())); * }*/ // this.GetListItems(sPListItem["ID"].ToString(), menuItem, sPList); aspMenu.Items.Add(menuItem); } this.Controls.Add(aspMenu); } catch (Exception ex) { this.Controls.Add(new LiteralControl("An error has occured with this web part. Please contact your system administrator and relay this error message: " + ex.InnerException.ToString() + " sub:CreateChildControls ")); } finally { if (sPWeb != null) { sPWeb.Dispose(); } } }