•创建一个显示指定文档库最新的文件列表的WebPart
•在WebPart定义可在浏览时让用户自行调整的属性
•在WebPart中访问SharePoint Object Model
二.开始创建
2.1新建项目"WebPartLibrary2"
2.2新建WebPart2.cs代码如下:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;//自己导入
namespace WebPartLibrary2
{
/**//// <summary>
/// Description for WebPart2.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart2 runat=server></{0}:WebPart2>"),
XmlRoot(Namespace="WebPartLibrary2")]
public class WebPart2 : Microsoft.SharePoint.WebPartPages.WebPart
{
自动生成#region 自动生成
private const string defaultText = "";
private string text = defaultText;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
/**//// <summary>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
/// </summary>
///<returns>An array of references to ToolPart objects.</returns>
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// return toolparts;
// }
#endregion
//一.创建属性 “ListName”
private String _sListName = "共享文档";
[Browsable(true),//此属性是否在(页面中的)属性Panel中显示出来
Category("设置"),//属性的类别
DefaultValue(""),//属性的(此WebPart的所有页面实例的)默认值
WebPartStorage(Storage.Personal),//枚举,Storage.Shared表示所有用户共享一个值,Storage.Personal为每个用户保存单独的值,Storage.None不保存
FriendlyName("文档库名称"),//显示在属性Panel中的名称
Description("要显示的文档库的名称")]//显示在属性Panel中的提示
public string ListName
{
get { return _sListName; }
set { _sListName = value; }
}
//二.创建属性 “DisplayCount”
private UInt32 _iDisplayCount = 10;
[Browsable(true),
Category("设置"),
DefaultValue(10),
WebPartStorage(Storage.Personal),
FriendlyName("显示文件数量"),
Description("可显示的文件的最大数量")]
public UInt32 DisplayCount
{
get { return _iDisplayCount; }
set { _iDisplayCount = value; }
}
/**//// <summary>
///三. 访问指定文档库中文件的Method
/// </summary>
/// <returns></returns>
private String GetListFiles()
{
String result = "";
if ((this.ListName != null) && (this.ListName.Length > 0))
{
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name=\"修改时间\" Ascending=\"FALSE\" /></OrderBy>";
query.RowLimit = this.DisplayCount;
SPList list = SPControl.GetContextWeb(this.Context).Lists[this.ListName];
foreach(SPListItem item in list.GetItems(query))
{
if (item.File != null)
{
result += "<li><a href='" + item.File.Url + "'>" + item.File.Name + "(" + item.File.Author.Name + ")" + "</a><br>";
}
}
}
return result;
}
/**//// <summary>
/// 四.调用GetListFiles()所返回的String来输出内容
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(this.GetListFiles());
}
}
}
2.3新建并配置WebPart2.dwp,修改WebPart2.dwp文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>李燕平的webpart2</Title>
<Description>第二个webpart</Description>
<Assembly>WebPartLibrary2</Assembly>
<TypeName>WebPartLibrary2.WebPart2</TypeName>
<!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>
2.4修改SPS虚拟站点的web.confi文件
增加<SafeControl Assembly="WebPartLibrary2" Namespace="WebPartLibrary2" TypeName="*" Safe="True" />
修改 <trust level="WSS_Minimal" originUrl="" />为<trust level="WSS_Medium" originUrl="" />
2.5将WebPartLibrary2.dll拷贝到 SPS虚拟站点根目录的“bin”目录中
2.6导入webpart ( 参见一步一步webpart-如何利用.net制作第一个webpart?(3) )
2.7将导入的webpart显示到页面(参见一步一步webpart-如何利用已导入的webpart?(4) )
....显示出现错误,修改中......
二.开始创建
2.1新建项目"WebPartLibrary2"
2.2新建WebPart2.cs代码如下:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;//自己导入
namespace WebPartLibrary2
{
/**//// <summary>
/// Description for WebPart2.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart2 runat=server></{0}:WebPart2>"),
XmlRoot(Namespace="WebPartLibrary2")]
public class WebPart2 : Microsoft.SharePoint.WebPartPages.WebPart
{
自动生成#region 自动生成
private const string defaultText = "";
private string text = defaultText;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
/**//// <summary>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
/// </summary>
///<returns>An array of references to ToolPart objects.</returns>
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// return toolparts;
// }
#endregion
//一.创建属性 “ListName”
private String _sListName = "共享文档";
[Browsable(true),//此属性是否在(页面中的)属性Panel中显示出来
Category("设置"),//属性的类别
DefaultValue(""),//属性的(此WebPart的所有页面实例的)默认值
WebPartStorage(Storage.Personal),//枚举,Storage.Shared表示所有用户共享一个值,Storage.Personal为每个用户保存单独的值,Storage.None不保存
FriendlyName("文档库名称"),//显示在属性Panel中的名称
Description("要显示的文档库的名称")]//显示在属性Panel中的提示
public string ListName
{
get { return _sListName; }
set { _sListName = value; }
}
//二.创建属性 “DisplayCount”
private UInt32 _iDisplayCount = 10;
[Browsable(true),
Category("设置"),
DefaultValue(10),
WebPartStorage(Storage.Personal),
FriendlyName("显示文件数量"),
Description("可显示的文件的最大数量")]
public UInt32 DisplayCount
{
get { return _iDisplayCount; }
set { _iDisplayCount = value; }
}
/**//// <summary>
///三. 访问指定文档库中文件的Method
/// </summary>
/// <returns></returns>
private String GetListFiles()
{
String result = "";
if ((this.ListName != null) && (this.ListName.Length > 0))
{
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name=\"修改时间\" Ascending=\"FALSE\" /></OrderBy>";
query.RowLimit = this.DisplayCount;
SPList list = SPControl.GetContextWeb(this.Context).Lists[this.ListName];
foreach(SPListItem item in list.GetItems(query))
{
if (item.File != null)
{
result += "<li><a href='" + item.File.Url + "'>" + item.File.Name + "(" + item.File.Author.Name + ")" + "</a><br>";
}
}
}
return result;
}
/**//// <summary>
/// 四.调用GetListFiles()所返回的String来输出内容
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(this.GetListFiles());
}
}
}
2.3新建并配置WebPart2.dwp,修改WebPart2.dwp文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>李燕平的webpart2</Title>
<Description>第二个webpart</Description>
<Assembly>WebPartLibrary2</Assembly>
<TypeName>WebPartLibrary2.WebPart2</TypeName>
<!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>
2.4修改SPS虚拟站点的web.confi文件
增加<SafeControl Assembly="WebPartLibrary2" Namespace="WebPartLibrary2" TypeName="*" Safe="True" />
修改 <trust level="WSS_Minimal" originUrl="" />为<trust level="WSS_Medium" originUrl="" />
2.5将WebPartLibrary2.dll拷贝到 SPS虚拟站点根目录的“bin”目录中
2.6导入webpart ( 参见一步一步webpart-如何利用.net制作第一个webpart?(3) )
2.7将导入的webpart显示到页面(参见一步一步webpart-如何利用已导入的webpart?(4) )
....显示出现错误,修改中......