使用 Silverlight 对象模型

上次修改时间: 2010年7月1日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

Microsoft SharePoint Foundation 2010 支持在两类上下文中实现 Silverlight 客户端对象模型:Silverlight Web 部件和 Silverlight 跨域数据访问系统(Silverlight 应用程序可通过此系统与 Microsoft SharePoint Foundation 2010 数据进行交互)。第三种可能的做法是修改服务器上的客户端访问跨域策略,但这将导致安全风险,因此 SharePoint Foundation 2010 中不支持此做法。

由于在使用 SharePoint Foundation Silverlight 对象模型时查询执行是异步的,因此您必须将回调方法的委托作为 ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) 方法中的参数传递,这与 JavaScript 对象模型中的查询执行类似。但是,若要通过 Silverlight 对象模型运行在用户界面 (UI) 中进行更改的代码,则必须通过调用 BeginInvoke() 将此工作委派给创建 UI 的线程的 Dispatcher 对象,如下面的示例所示。

通过 Silverlight Web 部件部署代码

若要在 Silverlight Web 部件中使用 SharePoint Foundation Silverlight 对象模型,您可以在 Visual Studio 中创建 Silverlight 应用程序,然后将您的代码添加到项目的默认 Page.xaml.cs 文件中的 Page 类。在构建项目后,将项目的应用程序包 (.xap) 文件上载到文档库中。将 Silverlight Web 部件插入 Web 部件页,并将 Web 部件的 URL 源指向文档库中的 .xap 文件。

下面的示例假定了项目的 MainPage.xml 文件中 Button 控件和 TextBlock 控件的定义。

检索列表数据

下面的示例演示如何在 Silverlight 应用程序的上下文中检索 SharePoint Foundation 数据。此示例使用针对程序执行成功或失败的事件处理程序。onQuerySucceeded 方法创建了一个委托(即 UpdateUIMethod)以在 UI 中显示返回的列表数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
 
namespace Microsoft.SDK.SharePointServices.Samples
{
    public partial class MainPage : UserControl
    {
        Web oWebsite;
        ListCollection collList;
        IEnumerable<List> listInfo;
 
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ClientContext clientContext = ClientContext.Current;
            oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;
 
             clientContext.Load(oWebsite,
                website=>website.Title);
 
             listInfo = clientContext.LoadQuery(
                collList.Include(
                    list=>list.Title,
                    list=>list.Fields.Include(
                        field=>field.Title).Where(
                        field=>field.Required  == true
                        && field.Hidden != true)));
 
            clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
        }
 
        private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
        {
            UpdateUIMethod updateUI = DisplayInfo;
            this.Dispatcher.BeginInvoke(updateUI);
        }
 
        private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
        {
            MessageBox.Show = "Request failed. " + args.Message + "\n" + args.StackTrace;
        }
 
        private void DisplayInfo()
        {
            MyOutput.Text = "Title: " + oWebsite.Title;
            collList = oWebsite.Lists;
 
            foreach (List oList in listInfo)
            {
                MyOutput.Text += "\n\tList: " + oList.Title;
 
                FieldCollection collField = oList.Fields;
                foreach (Field oField in collField)
                {
                    MyOutput.Text += "\n\t\tField: " + oField.Title;
                }
            }
        }
 
        private delegate void UpdateUIMethod();
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Partial Public Class MainPage
        Inherits UserControl
        Private oWebsite As Web
        Private collList As ListCollection
        Private listInfo As IEnumerable(Of List)

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim clientContext As ClientContext = ClientContext.Current
            oWebsite = clientContext.Web
            Dim collList As ListCollection = oWebsite.Lists

            clientContext.Load(oWebsite, Function(website) website.Title)

            listInfo = clientContext.LoadQuery(collList.Include(Function(list) list.Title, Function(list) list.Fields.Include(Function(field) field.Title).Where(Function(field) field.Required = True AndAlso field.Hidden <> True)))

            clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
        End Sub

        Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
            Dim updateUI As UpdateUIMethod = AddressOf DisplayInfo
            Me.Dispatcher.BeginInvoke(updateUI)
        End Sub

