65.9K
CodeProject 正在变化。 阅读更多。
Home

连接 SharePoint 中的自定义 Web 部件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.63/5 (18投票s)

2009 年 6 月 10 日

CPOL

4分钟阅读

viewsIcon

167184

downloadIcon

1290

介绍如何使用 Visual Studio 扩展 for Windows SharePoint Services 连接两个 SharePoint 自定义 Web 部件。

article.gif

引言

SharePoint 是一个对用户和开发人员都丰富而强大的平台。我们可以通过多种方式自定义和丰富 SharePoint。创建自定义 Web 部件是其中一种常见方式。通过自定义 Web 部件,我们可以快速而清晰地实现我们的功能。MOSS Web 部件的一个有用功能是能够发送和接收参数。我们可以轻松地根据另一个 Web 部件的结果来筛选数据视图 Web 部件。了解我们也可以连接自己的自定义 Web 部件是很重要的。

要求

本文创建的 Web 部件将使用 Windows SharePoint Services 3.0。它也能与 MOSS 2007 一起使用。要创建 Web 部件,我们将使用安装在 Windows Server 2003 上的 Visual Studio 2005,以及 Windows SharePoint Services 1.1 扩展(您也可以使用 Visual Studio 2008 和 WSS 1.2 扩展)。WSS 或 MOSS 也已安装。您可以使用 Microsoft Virtual PC 在开发机上设置这样的环境。

