Thursday, 15 December 2011

Creating Custom Level Menu in SharePoint 2010, MOSS 2007

our new blog site : http://blog.hintechnology.com
Creating Custom 3 Level Menu IN SharePoint
1. Creating a SharePoint list that will manage you navigation.
2. Create you wep part code for the menu.
3. Add your Menu web part in your master page.
4. Test your application to view the result.

1. Create a SharePoint List Which look like this having the following field in it;




Item level column to have choice field type and three level Level 1, Level 2, Level 3,


Display0 column will also have the choice field with yes and no value




2 And after you have created the menu list now you can create your own Menu web part

Here is the code which for the menu web part;

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Drawing;

namespace MenuWebPart
{
    [Guid("743ab9a9-31e0-4ba4-b2bc-cbb5696bd18e")]
    public class Menu : System.Web.UI.WebControls.WebParts.WebPart
    {
        public string _AdminList = "[type_list_Name_of_Menu/ CustomMenu]";
        public Menu()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        [Personalizable, WebBrowsable, WebDisplayName("Admin List")]
        public string Set_AdminList
        {
            get
            {
                return _AdminList;
            }
            set
            {
                _AdminList = value;
            }
        }

        protected override void CreateChildControls()
        {
            // Create ASPMenu control.
            AspMenu _menu;
            string MenuID = _AdminList + "CustomMenu";
            _menu = new AspMenu();

            //Set the controls look and feel
            _menu.ID = "CustomMenu";
            _menu.EnableViewState = false;
            _menu.Orientation = System.Web.UI.WebControls.Orientation.Horizontal;
            _menu.StaticDisplayLevels = 1;
            _menu.MaximumDynamicDisplayLevels = 3;
            _menu.StaticSubMenuIndent = 0;
            _menu.DynamicHorizontalOffset = 0;
            _menu.StaticPopOutImageUrl = "/_layouts/images/arrow.png";
            _menu.StaticPopOutImageTextFormatString = "";
            _menu.DynamicHoverStyle.BackColor = Color.FromName("#CBE3F0");
            _menu.SkipLinkText = "";
            _menu.StaticSubMenuIndent = 0;
            _menu.CssClass = "ms-topNavContainer";

            _menu.StaticMenuItemStyle.CssClass = "ms-topnav";
            _menu.StaticMenuItemStyle.ItemSpacing = 0;
            _menu.StaticSelectedStyle.CssClass = "ms-topnavselected";
            _menu.StaticHoverStyle.CssClass = "ms-topNavHover";
            _menu.DynamicMenuStyle.BackColor = Color.FromName("#F2F3F4");
            _menu.DynamicMenuStyle.BorderColor = Color.FromName("#A7B4CE");
            _menu.DynamicMenuStyle.BorderWidth = 0;
            _menu.DynamicHoverStyle.BackColor = Color.FromName("#525252");
            _menu.DynamicHoverStyle.BorderColor = Color.FromName("#CBE3F0");
            _menu.DynamicMenuItemStyle.CssClass = "ms-topNavFlyOuts";
            _menu.DynamicHoverStyle.CssClass = "ms-topNavFlyOutsHover";
            _menu.DynamicSelectedStyle.CssClass = "ms-topNavFlyOutsSelected";

            SPWeb thisWeb = null;

            try
            {

                SPSite thisSite = SPControl.GetContextSite(Context);
                thisWeb = thisSite.OpenWeb();
                SPList spCascadingNav = thisWeb.Lists[_AdminList];

                SPQuery query = new SPQuery();
                //query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 1</Value></Eq><Eq><FieldRef Name='Display' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 1</Value></Eq><Eq><FieldRef Name='Display0' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                SPListItemCollection items = spCascadingNav.GetItems(query);

                //For each item, generate the row.
                System.Web.UI.WebControls.MenuItem _item = new System.Web.UI.WebControls.MenuItem();
                System.Web.UI.WebControls.MenuItem _item2;
                System.Web.UI.WebControls.MenuItem _item3;

                foreach (SPListItem menuItem in items)
                {
                    string parentID = " ";
                    if (menuItem["Parent ID"] != null)
                    {
                        parentID = menuItem["Parent ID"].ToString();
                    }
                    else { parentID = "None"; }
                    string title = menuItem["Title"].ToString();
                    string url = menuItem["Link URL"].ToString();
                    string itemID = menuItem["Link ID"].ToString();

                    _item = new System.Web.UI.WebControls.MenuItem(title, "", "", url);

                    SPQuery level2query = new SPQuery();
                    level2query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 2</Value></Eq><Eq><FieldRef Name='Display0' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                    //level2query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 2</Value></Eq><Eq><FieldRef Name='Display' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                    SPListItemCollection level2items = spCascadingNav.GetItems(level2query);

                    foreach (SPListItem subMenuItem in level2items)
                    {
                        string subparentID = " ";
                        if (subMenuItem["Parent ID"] != null)
                        {
                            subparentID = subMenuItem["Parent ID"].ToString();
                        }
                        else { subparentID = "None"; }

                        if (subparentID == itemID)
                        {
                            string subtitle = subMenuItem["Title"].ToString();
                            string suburl = subMenuItem["Link URL"].ToString();
                            string subID = subMenuItem["Link ID"].ToString();

                            _item2 = new System.Web.UI.WebControls.MenuItem(subtitle, "", "", suburl);

                            SPQuery level3query = new SPQuery();
                            //level3query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 3</Value></Eq><Eq><FieldRef Name='Display' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                            level3query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 3</Value></Eq><Eq><FieldRef Name='Display0' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                            SPListItemCollection level3items = spCascadingNav.GetItems(level3query);

                            foreach (SPListItem sub3MenuItem in level3items)
                            {
                                string sub3parentID = " ";
                                if (sub3MenuItem["Parent ID"] != null)
                                {
                                    sub3parentID = sub3MenuItem["Parent ID"].ToString();
                                }
                                else { sub3parentID = "None"; }
                                if (sub3parentID == subID)
                                {
                                    string sub3title = sub3MenuItem["Title"].ToString();
                                    string sub3url = sub3MenuItem["Link URL"].ToString();

                                    _item3 = new System.Web.UI.WebControls.MenuItem(sub3title, "", "", sub3url);
                                    _item2.ChildItems.Add(_item3);
                                }
                            }
                            _item.ChildItems.Add(_item2);
                        }

                    }
                    _menu.Items.Add(_item);

                    Controls.Add(_menu);
                }
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl("An error has occured with this web part.  Please contact your system administrator and relay this error message: " + ex.Message));
            }
            finally
            {
                if (thisWeb != null)
                    thisWeb.Dispose();
            }

        }
    }
}


3 Changes to be done in the master page of the application.

Deploye your web part and make some changes in the master page of the application the application in the sharepoint designer 2010 and open the master page of the application and add the following tag in the top

<%@ Register TagPrefix=”customMenu”  namespace=” MenuWebPart”   assembly=” MenuWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=”65ac12bd34er8583″  %>


as Images Displayed below




and add the following tag in the body part of the master page as displayed below

< customMenu: MenuWebPart  runat=”server” id=”customMenu” Set_AdminList=”[type_list_Name_of_Menu]/ CustomMenu” __WebPartId=”{69ER54DE-2SFS-8581-A3C5-DD45S54SD5}” />



If your list name in the sharepoint site is  CustomMenu as displayed below then

=”[type_list_Name_of_Menu]”  instead this type the name as CustomMenu

I will also thank to the owner of link


http://cregan.wordpress.com/2008/05/09/wss-navigation-flyouts-security-trimming-custom-nav-items/


No comments:

Post a Comment