        Private Sub onQueryFailed(ByVal sender As Object, ByVal args As ClientRequestFailedEventArgs)
            MessageBox.Show = "Request failed. " & args.Message & vbLf & args.StackTrace
        End Sub

        Private Sub DisplayInfo()
            MyOutput.Text = "Title: " & oWebsite.Title
            collList = oWebsite.Lists

            For Each oList As List In listInfo
                MyOutput.Text += vbLf & vbTab & "List: " & oList.Title

                Dim collField As FieldCollection = oList.Fields
                For Each oField As Field In collField
                    MyOutput.Text += vbLf & vbTab & vbTab & "Field: " & oField.Title
                Next oField
            Next oList
        End Sub

        Private Delegate Sub UpdateUIMethod()
    End Class
End Namespace

上一示例使用 Current 属性指定当前请求上下文,而不是使用 ClientContext(String) 构造函数并指定 URL。例如,可以将 .xap 文件上载到网站集的根网站中的"共享文档"中,并在子网站中的根网站下的任意位置创建一个 Web 部件页,然后向此页添加 Silverlight Web 部件以指向 .xap 文件并实现其逻辑。换句话说,虽然您的代码可能位于根网站中,但其逻辑将对网站集的任何网站可用。

创建列表项

下面的示例演示如何在特定列表中创建列表项。此示例使用委托显示有关 UI 中新项的信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    public partial class MainPage : UserControl
    {
        private List oList;
        private string siteUrl = "http://MyServer/sites/MySiteCollection/MyWebSite";

        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ClientContext clientContext = new ClientContext(siteUrl);
            Web oWebsite = clientContext.Web;
            ListCollection collList = oWebsite.Lists;

            oList = clientContext.Web.Lists.GetByTitle("Announcements");

            ListItem oListItem = oList.AddItem(new ListItemCreationInformation());
            oListItem["Title"] = "My new item";
            oListItem["Body"] = "This is my new Silverlight item.";
            oListItem.Update();

            clientContext.Load(oList,
                list => list.Title);

            clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
        }

        private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
        {
            UpdateUIMethod updateUI = DisplayInfo;
            this.Dispatcher.BeginInvoke(updateUI);
        }

        private void DisplayInfo()
        {
            MyOutput.Text = "New item created in " + oList.Title;
        }

        private delegate void UpdateUIMethod(); 
        
        private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
        {
            MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Partial Public Class MainPage
        Inherits UserControl
        Private oList As List
        Private siteUrl As String = "http://MyServer/sites/MySiteCollection/MyWebSite"

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim clientContext As New ClientContext(siteUrl)
            Dim oWebsite As Web = clientContext.Web
            Dim collList As ListCollection = oWebsite.Lists

            oList = clientContext.Web.Lists.GetByTitle("Announcements")

            Dim oListItem As ListItem = oList.AddItem(New ListItemCreationInformation())
            oListItem("Title") = "My new item"
            oListItem("Body") = "This is my new Silverlight item."
            oListItem.Update()

            clientContext.Load(oList, Function(list) list.Title)

            clientContext.ExecuteQueryAsync(AddressOf onQuerySucceeded, AddressOf onQueryFailed)
        End Sub

        Private Sub onQuerySucceeded(ByVal sender As Object, ByVal args As ClientRequestSucceededEventArgs)
            Dim updateUI As UpdateUIMethod = AddressOf DisplayInfo
            Me.Dispatcher.BeginInvoke(updateUI)
        End Sub

        Private Sub DisplayInfo()
            MyOutput.Text = "New item created in " & oList.Title
        End Sub

        Private Delegate Sub UpdateUIMethod()

        Private Sub onQueryFailed(ByVal sender As Object, ByVal args As ClientRequestFailedEventArgs)
            MessageBox.Show("Request failed. " & args.Message & vbLf & args.StackTrace)
        End Sub
    End Class
End Namespace

请参阅

概念

代码示例:Silverlight 列表查看器

数据检索概述

客户端对象模型准则

如何:检索列表

如何:创建、更新和删除列表项

常见编程任务

其他资源

客户端类库

ECMAScript 类库