这篇随笔用自下义开发FolderNavigation 来了解自定义控件开发的流程,下面是讲建义的示例代码
首先新建一个空的SharePoint项目,添加两个类,两个类的代码不是主要的,主要是讲其开发部署流程,一个是WebFolderNavigation.cs, 一个是SPUtil.cs
WebFolderNavigation.cs
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using Microsoft.SharePoint; using Microsoft.SharePoint.Navigation; using System.Web.UI; using System.Collections; namespace CustomMenu { /// <summary> /// 当前站点文件夹导航 /// </summary> public class WebFolderNavigation : Menu { const String SORT_FIELD_NAME = "排序号"; private int _MaxFolderDepth = 2; /// <summary> /// 显示在导航栏上的文件夹最大深度 /// </summary> public int MaxFolderDepth { get { return _MaxFolderDepth; } set { _MaxFolderDepth = value; } } protected override void CreateChildControls() { if (this.Items.Count > 0) return; base.CreateChildControls(); SPWeb web = SPContext.Current.Web; string webUrl = web.ServerRelativeUrl; if (!webUrl.EndsWith("/")) webUrl += "/"; //获取到所有需要显示在导航栏上的列表 Dictionary<string, SPList> allVisibleList = new Dictionary<string, SPList>(); foreach (SPList list in web.Lists) { if (list.OnQuickLaunch) allVisibleList.Add(list.DefaultViewUrl.ToLower(), list); } //获取系统默认的导航链接 //系统默认导航第一级是列表分类,如文档,列表,讨论,第二级才是真正的列表链接。 //我们不需要显示第一级的分类,只需要显示第二级的列表链接 List<SPNavigationNode> navLinks = new List<SPNavigationNode>(); foreach (SPNavigationNode levelOneNode in web.Navigation.QuickLaunch) { if (levelOneNode.IsVisible) { foreach (SPNavigationNode levelTwoNode in levelOneNode.Children) { if (levelTwoNode.IsVisible) { navLinks.Add(levelTwoNode); } } } } //遍历第二级链接 foreach (SPNavigationNode node in navLinks) { //如果链接对应某个列表 if (allVisibleList.ContainsKey(node.Url.ToLower())) { SPList list = allVisibleList[node.Url.ToLower()]; //创建列表链接(一级栏目) MenuItem rootItem = new MenuItem(); rootItem.Text = list.Title; rootItem.ToolTip = list.Description; rootItem.NavigateUrl = list.DefaultViewUrl; this.Items.Add(rootItem); string sortFieldName = ""; if (list.Fields.ContainsField(SORT_FIELD_NAME)) { sortFieldName = list.Fields.GetField(SORT_FIELD_NAME).InternalName; } //创建子栏目 CreateMenuItem(list.RootFolder, rootItem.ChildItems, webUrl, sortFieldName, 1); } else { CreateMenuItemByNavigationNode(this.Items, node); } } } /// <summary> /// 按照导航节点创建MenuItem /// </summary> /// <param name="items"></param> /// <param name="node"></param> void CreateMenuItemByNavigationNode(MenuItemCollection items, SPNavigationNode node) { MenuItem item = new MenuItem(); item.Text = node.Title; item.NavigateUrl = node.Url; items.Add(item); if (node.Children.Count == 0) return; foreach (SPNavigationNode n in node.Children) { CreateMenuItemByNavigationNode(item.ChildItems, n); } } /// <summary> /// 通过子文件夹创建MenuItem,对文件夹按照排序字段进行排序 /// </summary> /// <param name="folder"></param> /// <param name="items"></param> /// <param name="webUrl"></param> /// <param name="sortField"></param> /// <param name="depth"></param> void CreateMenuItem(SPFolder folder, MenuItemCollection items, string webUrl, string sortField, int depth) { if (folder.SubFolders.Count == 0) return; IEnumerable subFoleder = null; if (sortField != "") subFoleder = SPUtil.GetSortedFolders(folder.SubFolders, sortField); else subFoleder = folder.SubFolders; //IList<SPFolder> folders = SPUtil.GetSortedFolders(folder.SubFolders , sortField ); foreach (SPFolder f in subFoleder) { if (SPUtil.IsHiddenFolder(f)) continue; MenuItem item = new MenuItem(); item.Text = f.Name; item.NavigateUrl = f.ServerRelativeUrl; items.Add(item); if (depth < MaxFolderDepth) CreateMenuItem(f, item.ChildItems, webUrl, sortField, depth + 1); } } } }
SPUtil.cs
using System; using System.Collections.Specialized; using System.Collections.Generic; using System.Text; using System.Xml; using Microsoft.SharePoint; namespace CustomMenu { /// <summary> /// /// </summary> public class SPUtil { static public bool IsHiddenFolder(SPFolder f) { return f.Item == null; } static public string GetWebUrl( SPWeb web ) { string webUrl = web.Url; if (!webUrl.EndsWith("/")) webUrl += "/"; return webUrl; } public static StringDictionary GetFieldDictionary(SPList list) { StringDictionary fields = new StringDictionary(); foreach (SPField f in list.Fields) { if (fields.ContainsKey(f.Title)) continue; fields.Add(f.Title, f.InternalName); } return fields; } /// <summary> /// 修改视图中的查询条件部分 /// </summary> /// <param name="doc"></param> /// <param name="query"></param> static public void ChangeSchemaXmlQuery(XmlDocument doc, string query) { if (String.IsNullOrEmpty(query)) return; string innerQuery = GetInnerQuery(query); if (innerQuery == "") return; XmlNode queryNode = doc.DocumentElement.SelectSingleNode("Query"); //queryNode.InnerXml = qxml; XmlNode whereNode = queryNode.SelectSingleNode("Where"); if (whereNode != null) queryNode.RemoveChild(whereNode); XmlNode newWhereNode = doc.CreateElement("Where"); newWhereNode.InnerXml = innerQuery; queryNode.AppendChild(newWhereNode); XmlNode ViewEmptyNode = doc.DocumentElement.SelectSingleNode("ViewEmpty"); ViewEmptyNode.InnerXml = "<HTML><![CDATA[<font color='red'><b>未找到符合查询条件的记录。</b></font>]]></HTML>"; } static string GetInnerQuery(string q) { XmlDocument doc = new XmlDocument(); doc.LoadXml(q); return doc.DocumentElement.InnerXml; } /// <summary> /// 获取ViewXml中的排序结 /// </summary> /// <param name="viewXml"></param> /// <returns></returns> public static string GetOrderBySection( string viewXml ) { XmlDocument doc = new XmlDocument(); doc.LoadXml(viewXml); XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("OrderBy"); if (nodes == null || nodes.Count == 0) return ""; else return nodes[0].OuterXml; } /// <summary> /// 指定ID的试图是否存在 /// </summary> /// <param name="views"></param> /// <param name="viewId"></param> /// <returns></returns> static public bool ViewExist(SPViewCollection views, Guid viewId) { foreach (SPView view in views) { if (view.ID == viewId) return true; } return false; } /// <summary> /// 获取按名称排序后的子文件夹列表 /// </summary> /// <param name="folders"></param> /// <returns></returns> static public IList<SPFolder> GetSortedFolders(SPFolderCollection folders) { List<SPFolder> sortedFolders = new List<SPFolder>(); foreach (SPFolder f in folders) { if (f.Item != null) //为非系统文件夹 sortedFolders.Add(f); } sortedFolders.Sort(CompareFolder); return sortedFolders; } static int CompareFolder(SPFolder f1, SPFolder f2) { return f1.Name.CompareTo(f2.Name); } /// <summary> /// 获取按某个字段排序后的子文件夹 /// </summary> /// <param name="folders"></param> /// <param name="sortFieldName"></param> /// <returns></returns> static public IList<SPFolder> GetSortedFolders(SPFolderCollection folders , string sortFieldName ) { List<SPFolder> sortedFolders = new List<SPFolder>(); foreach (SPFolder f in folders) { if( f.Item != null ) //为非系统文件夹 sortedFolders.Add(f); } SPFolderComparer c = new SPFolderComparer(sortFieldName); sortedFolders.Sort(c); return sortedFolders; } class SPFolderComparer : IComparer<SPFolder> { private string _sortFieldName; public SPFolderComparer(string sortFieldName) { _sortFieldName = sortFieldName; } #region IComparer<SPFolder> 成员 public int Compare(SPFolder x, SPFolder y) { //if (IsHiddenFolder(x) || IsHiddenFolder(y)) return 0; if (!x.Item.Fields.ContainsField(_sortFieldName) || !y.Item.Fields.ContainsField(_sortFieldName)) return 0; string sX = "" + x.Item[_sortFieldName]; if (sX == "") sX = "0"; string sY = "" + y.Item[_sortFieldName]; if (sY == "") sY = "0"; return Convert.ToInt32( sX ).CompareTo( Convert.ToInt32(sY) ) ; } #endregion } } }
重新生成项目,把DLL放到端口Bin目录
在端口Web.config 里添加
<SafeControl Assembly="CustomMenu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="CustomMenu" TypeName="*" Safe="True" />
在网站中的母版页中添加以下代码 ,如我的是Default.master
<%@ Register Assembly="CustomMenu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="CustomMenu" TagPrefix="CodeArt" %>
然后替换为导航控件,找到默认导航控件(控件ID为QuickLaunchNavigationManager),将其内容替换为如
<CodeArt:WebFolderNavigation width="100%" ID="folderav" runat="server" BackColor="#B5C7DE" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px"> <StaticSelectedStyle BackColor="#507CD1" /> <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" Height="25px" /> <DynamicHoverStyle BackColor="#284E98" ForeColor="White" /> <DynamicMenuStyle BackColor="#B5C7DE" /> <DynamicSelectedStyle BackColor="#507CD1" /> <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <StaticHoverStyle BackColor="#284E98" ForeColor="White" /> </CodeArt:WebFolderNavigation >
保存然后发布、审批就可以了,其他人就可以看到!