SharePoint平台为企业提供了强大的协作和内容管理功能,但随着企业需求的增加,SharePoint的默认界面可能无法满足所有用户的需求。本文将介绍如何使用SharePoint对象模型来自定义QuickLaunch,以便更好地满足用户的需求。
QuickLaunch是SharePoint中用于快速导航到列表、文档库、网站等的工具。随着网站和列表的增加,QuickLaunch变得越来越长,这就需要对它进行自定义,使其可以折叠。可以通过SharePoint对象模型来构建一个可折叠的QuickLaunch Webpart。
首先,需要使用SharePointVisual Studio模板创建一个Webpart。然后,需要初始化TreeView的ImageSet属性,以便通过更改Webpart属性来更改图像集。这个属性将在属性窗格中显示为一个下拉列表。
protected TreeViewImageSet _qlFormat;
[Personalizable(PersonalizationScope.Shared), WebBrowsable, WebDisplayName("格式"), WebDescription("")]
[DefaultValue(TreeViewImageSet.Arrows), WebPartStorage(Storage.Personal), FriendlyName("格式列表")]
[Description("从下拉列表中选择一个值。")]
[Browsable(true)]
public TreeViewImageSet SelectFormat
{
set { _qlFormat = value; }
get { return _qlFormat; }
}
接下来,需要向Webpart添加一个控件。
protected override void CreateChildControls()
{
base.CreateChildControls();
trViewQL = new TreeView();
this.Controls.Add(trViewQL);
}
为了维护TreeView的状态,需要将查询字符串附加到每个节点的URL上。
TreeNode trChild = new TreeNode();
trChild.Text = child.Title;
string separate = "";
if (child.Url.IndexOf('?') == -1)
{
separate = "?CNID=";
}
else
{
separate = "&CNID=";
}
trChild.NavigateUrl = child.Url + separate + child.Title;
将在OnPreRender方法中构建TreeView控件的QuickLaunch。这样做的原因是,每当Webpart属性发生变化时,它都会反映这些变化。
protected override void OnPreRender(EventArgs e)
{
string currentId = "";
if (System.Web.HttpContext.Current.Request.QueryString.Get("CNID") != null)
{
currentId = System.Web.HttpContext.Current.Request.QueryString.Get("CNID").ToString();
}
trViewQL.Nodes.Clear();
trViewQL.ImageSet = SelectFormat;
base.OnPreRender(e);
trViewQL.CollapseAll();
SPWeb web = SPContext.Current.Web;
SPNavigationNodeCollection qlNodes = web.Navigation.QuickLaunch;
foreach (SPNavigationNode node in qlNodes)
{
if (node.IsExternal)
{
bool IsparentOpen = false;
TreeNode trParent = new TreeNode();
trParent.Text = node.Title;
trParent.NavigateUrl = node.Url;
SPNavigationNodeCollection children = node.Children;
foreach (SPNavigationNode child in children)
{
TreeNode trChild = new TreeNode();
trChild.Text = child.Title;
string separate = "";
if (child.Url.IndexOf('?') == -1)
{
separate = "?CNID=";
}
else
{
separate = "&CNID=";
}
trChild.NavigateUrl = child.Url + separate + child.Title;
if (!IsparentOpen)
{
if (currentId.Equals(child.Title))
{
trParent.Expanded = true;
trChild.Selected = true;
IsparentOpen = true;
}
else
{
trParent.Expanded = false;
}
}
trParent.ChildNodes.Add(trChild);
}
trViewQL.Nodes.Add(trParent);
}
}
}
最后,通过覆盖Render方法来渲染控件。
protected override void Render(HtmlTextWriter writer)
{
trViewQL.RenderControl(writer);
}