1):假设我们有这样一个场景,我们为不同组的pages定义了不同的Layout,并且我们要在每个Layout中放置不同的可以修改Footer信息。
2):这里的实现方式是创建一个List,然后在List中添加不同的Item,每个Item包含Layout中的Footer信息,然后在SharePoint 中用SafeControl的方式创建能够重复利用的Control。
如下为具体实现方式:
1):创建Control,这里定义了一些参数,指定从哪个List中的哪个Item的哪个Field中获取信息
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint; namespace EricSunProject.WebParts.WebControls { [DefaultProperty("ListName")] [ToolboxData("<{0}:FnxDisclosureControl runat=server></{0}:FnxDisclosureControl>")] public class FnxDisclosureControl : WebControl { /// <summary> /// The name of the disclosure list /// </summary> [Bindable(true)] public string ListName { get { String s = (String)ViewState["ListName"]; return ((s == null) ? String.Empty : s); } set { ViewState["ListName"] = value; } } /// <summary> /// The key field name to retrieve the disclosure text /// </summary> [Bindable(true)] public string DisclosureField { get { String s = (String)ViewState["DisclosureField"]; return ((s == null) ? String.Empty : s); } set { ViewState["DisclosureField"] = value; } } /// <summary> /// The disclosure text field name /// </summary> [Bindable(true)] public string DisclosureTextField { get { String s = (String)ViewState["DisclosureTextField"]; return ((s == null) ? String.Empty : s); } set { ViewState["DisclosureTextField"] = value; } } /// <summary> /// The unique disclosure key value /// </summary> [Bindable(true)] public string DisclosureUniqueName { get { String s = (String)ViewState["DisclosureUniqueName"]; return ((s == null) ? String.Empty : s); } set { ViewState["DisclosureUniqueName"] = value; } } protected override void CreateChildControls() { base.CreateChildControls(); if (!string.IsNullOrEmpty(this.DisclosureUniqueName) && !string.IsNullOrEmpty(this.ListName) && !string.IsNullOrEmpty(this.DisclosureField) && !string.IsNullOrEmpty(this.DisclosureTextField)) { SPWeb rootWeb = SPContext.Current.Site.RootWeb; if (rootWeb != null) { SPList disclosureList = rootWeb.Lists[this.ListName]; if (disclosureList != null) { SPQuery query = new SPQuery(); query.Query = string.Format("<Where><Eq><FieldRef Name=\"{0}\" /><Value Type=\"Text\">{1}</Value></Eq></Where>", this.DisclosureField, this.DisclosureUniqueName); SPListItemCollection items = disclosureList.GetItems(query); if (items.Count > 0) { SPListItem contentItem = items[0]; // query should only return 1 item. string htmlContent = contentItem != null ? contentItem[this.DisclosureTextField] as string : string.Empty; this.Controls.Add(new Literal() { Text = htmlContent }); } } } } } } }
2):在package中声明此SafeControl
<?xml version="1.0" encoding="utf-8"?> <Solution xmlns="http://schemas.microsoft.com/sharepoint/"> <Assemblies> <Assembly Location="EricSunProject.WebParts.dll" DeploymentTarget="GlobalAssemblyCache"> <SafeControls> <SafeControl Assembly="EricSunProject.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1de19275c7811c3b" Namespace="EricSunProject.WebParts.WebControls" TypeName="FnxDisclosureControl" /> </SafeControls> </Assembly> </Assemblies> </Solution>
3):在对应的Layout中注册此Control
<%@ Register Tagprefix="EricSunControls" Namespace="EricSunProject.WebParts.WebControls" Assembly="EricSunProject.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1de19275c7811c3b" %>
4):应用此Control
<div class="footnote"> <EricSunControls:FnxDisclosureControl ID="FnxPageFooter" ListName="FNX Disclosures" DisclosureField="FnxDisclosureType" DisclosureTextField="FnxDisclosureText" DisclosureUniqueName="Home Page Disclosure" runat="server" /> </div>
这样就做到了这个Control可以重复利用,并且只需要改变参数,标识应用哪个List中的哪个Item中的哪个Field,去填充信息。当然了 SharePoint中的Item的值是可以随时修改的,这样后续只需要修改SharePoint中Item对应的Field值,那么我们自定义的Pages就可以随时显示修改后的Footer等信息。