现在的位置: 首页 > 综合 > 正文

SharePoint开发 – 自定义导航菜单(三)附其他代码

2018年01月23日 ⁄ 综合 ⁄ 共 4380字 ⁄ 字号 评论关闭

博客地址 http://blog.csdn.net/foxdave

接上篇点击打开链接

LeftNavGroupTemplate.cs

internal class LeftNavGroupTemplate : ITemplate
    {
        // Fields
        private int index;
        private string xml;

        // Methods
        public LeftNavGroupTemplate(string _navXml, int _groupIndex)
        {
            this.xml = _navXml;
            this.index = _groupIndex;
        }

        public void InstantiateIn(Control container)
        {
            HtmlGenericControl child = new HtmlGenericControl("div");
            child.Style["width"] = "150px";
            child.Style["overflow"] = "hidden";
            DevExpress.Web.ASPxTreeView.ASPxTreeView view = new DevExpress.Web.ASPxTreeView.ASPxTreeView
            {
                ID = "tv" + this.index
            };
            view.Font.Name = "微软雅黑";
            view.Font.Size = new FontUnit("12px");
            view.TextField = "title";
            view.ToolTipField = "title";
            view.NavigateUrlField = "url";
            view.ImageUrlField = "imgurl";
            view.Width = new Unit("100%");
            XmlDataSource source = new XmlDataSource
            {
                ID = Guid.NewGuid().ToString(),
                Data = this.xml
            };
            source.XPath = "/SiteMap/SiteMapNode[" + ((this.index + 1)).ToString() + "]/*";
            view.DataSource = source;
            view.DataBind();
            child.Controls.Add(view);
            container.Controls.Add(child);
        }
    }

Item.cs

[Serializable]
    public class Item
    {
        // Properties
        [XmlAttribute]
        public string Path { get; set; }

        [XmlAttribute]
        public string Title { get; set; }

        [XmlAttribute]
        public string Url { get; set; }
    }

GlobalTab.cs

[Serializable]
    public class GlobalTab
    {
        // Methods
        public GlobalTab()
        {
            if (this.ItemCol == null)
            {
                this.ItemCol = new List<Item>();
            }
        }

        // Properties
        [XmlElement("Item")]
        public List<Item> ItemCol { get; set; }
    }

Config.cs

 internal class Config
    {
        // Methods
        public static GlobalTab Deserialize(string xml)
        {
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(GlobalTab));
                return (GlobalTab)serializer.Deserialize(stream);
            }
        }

        public static XmlDataSource GetDataSource(SPWeb web, NavType navType)
        {
            XmlDataSource source = new XmlDataSource();
            try
            {
                string innerXml = Load(web, navType);
                if (string.IsNullOrEmpty(innerXml))
                {
                    return source;
                }
                XmlDocument document = new XmlDocument();
                document.LoadXml(innerXml);
                if (!web.IsRootWeb && (document.DocumentElement.GetAttribute("Inherited").ToLower() == "true"))
                {
                    innerXml = LoadParent(web.ParentWeb, navType);
                    document.LoadXml(innerXml);
                }
                if (!web.UserIsSiteAdmin)
                {
                    XmlNodeList list = document.SelectNodes("//SiteMapNode[@SPGroups]");
                    for (int i = 0; i < list.Count; i++)
                    {
                        string[] strArray = list[i].Attributes["SPGroups"].Value.Split(new char[] { ';' });
                        bool flag = false;
                        foreach (string groupName in strArray)
                        {
                            if ((from g in web.Groups.Cast<SPGroup>() select g.Name).Contains<string>(groupName))
                            {
                                SPGroup group = web.Groups[groupName];
                                //flag = (from g in web.CurrentUser.Groups.Cast<SPGroup>() select g.Name).Contains<string>(group.Name);
                                flag = group.ContainsCurrentUser;
                            }
                            if (flag)
                            {
                                break;
                            }
                        }
                        if (!flag)
                        {
                            list[i].ParentNode.RemoveChild(list[i]);
                        }
                    }
                }
                foreach (XmlNode node in document.SelectNodes("//SiteMapNode[@url]"))
                {
                    string url = node.Attributes["url"].Value;
                    node.Attributes["url"].Value = url.Replace("~site/", SPMIPUtility.GetRelative(web));
                }
                innerXml = document.InnerXml;
                source.Data = innerXml;
                source.XPath = "/SiteMap/*";
                source.DataBind();
                source.EnableCaching = false;
            }
            catch (Exception exception)
            {
                SPMIPTrace.WriteError("SPMIPNavigation", exception);
            }
            return source;
        }

        public static string Load(SPWeb web, NavType navType)
        {
            string str = null;
            try
            {
                StreamReader reader;
                SPList settingList = SPMIPUtility.CheckSettingList("SPMIPSetting");
                switch (navType)
                {
                    case NavType.Top:
                        {
                            SPFile topNavSettingFile = settingList.RootFolder.Files["TopNav.xml"];
                            if (topNavSettingFile.Exists)
                            {
                                using (reader = new StreamReader(topNavSettingFile.OpenBinaryStream()))
                                {
                                    str = reader.ReadToEnd();
                                }
                            }
                            break;
                        }
                    case NavType.Left:
                        {
                            SPFile leftNavSettingFile = settingList.RootFolder.Files["LeftNav.xml"];
                            if (leftNavSettingFile.Exists)
                            {
                                using (reader = new StreamReader(leftNavSettingFile.OpenBinaryStream()))
                                {
                                    str = reader.ReadToEnd();
                                }
                            }
                            break;
                        }
                }
                return str;
            }
            catch (Exception exception)
            {
                SPMIPTrace.WriteError("SPMIPNavigation", exception);
                return str;
            }
        }

        public static string LoadParent(SPWeb web, NavType navType)
        {
            if (web == null)
            {
                return null;
            }
            string str = null;
            try
            {
                str = Load(web, navType);
                if (!string.IsNullOrEmpty(str))
                {
                    return str;
                }
                return LoadParent(web.ParentWeb, navType);
            }
            catch
            {
                return LoadParent(web.ParentWeb, navType);
            }
        }

        public static string Serialize(GlobalTab _class)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(GlobalTab));
                XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                namespaces.Add(string.Empty, string.Empty);
                serializer.Serialize(stream, _class, namespaces);
                stream.Position = 0;
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                return Encoding.UTF8.GetString(buffer);
            }
        }

        // Nested Types
        public enum NavType
        {
            Top,
            Left
        }
    }

抱歉!评论已关闭.