使用 LINQ 基于 SharePoint 2010 数据创建 Silverlight 关系图

SharePoint 快速入门横幅

SharePoint 2010 中的 Web 开发入门创建 Microsoft Silverlight 应用程序,以便使用 LINQ 检索 Microsoft SharePoint 2010 列表数据,然后在图表中显示此类数据。

上次修改时间: 2011年1月27日

适用范围: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio 2010

本文内容
创建 Silverlight 应用程序项目
添加代码来访问列表数据并将其显示在 Silverlight 关系图控件中
部署并测试解决方案
后续步骤

在本练习中,您将创建一个 Microsoft Silverlight 3 应用程序,以便使用 Silverlight 图表控件在图表中显示 SharePoint 列表数据。若要完成此任务,您必须执行以下操作:

  • 创建 Silverlight 应用程序项目

  • 添加代码来访问列表数据并将其显示在 Silverlight 关系图控件中

  • 部署并测试解决方案

本练习假定您已完成创建 Silverlight 应用程序以访问 SharePoint 2010 数据。该主题介绍如何创建 Microsoft Silverlight 3 应用程序,以便检索 SharePoint 列表数据,然后将数据显示在网站的数据网格控件中。

创建 Silverlight 应用程序项目

在该任务中,您将在 Microsoft Visual Studio 2010 中创建 Silverlight 应用程序项目。

创建 Silverlight 项目

  1. 启动 Visual Studio 2010,单击"文件",指向"新建",然后单击"项目"。

  2. 在"已安装的模板"部分,导航到"其他项目类型"节点,单击"Visual Studio 解决方案",然后单击"空白解决方案"。

  3. 在屏幕顶部,选择".NET Framework 3.5",在"名称"框中键入 Begin,然后单击"确定"。

  4. 在"文件"菜单上,指向"新建",然后单击"项目"。

  5. 在"已安装的模板"部分,展开"Visual C#"节点,单击"Silverlight",然后单击"Silverlight 应用程序"。

  6. 在屏幕顶部,选择".NET Framework 3.5",在"名称"框中键入 SilverlightEmployeeContributionsGraph,然后单击"确定"。

  7. 在"新建 Silverlight 应用程序"对话框屏幕中,单击"确定"。

  8. 接下来,添加对 SharePoint Silverlight 客户端对象模型的引用。在解决方案资源管理器中,右键单击"SilverlightEmployeeContributionsGraph"节点,然后单击"添加引用"。

  9. 导航到以下文件夹: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin。

  10. 选择"Microsoft.SharePoint.ClientSilverlight.dll"和"Microsoft.SharePoint.Client.Silverlight.Runtime.dll",然后单击"确定"。

  11. 接下来,添加对 Silverlight 图表控件程序集的引用。该程序集位于"添加引用"对话框屏幕中的".NET"选项卡上,名为"System.Windows.Controls.DataVisualization.Toolkit"。

    备注

    如果在"添加引用"对话框屏幕中未看到此程序集,则可通过安装 Microsoft Silverlight 工具包(该链接可能指向英文页面) 来获取它。

添加代码来访问列表数据并将其显示在 Silverlight 关系图控件中

在该任务中,您将添加代码,该代码允许您使用 LINQ 和 Silverlight 图表控件来访问 SharePoint 列表数据,以将此类数据显示在图表中。

