本文将举例说明如何在wssv3中创建一个Filter consumer WebPart 来显示头条新闻。这些头条新闻内容依赖于地理区域的筛选提供者WebPart中用户的选择。
我们来编写
NewsHeadlinesWebPart 类的代码。
- 首先来创建用于显示头新闻到用户界面上的DataGrid 控件。
- 开放一个consumer连接点用来接收地理区域筛选提供者WebPart发送来的IFilterValues接口。
- 在OnPreRender方法中使用IFilterValues接口显示基于当前区域筛选值的头条新闻。所有未筛选的可用的头条新闻通过Excel Services从一个Excel工作薄载入。该Excel工作薄假定存放在一个受信任的文件位置,并且其具备下列条件:
- 包括一个工作表名为"Sheet1"
- "Sheet1"工作表中存在一个名为"Headlines"的范围
- 该"Headlines"范围由两栏组成,第一栏放头条新闻(news headline),第二栏放区域(region)
用下面的代码替换现有的整个NewsHeadlinesWebPart 类定义。
public class NewsHeadlinesWebPart : wsswebparts.WebPart
{
public class Headline
{
private string title;
private string region;
public Headline(string Title, string Region)
{
this.title = Title;
this.region = Region;
}
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
public string Region
{
get
{
return this.region;
}
set
{
this.Region = value;
}
}
}
List<wsswebparts.IFilterValues> filterProviders =
new List<wsswebparts.IFilterValues>();
List<Headline> unfilteredHeadlines;
DataGrid headlinesDataGrid;
Label lblError;
protected override void CreateChildControls()
{
headlinesDataGrid = new DataGrid();
lblError = new Label();
unfilteredHeadlines = new List<Headline>();
headlinesDataGrid.ID = "list1";
Controls.Add(headlinesDataGrid);
base.CreateChildControls();
}
private void GetHeadlinesUsingWebService()
{
Status[] status = null;
string sessionId = null;
// Get the list of headlines from the Excel workbook by calling
// Excel Web Services.
// Initialize Excel Web Services.
ExcelService es = new ExcelService();
// Open the workbook. This actionloads the workbook from the
// specified URL and returns a sessionId that can be used to
// perform further operations on the workbook. Replace the
// <TrustedLocation> placeholder with a full Windows SharePoint
// Services location, network file share, or Web folder address
// of the trusted location of the Excel workbook containing
// the news headlines. Replace the <Workbook>
// placeholder with the name of the Excel workbook containing
// the news headlines.
try
{
sessionId =
es.OpenWorkbook("<TrustedLocation>/<Workbook>.xlsx",
string.Empty, string.Empty, out status);
}
catch
{
sessionId = null;
}
// Ensure that the workbook has been successfully opened on the
// server. If not, show an error message to the user.