Exemplo n.º 1
0
        private void GetListItems(string str, MenuItem _spMenu, SPList _spListMenu, SPSite thisSite)
        {
            AudienceManager    audMgr         = new AudienceManager(SPServiceContext.GetContext(thisSite));
            AudienceCollection audiences      = audMgr.Audiences;
            AudienceLoader     audienceLoader = AudienceLoader.GetAudienceLoader();

            try
            {
                SPQuery _spQuery = new SPQuery();
                string  target   = "";
                _spQuery.Query = String.Format("<OrderBy><FieldRef Name='LinkOrder' Ascending='True' /><FieldRef Name='Title' Ascending='True' /></OrderBy><Where><And><Eq><FieldRef Name='ParentMenu' LookupId= 'TRUE'  /><Value Type='Lookup'>{0}</Value></Eq><Eq><FieldRef Name='ShowMenuItem' /><Value Type='Choice'>Yes</Value></Eq></And></Where>", str);
                SPListItemCollection _spListItems = _spListMenu.GetItems(_spQuery);
                MenuItem             _spMenuItem  = new MenuItem();
                foreach (SPListItem item in _spListItems)
                {
                    target = "";
                    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["Link URL"].ToString()), target);
                        }
                        GetListItems(item["ID"].ToString(), _spMenuItem, _spListMenu, thisSite);
                        _spMenu.ChildItems.Add(_spMenuItem);
                    }
                }
            }
            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:GetListItems", ex.Message)));
            }
        }
Exemplo n.º 2
0
        /// <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();
                }
            }
        }