将代码添加到项目

  1. 在解决方案资源管理器中,右键单击"App.xaml"文件,然后单击"查看代码"。将以下 using 语句添加到该文件的顶部。

    using Microsoft.SharePoint.Client;
    using System.Threading;
    
  2. 将以下代码添加到 Application_Startup 方法的开头。

    ApplicationContext.Init(e.InitParams, SynchronizationContext.Current);
    
  3. 在"MainPage.xaml"文件的 XAML 视图中,将以下 XML 命名空间添加到 UserControl 元素中。

    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    
  4. 在 grid 元素中添加以下 Silverlight 图表控件。

    <chartingToolkit:Chart x:Name="chart" Width="350" Height="250" Title="Team Contributions">
        <chartingToolkit:Chart.Series>
            <chartingToolkit:ColumnSeries ItemsSource="{Binding}" 
                DependentValuePath="Contributions" 
                IndependentValuePath="Name"
                AnimationSequence="FirstToLast"   
                Title="Contributions" IsSelectionEnabled="True" />
        </chartingToolkit:Chart.Series>
    </chartingToolkit:Chart>
    
  5. 打开"MainPage.xaml.cs",然后将以下 using 语句添加到该文件的顶部。

    using Microsoft.SharePoint.Client;
    
  6. 将以下类添加到 MainPage 类前面。

    public class EmployeeContributions
    {
        public string Name { get; set; }
        public string TeamName { get; set; }
        public decimal Contributions { get; set; }
    }
    
    public class TeamContributions
    {
        public string Name { get; set; }
        public decimal Contributions { get; set; }
    }
    
  7. 将以下变量添加到 MainPage 类中。

    private ListItemCollection _employees;
    
  8. 在调用 initializeComponent 后向 MainPage 方法中添加以下代码。

    ClientContext context = new ClientContext(ApplicationContext.Current.Url);
    context.Load(context.Web);
    List employees = context.Web.Lists.GetByTitle("Employees");
    context.Load(employees);
    
    CamlQuery query = new CamlQuery();
    string camlQueryXml = null;
    
    query.ViewXml = camlQueryXml;
    _employees = employees.GetItems(query);
    context.Load(_employees);
    context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);
    
  9. 在 MainPage 方法后添加以下代码。

    private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
    {
        // This is not called on the UI thread.
        Dispatcher.BeginInvoke(BindData);
    }
    
    private void BindData()
    {
        List<EmployeeContributions> employees = new List<EmployeeContributions>();
    
            // Get the list item values into a strongly-typed class.
            foreach (ListItem li in _employees)
            {
                employees.Add(new EmployeeContributions
                {
                    Name = li["Title"].ToString(),
                    TeamName = li["Team"].ToString(),
                    Contributions = Convert.ToDecimal(li["Contribution_x0020__x0028_in_x00"])
                });
            }
    
            // Use LINQ to organize employees by team name and then total team contributions.
             List<TeamContributions> teams = employees
                 .GroupBy(e => e.TeamName)
                 .Select(t => new TeamContributions
                 {
                     Name = t.Key,
                     Contributions = t.Sum(e => e.Contributions)
                 }).ToList();
             // This must be on a UI thread.
             chart.DataContext = teams; 
    }
    

    借助添加的代码,SharePoint Silverlight 客户端对象模型从 SharePoint 列表中检索数据。如果将员工贡献项添加到该列表,则 LINQ 可用于将员工组织到各团队,并汇总他们的贡献。随后可将团队贡献设置为图表的数据源。

部署并测试解决方案

在该任务中,您将部署并测试解决方案,方法是将 Silverlight Web 部件添加到网站的页面上。

测试解决方案

  1. 若要将解决方案部署到 SharePoint,通过 Silverlight 项目创建生成的结果 .xap 文件必须位于 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin 文件夹中。

    右键单击"SilverlightEmployeeContributionsGraph"节点,单击"属性",然后单击"生成"。

  2. 将"输出路径"更改为 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin。

  3. 通过以下方法生成解决方案:右键单击"SilverlightEmployeeContributionsGraph"节点,然后单击"生成"。.xap 文件已复制到所需目录,您可以将 Silverlight Web 部件添加到 SharePoint 网站。

  4. 打开 Internet Explorer,并导航到之前指定的网站。

  5. 接下来,更新在 创建 Silverlight 应用程序以访问 SharePoint 2010 数据 时添加的 Silverlight Web 部件,以指向在本主题创建的 Silverlight 图表控件。单击 Silverlight Web 部件右上角的下拉列表,然后单击"编辑 Web 部件"。

  6. 单击屏幕最右边的"配置",然后在 Silverlight Web 部件对话框屏幕中键入 /_layouts/ClientBin/SilverlightEmployeeContributionsGraph.xap。

  7. 单击"确定",然后在 Silverlight Web 部件提要栏底部单击"确定"。

    结果应类似于图 1。

    图 1. Silverlight 图表 Web 部件

    Silverlight 图表 Web 部件

后续步骤