在 Visual Studio 中创建 ProviderWebPart

  1. 在 Visual Studio 中创建一个 Web 部件项目,并命名为 ProviderWebPart。
  2. addnewwebpart.gif

  3. 在解决方案资源管理器中删除 WebPart1 文件夹。
  4. 右键单击项目,选择“添加新项”,然后选择“Web 部件”。将 Web 部件的名称更改为 ProviderWebPart。
  5. 在 Web 部件类中使用以下代码
  6. public class ProviderWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        TextBox txt;
        Button btn;
    
        public ProviderWebPart()
        {
            this.Title = "Provider WebPart";
            this.ExportMode = WebPartExportMode.All;
        }
    
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
    
            Table tbl;
            TableRow row;
            TableCell cell;
    
            // A table for layout 
            tbl = new Table();
            row = new TableRow();
            cell = new TableCell();
            
            // first row for title
            cell.ColumnSpan = 2;
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            Label lblTitle = new Label();
            lblTitle.Text = "This WebPart will send a parameter to a consumer:";
            cell.Controls.Add(lblTitle);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);
    
            // second row of table for textbox and button
            row = new TableRow();
            cell = new TableCell();
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            txt = new TextBox();
            txt.Text = "";
            txt.Width = Unit.Pixel(120); ;
            cell.Controls.Add(txt);
            row.Controls.Add(cell);
            cell = new TableCell();
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            btn = new Button();
            btn.Text = "Send...";
            btn.Click += new EventHandler(btn_Click);
            cell.Controls.Add(btn);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);
            
            //add table to webpart
            this.Controls.Add(tbl);
        }
    
        void btn_Click(object sender, EventArgs e)
        {
            // will be added later
        }
    }

    注意:上面的代码将渲染 ProviderWebPart 的界面。

  7. 现在,我们声明一个用于两个 Web 部件之间通信的接口。我们必须创建一个具有强名称的类库项目。要做到这一点,请转到“文件”菜单,选择“添加新项目”,然后选择“类库”。将其命名为 CommunicationInterface。
  8. class1.cs 重命名为 ICommunicationInterface.cs,并将以下代码替换到该文件中
  9. namespace CommunicationInterface
    {
        public interface ICommunicationInterface
        {
            string Parameter1 { get; }
        }
    }

    正如您所看到的,我们声明了一个带有一个参数的通信接口。您可以扩展它并添加自己的参数。

  10. 右键单击 CommunicationInterface,选择“属性”;在“签名”选项卡中,勾选“为程序集签名”,然后在下拉列表中选择“<新建...>”,将文件命名为 key1.snk,并取消勾选“使用密码保护密钥文件”。
  11. strong.gif

  12. 右键单击并生成 CommunicationInterface 项目。
  13. 在 ProviderWebPart 项目中,右键单击并选择“添加引用”,然后选择“项目”选项卡和 CommunicationInterface 项目。
  14. 双击 ProviderWebPart.cs。现在,我们必须在 Web 部件类中实现该接口,因此将此代码添加到类中
  15. Use CommunicationInterface;

    并将类声明更改为

    public class ProviderWebPart : 
           System.Web.UI.WebControls.WebParts.WebPart, 
           ICommunicationInterface
  16. 使用以下代码实现 Web 部件类中的 Parameter1 属性
  17. // implement the Parameter1 property from interface
    protected string _parameter1 = "";
    public string Parameter1
    {
        get { return _parameter1; }
    }
  18. 创建返回接口并使用 ConnectionProvider 属性进行装饰的属性。ConnectionProvider 的参数是连接的显示名称和实际名称(ID)。当我们声明多个连接提供程序和使用者(如 SharePoint Web 部件)时,我们必须为 Web 部件中的连接 ID 选择一个唯一的名称。
  19. // create a property that return the interface reference
    // and decorate it with ConnectionProvider
    [ConnectionProvider("Parameter1 Provider", 
                        "Parameter1 Provider")]
    public ICommunicationInterface ConnectionInterface()
    {
        return this;
    }
  20. 将以下代码添加到 btn_click 事件中
  21. // set connection provider property with required textbox info.
    this.localParameter1 = txt.Text;

    现在,ProviderWebPart 的整个代码必须是这样的

    public class ProviderWebPart : 
           System.Web.UI.WebControls.WebParts.WebPart, 
           ICommunicationInterface
    {
        TextBox txt;
        Button btn;
    
        // implement the Parameter1 property from interface
        protected string _parameter1 = "";
        public string Parameter1
        {
            get { return _parameter1; }
        }
    
        // create a property that return the interface reference
        // and decorate it with ConnectionProvider
        [ConnectionProvider("Parameter1 Provider", 
                            "Parameter1 Provider")]
        public ICommunicationInterface ConnectionInterface()
        {
            return this;
        }
    
        public ProviderWebPart()
        {
            this.Title = "Provider WebPart";
            this.ExportMode = WebPartExportMode.All;
        }
    
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
    
            Table tbl;
            TableRow row;
            TableCell cell;
    
            // A table for layout 
            tbl = new Table();
            row = new TableRow();
            cell = new TableCell();
            
            // first row for title
            cell.ColumnSpan = 2;
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            Label lblTitle = new Label();
            lblTitle.Text = "This WebPart will send a parameter to a consumer:";
            cell.Controls.Add(lblTitle);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);
    
            // second row of table for textbox and button
            row = new TableRow();
            cell = new TableCell();
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            txt = new TextBox();
            txt.Text = "";
            txt.Width = Unit.Pixel(120); ;
            cell.Controls.Add(txt);
            row.Controls.Add(cell);
            cell = new TableCell();
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            btn = new Button();
            btn.Text = "Send...";
            btn.Click += new EventHandler(btn_Click);
            cell.Controls.Add(btn);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);
            
            //add table to webpart
            this.Controls.Add(tbl);
            
        }
    
        void btn_Click(object sender, EventArgs e)
        {
        // set connection provider property with required textbox info.
            this._parameter1 = txt.Text;
        }
    }
  22. 在属性文件夹中,双击 AssemblyInfo.cs 并更改
  23. [assembly: CLSCompliant(true)] 

    to

    [assembly: CLSCompliant(false)]

    clscompliant.gif

  24. 为了成功部署 Web 部件,我们需要将 CommunicationInterface DLL 添加到 GAC。因此,打开 Windows 资源管理器并浏览:c:\windows\assembly。然后,打开另一个资源管理器实例并浏览:[您的解决方案路径]\ProviderWebPart\CommunicationInterface\bin\Debug。现在,将 CommunicationInterface.dll 拖放到 c:\windows\assembly 中。
  25. 右键单击 Web 部件项目,选择“生成”和“部署”。
  26. 现在,ProviderWebPart 可以在 SharePoint 中使用了,我们必须创建一个使用者 Web 部件。

