基于映射路径生成共享点菜单,并通过整个会话保留数据

时间:2013-05-31 08:49:47

标签: sharepoint-2010

获取网站集内的网站应用程序和子网站内的所有网站集,并创建菜单库导航网址。

http://test.com/
http://:test.com/Development
http://:test.com/Development/Project 1
http://:test.com/Development/Project 2
http://:test.com/Development/Project 3
http://:test.com/testing
http://:test.com/testing/Project 1
http://:test.com/testing/Project 2
http://:test.com/testing/Project 3

My menu should be
Development -> Project1
               Project2
               Project3
Testing->      Project1
               Project2
               Project3

1 个答案:

答案 0 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections;
using System.Xml;
using System.Data;

namespace MasterPageWithCodeBehind.MasterPageModule
{
    public class _starter : MasterPage
    {
        protected Menu CustomMenu;
        private static IDictionary<string, XmlDocument> _Tracker = new Dictionary<string, XmlDocument> { };
        protected static List<MappedPath> lstNavigationPath;

    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDataSource xmlDS = new XmlDataSource();
        xmlDS.ID = "xSource1";
        XmlDocument xmlData = GetMenuData(SPContext.Current.Web.CurrentUser.LoginName);

        xmlDS.Data = xmlData.InnerXml;
        xmlDS.XPath = "Home/Menu";

        try
        {
            CustomMenu.DataSource = xmlDS;
            CustomMenu.DataBind();
        }
        catch (Exception Ex)
        {
        }
    }

    public static XmlDocument GetMenuData(string loginUser)
    {
        //value to return
        //Menu item = null;
        XmlDocument item = null;

        //lock collection to prevent changes during operation
        lock (_Tracker)
        {
            //if value not found, create and add
            if (!_Tracker.TryGetValue(loginUser, out item))
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    SPWebApplication webapp = site.WebApplication;
                    item = GenerateMenu(webapp);
                }
                //calculate next key
                string newIdent = loginUser;

                //add item
                _Tracker.Add(newIdent, item);
            }
            else
            {
                item = _Tracker[loginUser];
            }
        }
        return item;
    }

    public static XmlDocument GenerateMenu(SPWebApplication webApp)
    {
        lstNavigationPath = new List<MappedPath>();
        string loginUserName = SPContext.Current.Web.CurrentUser.LoginName;

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            foreach (SPSite site in webApp.Sites)
            {
                using (SPWeb oSPWeb = site.OpenWeb())
                {
                    foreach (SPWeb web in site.AllWebs)
                    {
                        if (web.DoesUserHavePermissions(loginUserName, SPBasePermissions.Open))
                        {
                            lstNavigationPath.Add(new MappedPath()
                            {
                                Title = web.Title,
                                URL = web.Url,
                                navigationpath = web.ServerRelativeUrl
                            });
                        }
                    }
                }
            }
        });

        XmlDocument xDoc = new XmlDocument();
        XmlNode docNode = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
        xDoc.AppendChild(docNode);
        // Home Node
        XmlNode homeNode = xDoc.CreateElement("Home");
        xDoc.AppendChild(homeNode);
        foreach (MappedPath obj in lstNavigationPath)
        {
            string navigationPath = obj.navigationpath.TrimStart('/');
            XmlNode menuNode = null;
            XmlNode parentNode = null;
            if (navigationPath.Split('/').Count() == 1)
            {
                menuNode = xDoc.CreateElement("Menu");
            }
            else
            {
                string parentNodeString = "/" + navigationPath.Substring(0, navigationPath.LastIndexOf('/'));
                parentNode = xDoc.SelectSingleNode(string.Format("Home/Menu[@navigationpath=\"{0}\"]", parentNodeString));
                if (parentNode == null)
                {
                    parentNode = xDoc.SelectSingleNode(string.Format("descendant::SubMenu[@navigationpath=\"{0}\"]", parentNodeString));
                }
                menuNode = xDoc.CreateElement(parentNode == null ? "Menu" : "SubMenu");
            }

            XmlAttribute textAttribute = xDoc.CreateAttribute("text");
            textAttribute.Value = obj.Title;
            menuNode.Attributes.Append(textAttribute);
            XmlAttribute urlAttribute = xDoc.CreateAttribute("url");
            urlAttribute.Value = obj.URL;
            menuNode.Attributes.Append(urlAttribute);
            XmlAttribute navigationpathAttribute = xDoc.CreateAttribute("navigationpath");
            navigationpathAttribute.Value = obj.navigationpath;
            menuNode.Attributes.Append(navigationpathAttribute);
            if (parentNode != null)
            {
                parentNode.AppendChild(menuNode);
            }
            else
            {
                homeNode.AppendChild(menuNode);
            }
        }
        return xDoc;
    }
}

public class MappedPath
{
    public string Title { get; set; }
    public string URL { get; set; }
    public string navigationpath { get; set; }
}

}