クエリ内のメソッド間で値オブジェクトを使用できない
最終更新日: 2011年5月27日
適用対象: SharePoint Foundation 2010
メソッドまたはプロパティを使用して値を返す場合、または ClientValueObject クラス (JavaScript: ClientValueObject) から継承するオブジェクトを使用する場合は、同じクエリの別のメソッド呼び出しに対して値をパラメーターとして使用できません。値オブジェクトの詳細については、「クライアント オブジェクト、値オブジェクト、およびスカラー プロパティ」を参照してください。
たとえば、Web サイトのタイトルと同じ表示名の SharePoint リストを作成するとします。SharePoint Foundation サーバー オブジェクト モデルに詳しい場合は、次のコードを記述したくなると思います。
正しくないコード
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class CreateList
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
Web oWebsite = clientContext.Web;
ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.TemplateType = 104;
listCreationInfo.Title = oWebsite.Title;
List oList = oWebsite.Lists.Add(listCreationInfo);
clientContext.ExecuteQuery();
}
}
}
コードをコンパイルすると、"プロパティまたはフィールドが初期化されていない" ため、PropertyOrFieldNotInitializedException が返されます。この例では、ExecuteQuery() を呼び出す前は Web サイト オブジェクトの Title プロパティ (JavaScript: title) を利用できないため例外を返しています。SQL を使用すると、Web サイト タイトルを持つローカル変数を宣言し、リスト作成でこのローカル変数を使用できますが、クライアント オブジェクト モデルではローカル変数を宣言できません。したがって、次の変更された例で示すように、ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を 2 回呼び出して、最初に Title プロパティ (JavaScript: title) の値を返し、その後でリストを作成する必要があります。
正しいコード
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class CreateList
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite,
w => w.Title);
clientContext.ExecuteQuery();
ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.TemplateType = 104;
listCreationInfo.Title = oWebsite.Title;
List oList = oWebsite.Lists.Add(listCreationInfo);
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class CreateList
Shared Sub Main()
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
Dim oWebsite As Web = clientContext.Web
clientContext.Load(oWebsite, Function(w) w.Title)
clientContext.ExecuteQuery()
Dim listCreationInfo As New ListCreationInformation()
listCreationInfo.TemplateType = 104
listCreationInfo.Title = oWebsite.Title
Dim oList As List = oWebsite.Lists.Add(listCreationInfo)
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
function getWebSiteTitle() {
this.clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
this.oWebsite = clientContext.get_web();
clientContext.load(oWebsite, 'Title');
clientContext.executeQueryAsync(Function.createDelegate(this, this.createList), Function.createDelegate(this, this.onQueryFailed));
}
function createList() {
var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_templateType(104);
listCreationInfo.set_title(oWebsite.get_title());
var oList = oWebsite.get_lists().add(listCreationInfo);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('List created.');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
関連項目
概念
値プロパティを使用する前に Load と ExecuteQuery を呼び出す