通过为web part创建自定义属性,我们可以通过改变web part的属性实现web part在页面上的动态显示。这些属性存储在数据库中,可以在页面加载的时候进行重用。
接下来创建的web part将会显示Address,web part提供自定义属性可以供SharePoint 的用户进行修改。我们有一些简单的地址信息如name,street,zip code和city。
创建项目
打开Visual Studio,创建一个Visual Web Part project,命名为Example.AddressWebPart。
删除默认的Visual Web Part:“VisualWebPart1”
添加一个Visual Web Part, 命名为Address
打开Features文件夹,双击Feature1,改变feature名字为Address Feature,
为了使得web part在SharePoint上有一个很好的描述,我们可以对配置信息做一些小的改动:双击Address.webpart文件在Address文件夹中,改变web part的描述信息:
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="/WebPart/v3">
<metaData>
<type name="Images.Address.Address, $SharePoint.Project.AssemblyFullName$" />
<importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Address</property>
<property name="Description" type="string">Displays a personalizable address on the screen</property>
</properties>
</data>
</webPart>
</webParts>
我们也可以改变web part的category,例如改变category为“Utilities”web part就会在名字为Utilities的group中,打开Elements.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="/sharepoint/" >
<Module Name="Address" List="113" Url="_catalogs/wp">
<File Path="Address\Address.webpart" Url="Address.webpart" Type="GhostableInLibrary" >
<Property Name="Group" Value="Utilities" />
</File>
</Module>
</Elements>
自定义web part
首先,添加我们需要的属性到Address.cs文件中,Personalizable告诉Web Part framework去存储属性到数据库中,WebBrowsable属性高数web part framework在web part的编辑区显示属性:
namespace Images.Address
{
[ToolboxItemAttribute(false)]
public class Address : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/Images/Address/AddressUserControl.ascx";
[Personalizable(), WebBrowsable]
public String Firstname { get; set; }
[Personalizable(), WebBrowsable]
public String Lastname { get; set; }
[Personalizable(), WebBrowsable]
public String Street { get; set; }
[Personalizable(), WebBrowsable]
public int Zip { get; set; }
[Personalizable(), WebBrowsable]
public String City { get; set; }
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
}
}
}
现在我们可以查看web part在SharePoint中的显示结果,deploy当前的solution,添加创建的web part到一个SharePoint 的page上,点击“Edit Web Part”,我们可以看到定义的属性在“miscellaneous”下面,如图所示:
尝试在文本框中出入一些值,web part framework将会存储这些值到数据库中,并关联到web part。然而在web part中没有显示,我们需要添加control到“AddressUserControl.ascx”,添加control之前,对“AddressUserControl.ascx.cs”文件做如下修改:
public partial class AddressUserControl : UserControl
{
public Address WebPart { get; set; }
}
然后点击“Address.cs”文件,编辑CreateChildControls事件:
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
if (control != null)
{
((AddressUserControl)control).WebPart = this;
}
Controls.Add(control);
}
通过上述代码的改变,我们告诉AddressUserControl去填充在web part配置时的属性。这样我们就可以进入到在Address.cs文件中定义的属性。
最后添加control到AddressUserControl.ascx文件:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="mandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddressUserControl.ascx.cs" Inherits="Images.Address.AddressUserControl" %>
<asp:Label ID="lblFirstname" runat="server" style="font-weight: 700" ></asp:Label> <asp:Label
ID="lblLastname" runat="server" style="font-weight: 700" ></asp:Label>
<br />
<br />
<asp:Label ID="lblStreet" runat="server" ></asp:Label>
<br />
<asp:Label ID="lblZip" runat="server" ></asp:Label> - <asp:Label ID="lblCity" runat="server" ></asp:Label>
添加Prerender事件到AddressUserControls.ascx.cs文件:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (this.WebPart != null)
{
this.lblFirstname.Text = this.WebPart.Firstname;
this.lblLastname.Text = this.WebPart.Lastname;
this.lblStreet.Text = this.WebPart.Street;
this.lblZip.Text = this.WebPart.Zip.ToString();
this.lblCity.Text = this.WebPart.City;
}
}
Deploy当前的solution,添加web part到一个SharePoint page上,修改它的属性,结果显示如下: