MSDN Visual系列:在WSSv3中编写一个Filter Provider WebPart

it2022-05-05  60

原文:http://msdn2.microsoft.com/en-us/library/bb457205.aspxWSSv3通过ASP.NET WebPart连接框架提供WebPart之间连接的编程。我们可以通过这种模式得到用户提供的搜索或过滤条件。WebPart连接基于“提供者(providers)”和“消费者(consumers)”体系之下。一个provider WebPart可以通过我们程序的界面提供信息给一个或多个consumer WebPart。本文将举例说明如何在wssv3中创建一个Filter Provider WebPart 来实现地理区域的筛选。该WebPart将显示一个界面供用户选择一个或多个地理区域。在接下来的系列文章中,我们会介绍如何创建一个头条新闻的filter consumer WebPart来和本文的地理位置filter provider WebPart配合使用,望关注。下面是创建filter provider WebPart的五大步骤(如果您熟悉WebPart开发,可跳过。直接看代码实现部分)

在Visual Studio 2005中创建一个web control library项目。 添加到Microsoft.SharePoint.dll的引用。 设置assembly的版本号。 为assembly添加强命名。 编写地理区域filter provider webpart的代码。

在Visual Studio 2005中创建一个web control library项目

创建一个filter provider WebPart最简单的方式是套用Visual Studio 2005的自定义Web控件模板。下面是创建的步骤:

文件->新建->项目,出现新建项目对话框。 在项目类型中,选择C#,选择Windows类别。 在模板选择中,选择web控件库。 为项目指定一个名称RegionFilterWebPart。 指定保存的位置,确定。现在,项目中包含一个名为WebCustomControl1.cs的代码文件。 在解决方案浏览器中右击该文件,选重名名。将该文件重命名为RegionFilterWebPart.cs。

添加到SharePoint程序集的引用我们将要编写的地理区域筛选提供者WebPart是要从Microsoft.SharePoint.WebPartPages.WebPart类继承而来。因此,必须先添加Windows SharePoint Services的程序集,以便允许使用该类。如果Visual Studio运行在Office SharePoint Server 2007服务器上,请以下面的步骤进行引用:

项目->添加引用,出现添加引用对话框。 点击.NET标签,选择Windows SharePoint Services组件(Microsoft.SharePoint.dll)。 确定,完成引用的添加。

如果Visual Studio与Office SharePoint Server 2007不在同一台机器上,我们需要从一台装有Office SharePoint Server 2007的机器上拷贝该文件到我们的开发环境所在机器上。默认情况下,Microsoft.SharePoint.dll位于装有SharePoint的机器的以下目录中:

C: \ Program Files \ Common Files \ Microsoft Shared \ web server extensions \ 12 \ ISAPI 

拷贝过来后,将这个文件添加到引用中。添加到本地拷贝的SharePoint程序集的引用

项目->添加引用,出现添加引用对话框。 点击浏览,导航到放置Microsoft.SharePoint.dll文件的目录,选中该文件。 确定,完成引用的添加。

设置WebPart程序集的版本号默认情况下,自定义Web控件项目的AssemblyVersion属性设为每次重新编译时自动增加。Web部件页通过Web.config文件中注册的版本号来识别WebPart。如果AssemblyVersion属性设为每次重新编译时自动增加,当我们把WebPart导入到Web部件页后又重新编译了该WebPart,就会因为找不到程序集而出错了。避免字增的方法就是手工指定一个版本号。

为WebPart程序集手工指定版本号

项目->RegionFilterWebPart属性。在项目属性页面中,点应用程序标签。点程序集信息在程序集信息对话框中,设定版本为1.0.0.0 确定,保存。关闭项目属性页。 为WebPart程序集进行强命名 为了使我们的WebPart可以部署到GAC(global assembly cache)中,供多个应用程序共享,我们必须为WebPart增加强命名。强名称由一个文本格式的名称,版本号,地区语言信息(如果提供了的话)和一个公钥数字签名组成。 在Visual Studio中为WebPart强命名 项目->RegionFilterWebPart属性。在项目属性页面中,点签名标签。在选择一个强名key文件处,点新建。在创建强命名key文件对话框中,填写keypair文件名。取消下面的使用密码保护我的密钥的选择框。 关闭项目属性页。

实现地理区域Filter Provider 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;

引用这些命名空间可以使我们方便的使用其中的类库和类型,而不必通过使用完整的命名空间路径来访问。我们来编写RegionFilterWebPart的代码。

首先来创建要显示在界面上供用户选择地理区域的复选框。实现ITransformableFilterValues接口,以使得我们的地理区域筛选WebPart可以连接filter consumer WebPart。开放一个provider连接点将用户选择的区域值作为ITransformableFilterValues接口的实例返回。

用下面的代码替换现有的整个RegionFilterWebPart类定义。

public   class  RegionFilterWebPart:    wsswebparts.WebPart, wsswebparts.ITransformableFilterValues{    CheckBoxList cblRegionList;    ListItem cbitemRegion;     protected   override   void  CreateChildControls()    {        cblRegionList  =   new  CheckBoxList();        cblRegionList.AutoPostBack  =   true ;        Controls.Add(cblRegionList);        cbitemRegion  =   new  ListItem();        cbitemRegion.Text  =   " Redmond " ;        cblRegionList.Items.Add(cbitemRegion);        cbitemRegion  =   null ;        cbitemRegion  =   new  ListItem();        cbitemRegion.Text  =   " Seattle " ;        cblRegionList.Items.Add(cbitemRegion);        cbitemRegion  =   null ;                cbitemRegion  =   new  ListItem();        cbitemRegion.Text  =   " US " ;        cblRegionList.Items.Add(cbitemRegion);        cbitemRegion  =   null ;        cbitemRegion  =   new  ListItem();        cbitemRegion.Text  =   " World " ;        cblRegionList.Items.Add(cbitemRegion);        cbitemRegion  =   null ;        cbitemRegion  =   new  ListItem();        cbitemRegion.Text  =   " All " ;        cblRegionList.Items.Add(cbitemRegion);        cbitemRegion  =   null ;         base .CreateChildControls();    }     //  Implementations of the ITransformableFilterValues properties.     [wsswebparts.WebPartStorage(wsswebparts.Storage.None)]     public   virtual   bool  AllowMultipleValues    {         get         {             return   true ;        }    }    [wsswebparts.WebPartStorage(wsswebparts.Storage.None)]     public   virtual   bool  AllowAllValue    {         get         {             return   true ;        }    }    [wsswebparts.WebPartStorage(wsswebparts.Storage.None)]     public   virtual   bool  AllowEmptyValue    {         get         {             return   false ;        }    }        [wsswebparts.WebPartStorage(wsswebparts.Storage.None)]     public   virtual   string  ParameterName    {         get         {             return   " Geographic Region " ;        }    }    [wsswebparts.WebPartStorage(wsswebparts.Storage.None)]     public   virtual  ReadOnlyCollection < string >  ParameterValues    {         get         {             string [] values  =   this .GetCurrentlySelectedGeographies();             return  values  ==   null   ?                  null  :                 new  ReadOnlyCollection < string > (values);        }    }        public   string [] GetCurrentlySelectedGeographies()    {        String[] choices  =   new  String[ 5 ];         bool  anythingSelected  =   false ;         //  It's possible for the ParameterValues property to be accessed         //  before CreateChildControls has been called, so ensure the         //  Region List combobox has been created.          if  (cblRegionList  ==   null )        {            choices  =   null ;             return  choices;        }         for  ( int  i  =   0 ; i  <  cblRegionList.Items.Count; i ++ )        {             //  Get the selected choices              if  (cblRegionList.Items[i].Selected)            {                anythingSelected  =   true ;                 if  (cblRegionList.Items[i].Text  !=   " All " )                {                    choices[i]  =  cblRegionList.Items[i].Text;                }                 else                 {                    choices  =   null ;                     return  choices;                }            }        }                 if  ( ! anythingSelected)            choices  =   null ;         return  choices;    }     //  Use the ConnectionProvider attribute to specify the method that     //  the Web Part framework should call to allow us to return an instance     //  of our ITransformableFilterValues interface.     [aspnetwebparts.ConnectionProvider( " Region Filter " ,      " ITransformableFilterValues " , AllowsMultipleConnections  =   true )]     public  wsswebparts.ITransformableFilterValues     SetConnectionInterface()    {         return   this ;    }     protected   override   void  RenderContents(HtmlTextWriter output)    {         this .EnsureChildControls();        RenderChildren(output);    }}

在wssv3中,filter WebPart通过WebPart连接将筛选条件从一个WebPart(提供者WebPart)传递到另一个WebPart(消费者WebPart)。这使得我们可以编写更加成熟的应用,通过允许用户提供搜索或筛选的条件来整合2007 Microsoft Office system。(在接下来的系列文章中,将会有通过WebPart调用Excel Web Services的内容,请关注)查看视频

转载于:https://www.cnblogs.com/Sunmoonfire/archive/2007/06/04/769780.html

相关资源:数据结构—成绩单生成器

最新回复(0)