在Visual Studio 2005中创建一个web control library项目
创建一个filter consumer WebPart最简单的方式是套用Visual Studio 2005的自定义Web控件模板。下面是创建的步骤:
文件->新建->项目,出现新建项目对话框。 在项目类型中,选择C#,选择Windows类别。 在模板选择中,选择web控件库。 为项目指定一个名称NewsHeadlinesWebPart 。 指定保存的位置,确定。现在,项目中包含一个名为WebCustomControl1.cs的代码文件。 在解决方案浏览器中右击该文件,选重名名。将该文件重命名为NewsHeadlinesWebPart.cs。添加必要的程序集的引用我们将要编写的头条新闻筛选消费者WebPart是要从Microsoft.SharePoint.WebPartPages.WebPart类继承而来。因此,必须添加Windows SharePoint Services的程序集;由于使用到了Excel Services,还要引用Excel Services的程序集,以便允许使用其中的类。下面的步骤是展示了如何直接连接到Excel Web Serivces库。之所以如此是因为头条新闻WebPart运行域SharePoint环境中,这与使用SOAP通过HTTP访问Web service的方式不同。在我们的这种场景下直接连接可以提供更高的性能和安全性,并允许我们的头条新文WebPart可以工作在所有的SharePoint拓扑结构下。而当我们创建独立的Web应用程序(Web应用程序并不运行在SharePoint中)时,实际上是真正用到Web service的接口。关于何时选择使用SOAP方式通过HTTP访问,何时直接连接到Excel Web Services DLL,您可以参考这篇MOSS SDK文章:Loop-back SOAP Calls and Direct Linking如果Visual Studio运行在Office SharePoint Server 2007服务器上,请以下面的步骤进行引用:
项目->添加引用,出现添加引用对话框。 点击.NET标签,选择Excel Web Service 组件(Microsoft.Office.Excel.Server.WebServics.dll)。接着向下滚动,找到Windows SharePoint Services组件(Microsoft.SharePoint.dll),按住Ctrl键,再选择。 确定,完成引用的添加。如果Visual Studio与Office SharePoint Server 2007不在同一台机器上,我们需要从一台装有Office SharePoint Server 2007的机器上拷贝该文件到我们的开发环境所在机器的项目文件夹下。默认情况下,Microsoft.SharePoint.dll和Microsoft.Office.Excel.Server.WebServics.dll位于装有SharePoint的机器的以下目录中:
C: \ Program Files \ Common Files \ Microsoft Shared \ web server extensions \ 12 \ ISAPI拷贝过来后,将这两个文件添加到引用中。添加到本地拷贝的程序集的引用
项目->添加引用,出现添加引用对话框。 点击浏览,导航到放Windows SharePoint Services和Excel Web Service程序集文件的目录 选中Microsoft.SharePoint.dll 和Microsoft.Office.Excel.Server.WebServices.dll 文件。 确定,完成引用的添加。如果要使Excel Web Service正常工作,还需要添加到System.Web.Services程序集的引用:
项目->添加引用,出现添加引用对话框。 点击.NET标签,选择System.Web.Services 组件。 确定,完成引用的添加。设置WebPart程序集的版本号默认情况下,自定义Web控件项目的AssemblyVersion属性设为每次重新编译时自动增加。Web部件页通过Web.config文件中注册的版本号来识别WebPart。如果AssemblyVersion属性设为每次重新编译时自动增加,当我们把WebPart导入到Web部件页后又重新编译了该WebPart,就会因为找不到程序集而出错了。避免字增的方法就是手工指定一个版本号。
为WebPart程序集手工指定版本号
项目->NewsHeadlinesWebPart 属性。 在项目属性页面中,点应用程序标签。 点程序集信息 在程序集信息对话框中,设定版本为1.0.0.0 确定,保存。 关闭项目属性页。 为WebPart程序集进行强命名 为了使我们的WebPart可以部署到GAC(global assembly cache)中,供多个应用程序共享,我们必须为WebPart增加强命名。强名称由一个文本格式的名称,版本号,地区语言信息(如果提供了的话)和一个公钥数字签名组成。 在Visual Studio中为WebPart强命名 项目->NewsHeadlinesWebPart 属性。 在项目属性页面中,点签名标签。 在选择一个强名key文件处,点新建。 在创建强命名key文件对话框中,填写keypair文件名。取消下面的使用密码保护我的密钥的选择框。 关闭项目属性页。实现头条新闻Filter Consumer WebPart下面我们将创建一个类实现头条新闻筛选消费者部件。在代码文件头部添加下列引用。
using wsswebparts = Microsoft.SharePoint.WebPartPages; using aspnetwebparts = System.Web.UI.WebControls.WebParts; using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Collections.ObjectModel; using Microsoft.Office.Excel.Server.WebServices;引用这些命名空间可以使我们方便的使用其中的类库和类型,而不必通过使用完整的命名空间路径来访问。我们来编写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. if (sessionId == null ) { ShowError( " Error opening workbook. Check the URL in " + " OpenWorkbook, and be sure that the workbook is in " + " a trusted location " ); return ; } // Retrieve the headlines/regions currently defined in the // workbook from Excel Services and add them to the collection of // headlines. Or, if there is a problem getting the headlines, // show an error to the user. // The code shown below assumes the following: // // - The opened workbook contains a worksheet named "Sheet1". // - The "Sheet1" worksheet contains a range named "Headlines". // - The "Headlines" range is two columns wide with the first // column containing a news headline and the second column // containing a region. object [] AllHeadlines = es.GetRangeA1(sessionId, " Sheet1 " , " Headlines " , true , out status); if (AllHeadlines != null ) { unfilteredHeadlines = new List < Headline > (); foreach ( object [] HeadlineRow in AllHeadlines) { unfilteredHeadlines.Add( new Headline(Convert.ToString(HeadlineRow[ 0 ]), Convert.ToString(HeadlineRow[ 1 ]))); } } else { ShowError( " Error getting headlines from workbook. " ); return ; } } private void ShowError( string message) { // Show an error message to the user, and remove all other // controls from the Web Part. lblError.Text = message; this .Controls.Clear(); this .Controls.Add(lblError); } // Use the ConnectionConsumer attribute to specify a callback // method that the Web Part framework can use to provide filter // provider instances. [aspnetwebparts.ConnectionConsumer( " News Headlines " , " IFilterValues " , AllowsMultipleConnections = true )] public void SetConnectionInterface( wsswebparts.IFilterValues filterProvider) { if (filterProvider != null ) { // Add the filter provider to the list of providers. this .filterProviders.Add(filterProvider); // Tell the provider the parameter we are looking for. List < wsswebparts.ConsumerParameter > l = new List < wsswebparts.ConsumerParameter > (); l.Add( new wsswebparts.ConsumerParameter( " Region " ,wsswebparts.ConsumerParameterCapabilities.SupportsMultipleValues | wsswebparts.ConsumerParameterCapabilities.SupportsAllValue)); filterProvider.SetConsumerParameters( new ReadOnlyCollection < wsswebparts.ConsumerParameter > (l)); } } protected override void OnPreRender(EventArgs e) { this .EnsureChildControls(); // Call Excel Web Services to get the list of all // news headlines. GetHeadlinesUsingWebService(); // The filtering logic performs a union of all of the // filters (a logical OR). If we didn't get any filter // providers or if any of the filters send the "All" value // (that is, provider.ParameterValues == null), we don't // need to filter and we can return all of the // headlines. List < Headline > filteredHeadlines = null ; bool shouldFilter = true ; if ( this .filterProviders.Count == 0 ) { shouldFilter = false ; } else if ( this .filterProviders.Count > 0 ) { foreach (wsswebparts.IFilterValues filterProvider in this .filterProviders) { if (filterProvider.ParameterValues == null ) { // Some filter sent "All"--don't bother with the // rest of the filtering. shouldFilter = false ; break ; } } } if ( ! shouldFilter) { // The "filtered" headlines are unfiltered. filteredHeadlines = this .unfilteredHeadlines; } else { // Just fill in the headlines that match the filters. filteredHeadlines = new List < Headline > (); // Create a lookup from region to a list of headlines that // correspond to that region. Dictionary < string , List < Headline >> regionHeadlineMap = new Dictionary < string , List < Headline >> (); foreach (Headline headline in this .unfilteredHeadlines) { List < Headline > headlinesForRegion = null ; if ( ! regionHeadlineMap.TryGetValue(headline.Region, out headlinesForRegion)) { headlinesForRegion = new List < Headline > (); regionHeadlineMap.Add(headline.Region, headlinesForRegion); } headlinesForRegion.Add(headline); } foreach (wsswebparts.IFilterValues filterProvider in this .filterProviders) { ReadOnlyCollection < String > values = filterProvider.ParameterValues; if (values != null ) { foreach ( string v in values) { if (v == null ) { // This indicates the "Empty" value, which // doesn't apply to headlines, because // they all have regions. } else { List < Headline > matchedHeadlines; if (regionHeadlineMap.TryGetValue(v, out matchedHeadlines)) { foreach (Headline matchedHeadline in matchedHeadlines) { if ( ! filteredHeadlines.Contains(matchedHeadline)) {filteredHeadlines.Add(matchedHeadline); } } } } } } } } // Display the filtered headlines. headlinesDataGrid.DataSource = filteredHeadlines; headlinesDataGrid.DataBind(); base .OnPreRender(e); }}查看视频
转载于:https://www.cnblogs.com/Sunmoonfire/archive/2007/06/05/771414.html
相关资源:数据结构—成绩单生成器