Silverlight オブジェクト モデルを使用する

最終更新日: 2010年7月1日

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

Microsoft SharePoint Foundation 2010 は、2 つのコンテキストで、Silverlight クライアント オブジェクト モデルの実装をサポートします。それは、Silverlight Web パーツ内の実装と Silverlight クロスドメイン データ アクセス内の実装です。これらの実装により、Silverlight アプリケーションは Microsoft SharePoint Foundation 2010 データと対話します。3 つ目のオプションとして、サーバーでクライアント アクセスのドメイン間ポリシーを変更することが考えられますが、これはセキュリティ リスクがあるため、SharePoint Foundation 2010 ではサポートされません。

SharePoint Foundation Silverlight オブジェクト モデルを使用する場合、クエリは非同期で実行されるため、コールバック メソッドのデリゲートを ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) メソッドのパラメーターとして渡す必要があります。これは、JavaScript オブジェクト モデル内でのクエリの実行に似ています。ただし、Silverlight オブジェクト モデルを経由してユーザー インターフェイスを変更するコードを実行するには、次の例のように、BeginInvoke() を呼び出して UI を作成したスレッドの Dispatcher オブジェクトに、この作業を委任する必要があります。

Silverlight Web パーツ経由でコードを展開する

Silverlight Web パーツ内で SharePoint Foundation Silverlight オブジェクト モデルを使用するには、Silverlight アプリケーションを Visual Studio 内に作成し、コードを、プロジェクトの既定の Page.xaml.cs ファイル内の Page クラスに追加できます。プロジェクトを作成した後に、プロジェクトのアプリケーション パッケージ (.xap) ファイルをドキュメント ライブラリにアップロードします。Silverlight Web パーツを Web パーツ ページに挿入し、Web パーツの URL ソースがドキュメント ライブラリ内の .xap ファイルを指すようにします。

次の例は、Button コントロールと TextBlock コントロールがプロジェクトの MainPage.xml ファイルに定義されていることを前提とします。

リスト データを取得する

次の例は、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

前述の例では、ClientContext(String) コンストラクターを使用して URL を指定する代わりに、Current プロパティを使用して現在の要求コンテキストを指定しています。たとえば, .xap ファイルをサイト コレクションのルート Web サイトの Shared Documents にアップロードし、子 Web サイトのルート Web サイトの下の任意の場所に Web パーツ ページを作成して、Silverlight Web パーツを, .xap ファイルを指す (したがってそのロジックを実装する) このページに追加できます。つまり、コードはルート Web サイトに存在しますが、そのロジックはサイト コレクション内のあらゆる Web サイトで使用できます。

リスト アイテムを作成する

次の例は、特定のリストにリスト アイテムを作成する方法を示しています。この例では、デリゲートを使用して、新しいアイテムに関する情報を 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 List Viewer

データ取得の概要

クライアント オブジェクト モデルのガイドライン

[方法] リストを取得する

[方法] リスト アイテムの作成、更新、削除を行う

一般的なプログラミング作業

その他の技術情報

クライアント クラス ライブラリ

ECMAScript クラス ライブラリ