MSDN Visual系列:在SharePoint中编写调用Excel Services的Filter Consumer WebPart

本文将省略在上一篇文章中讲到的一些内容。请先阅读上一片“ MSDN Visual系列:在WSSv3中编写一个Filter Provider WebPart”。

原文: http://msdn2.microsoft.com/en-us/library/bb460810.aspx

本文将举例说明如何在wssv3中创建一个Filter consumer WebPart 来显示头条新闻。这些头条新闻内容依赖于地理区域的筛选提供者WebPart中用户的选择。头条新闻WebPart将通过调用Microsoft Excel Services从一个Excel工作簿中得到。

下面是创建filter consumer WebPart的五大步骤
  1. 在Visual Studio 2005中创建一个web control library项目。
  2. 添加到Microsoft.SharePoint.dll的引用。
  3. 设置assembly的版本号。
  4. 为assembly添加强命名。
  5. 编写头条新闻filter consumer webpart的代码。

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

创建一个filter consumer WebPart最简单的方式是套用Visual Studio 2005的自定义Web控件模板。

下面是创建的步骤:

  1. 文件->新建->项目,出现新建项目对话框。
  2. 在项目类型中,选择C#,选择Windows类别。
  3. 在模板选择中,选择web控件库。
  4. 为项目指定一个名称NewsHeadlinesWebPart
  5. 指定保存的位置,确定。现在,项目中包含一个名为WebCustomControl1.cs的代码文件。
  6. 在解决方案浏览器中右击该文件,选重名名。将该文件重命名为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服务器上,请以下面的步骤进行引用:

  1. 项目->添加引用,出现添加引用对话框。
  2. 点击.NET标签,选择Excel Web Service 组件(Microsoft.Office.Excel.Server.WebServics.dll)。接着向下滚动,找到Windows SharePoint Services组件(Microsoft.SharePoint.dll),按住Ctrl键,再选择。
  3. 确定,完成引用的添加。

如果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 

拷贝过来后,将这两个文件添加到引用中。

添加到本地拷贝的程序集的引用

  1. 项目->添加引用,出现添加引用对话框。
  2. 点击浏览,导航到放Windows SharePoint Services和Excel  Web Service程序集文件的目录
    选中Microsoft.SharePoint.dll 和Microsoft.Office.Excel.Server.WebServices.dll 文件。
  3. 确定,完成引用的添加。

如果要使Excel Web Service正常工作,还需要添加到System.Web.Services程序集的引用: 

  1. 项目->添加引用,出现添加引用对话框。
  2. 点击.NET标签,选择System.Web.Services 组件。
  3. 确定,完成引用的添加。

设置WebPart程序集的版本号

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

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

  1. 项目->NewsHeadlinesWebPart 属性。
  2. 在项目属性页面中,点应用程序标签。
  3. 点程序集信息
  4. 在程序集信息对话框中,设定版本为1.0.0.0
  5. 确定,保存。
  6. 关闭项目属性页。
为WebPart程序集进行强命名

为了使我们的WebPart可以部署到GAC(global assembly cache)中,供多个应用程序共享,我们必须为WebPart增加强命名。强名称由一个文本格式的名称,版本号,地区语言信息(如果提供了的话)和一个公钥数字签名组成。

在Visual Studio中为WebPart强命名

  1. 项目->NewsHeadlinesWebPart 属性。
  2. 在项目属性页面中,点签名标签。
  3. 在选择一个强名key文件处,点新建。
  4. 在创建强命名key文件对话框中,填写keypair文件名。取消下面的使用密码保护我的密钥的选择框。
  5. 关闭项目属性页。

实现头条新闻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 类的代码。

  1. 首先来创建用于显示头新闻到用户界面上的DataGrid 控件。
  2.  开放一个consumer连接点用来接收地理区域筛选提供者WebPart发送来的IFilterValues接口。
  3. 在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随着SharePoint Conference 2009在Las Vegas的开幕,神秘的SharePoint Server 2010的真实面目也终于揭晓。新的SharePoint究竟都带来了哪些新功能,如果升级又要考虑什么呢?这一系列博客将为您详细解说这些要点。 新的服务架构和服务应用 SharePoint 2010引人注目的一点是服务架构的整体改变。以前的共享服务Shared Service Provider不复存在,取而代之的是独立的Shared Service Application。不同服务之间既可以互相依存,也可以独立存在,用户应用可以根据需求选择调用哪些服务,从而最大限度节省系统资源和带宽,同时独立互相之间的进程。这种做法提高了安全性和扩展性,使得单一站点多服务应用成为可能,而跨服务器场服务调用也可以实现。 同时,SharePoint 2010 也引入了一系列新服务应用,举例如下: Access Service 用户可以使用Access设计好自己的应用,直接发布到SharePoint 2010上展现并使用。而Access自身也更加易用,快速应用开发并发布易如反掌。 Visio Service 用Visio设计工作流,并直接导入SharePoint控制公文流转?在2007上这只能通过一些合作伙伴的解决方案实现,现在这是2010的自带功能! User Code Service SharePoint默认的功能不满意,想上传自己的代码或者别人提供的Webpart但管理员死活不同意?User Code Service可以帮你的忙。这是一个类似于沙盒的功能,在开发阶段我们的确称它为Sandboxed Solution,它将用户上传的解决方案放在沙盒执行,保证服务器场的安全性,同时管理员可以控制这些代码占用资源的多少以免出现问题。 Web Analytic Service 顾名思义,Web分析服务。通过分析用户的各种访问行为,管理员可以轻松了解到服务器的访问状况,内容分布等。 PerformancePoint Service PerformancePoint Server在2010里被纳入了SharePoint,成为其服务组件的一部分。更强大的商务智能展现报表服务,更华丽的图形,是这一版本PPS的侧重点。毕竟吸引眼球、洞察问题是商务智能最重要的用途。 Office Web Application 严格的说这是一组服务,包括Excel Service, Word Service, PowerPoint Service(非具体名称)。通过这些服务,通过网页方式查看编辑Office文档成为可能。这种页面编辑方式兼容性良好。 这些服务各自也有其他用途,比如说Excel Service的实时计算并生成图表供其他应用调用,PowerPoint Service的通过Silverlight在搜索结果展现PPT预览图,都给用户带来很不一样的体验。 Managed Metadata Service 受控元数据服务在SharePoint 2010无所不在,他是标签和分类的基础。通过心统一管理元数据分类和词汇表,SharePoint上的文档管理可以使得企业数据的分类更有序,知识重复利用率得到提高,检索效率也得到提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值