在 Visual Studio 中创建 ConsumerWebPart

  1. 在“文件”菜单中,向解决方案添加一个新的 Web 部件项目,并命名为 ConsumerWebPart。
  2. 在解决方案资源管理器中删除 WebPart1 文件夹。
  3. 右键单击项目,选择“添加新项”,然后选择“Web 部件”。将 Web 部件的名称更改为 ConsumerWebPart。
  4. 在 ConsumerWebPart 项目中,右键单击并添加对 CommunicationInterface 项目的引用。
  5. 在属性文件夹中,双击 AssemblyInfo.cs 并更改
  6. [assembly: CLSCompliant(true)]

    to

    [assembly: CLSCompliant(false)]
  7. 双击 ConsumerWebPart.cs 并在类上方添加以下代码
  8. using CommunicationInterface;
  9. ConsumerWebPart 类中,添加以下代码
  10. public class ConsumerWebPart : 
           System.Web.UI.WebControls.WebParts.WebPart
    {
        Label lblTitle;
        Label lblResult;
    
        ///// the string info consumer from custom reciever   //
        ICommunicationInterface connectionInterface = null;
        // The consumer webpart  must define a method that
        // would accept the interface as an parameter
        // and must be decorated with ConnectionConsumer attribute      
        [ConnectionConsumer("Parameter1 Consumer", 
                            "Parameter1 Consumer")]
        public void GetConnectionInterface(ICommunicationInterface 
                                           _connectionInterface)
        {
            connectionInterface = _connectionInterface;
        }
        /////////////////////////////////////////////////////////  
    
        public ConsumerWebPart()
        {
            this.Title = "Consumer WebPart";
            this.ExportMode = WebPartExportMode.All;
        }
    
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
    
            Table tbl;
            TableRow row;
            TableCell cell;
    
            // A table for layout 
            tbl = new Table();
            row = new TableRow();
            cell = new TableCell();
    
            // first row for title
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            lblTitle = new Label();
            lblTitle.Text = "This WebPart will recieve " + 
                            "a parameter from a provider:";
            cell.Controls.Add(lblTitle);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);
    
            //second row for result
            row = new TableRow();
            cell = new TableCell();
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            lblResult = new Label();
            //check the connectionInterface for recieving the parameter1
            if (connectionInterface != null)
            {
                lblResult.Text = connectionInterface.Parameter1+ 
                                 " is recieved!";
            }
            else
            {
                lblResult.Text = "nothing is recieved!";
            }
            cell.Controls.Add(lblResult);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);
    
            //add table to webpart
            this.Controls.Add(tbl);
        }
    }
  11. 生成并部署 ConsumerWebPart 项目。
  12. solution.gif

在 SharePoint 中使用已创建的 Web 部件并连接它们

在创建和部署我们的 Web 部件后,我们可以在 SharePoint 环境中检查其功能。

  1. 浏览您的 SharePoint 网站并编辑 Web 部件页面,例如 default.aspx。使用“站点操作”和“编辑页面”。
  2. 单击 Web 部件区域中的“添加 Web 部件”;您已部署的 Web 部件应出现在“其他”类别中。勾选 ProviderWebPart 和 ConsumerWebPart,然后单击“添加”。
  3. addwebpart.gif

  4. 现在,您可以连接您的 Web 部件了。单击 ConsumerWebPart 中的“编辑”,然后在“连接”中;勾选“从 ProviderWebPart 获取 Paramet1 消费者”。
  5. 现在,单击页面顶部的“退出编辑模式”。
  6. 检查我们 Web 部件的功能。键入一个单词,然后单击“发送...”按钮,并检查 ConsumerWebPart。
  7. result.gif

其他改进

演示的示例很简单。您可以定义 CommunicationInterface 中的参数,并在 Web 部件之间传输它们。每个 Web 部件都可以同时是使用者和提供者。

结论

示例代码表明,我们可以像 Microsoft SharePoint Server Web 部件一样连接我们的自定义 Web 部件。因此,我们可以开发更高级和通用的连接 Web 部件。

历史

  • 2009/06/10 - 版本 1.0。
© . All rights reserved.