クライアント オブジェクトの作成
最終更新日: 2011年5月27日
適用対象: SharePoint Foundation 2010
クライアント オブジェクトの作成が可能な場合 (List オブジェクト (たとえばJavaScript: List) など)、それに対応するクライアント オブジェクト コレクション (ListCollection (JavaScript: ListCollection) など) には Add メソッドが含まれます。このメソッドは、新しいオブジェクトの作成に必要なすべての情報が含まれている ClientObjectCreationInformation オブジェクト (ListCreationInformation (JavaScript: ListCreationInformation) など) を受け入れます。
次の例では、WebCreationInformation (JavaScript: WebCreationInformation) と Add(WebCreationInformation) メソッド (JavaScript: add(parameters)) を使用して、指定された Web サイトの下に子 Web サイトを作成します。
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
WebCollection collWeb = clientContext.Web.Webs;
WebCreationInformation webCreationInfo = new WebCreationInformation();
webCreationInfo.Title = "My New Web Site";
webCreationInfo.Description = "Description of new Web site...";
webCreationInfo.Language = 1033;
webCreationInfo.Url = "MyNewWebSite";
webCreationInfo.UseSamePermissionsAsParentSite = true;
webCreationInfo.WebTemplate = "STS#0";
Web oNewWebsite = collWeb.Add(webCreationInfo);
clientContext.ExecuteQuery();
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite")
Dim collWeb As WebCollection = clientContext.Web.Webs
Dim webCreationInfo As New WebCreationInformation()
webCreationInfo.Title = "My New Web Site"
webCreationInfo.Description = "Description of new Web site..."
webCreationInfo.Language = 1033
webCreationInfo.Url = "MyNewWebSite"
webCreationInfo.UseSamePermissionsAsParentSite = True
webCreationInfo.WebTemplate = "STS#0"
Dim oNewWebsite As Web = collWeb.Add(webCreationInfo)
clientContext.ExecuteQuery()
function createWebsite() {
var clientContext = new SP.ClientContext('http://MyServer/sites/MySiteCollection/MyWebSite');
var collWeb = clientContext.get_web().get_webs();
var webCreationInfo = new SP.WebCreationInformation();
webCreationInfo.set_title('My New Web Site');
webCreationInfo.set_description('Description of new Web site...');
webCreationInfo.set_language(1033);
webCreationInfo.set_url('MyNewWebSite');
webCreationInfo.set_useSamePermissionsAsParentSite(true);
webCreationInfo.set_webTemplate('STS#0');
var oNewWebsite = collWeb.add(webCreationInfo);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert("Created Web site.");
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
前の例では、Add(WebCreationInformation) メソッド (JavaScript: add(parameters)) によって、Web サイトがクライアント オブジェクト コレクション内に直ちに作成されます。以降のコレクション データ取得を待機したり、ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) の呼び出しを待機したりすることはありません。
ClientObjectCreationInformation オブジェクトを使用して、多くのオブジェクトを作成できます。たとえば、ナビゲーション ノードを作成する場合も、ClientObjectCreationInformation オブジェクトを使用します。次の例では、NavigationNodeCreationInformation オブジェクト (JavaScript: NavigationNodeCreationInformation) によってナビゲーション ノードを定義し、そのオブジェクトを使用して、指定された Web サイトのサイド リンク バー領域内にリンクを追加します。この例では、別の Web サイト上のリストへのリンクが 2 番目の子ノードとしてトップ レベルの Lists ノードの下に追加されます。
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
NavigationNodeCollection collNavNode = clientContext.Web.Navigation.QuickLaunch;
clientContext.Load(collNavNode);
clientContext.ExecuteQuery();
NavigationNodeCollection collChildNavNode = collNavNode[1].Children;
clientContext.Load(collChildNavNode);
clientContext.ExecuteQuery();
NavigationNodeCreationInformation navNodeCreationInfo = new NavigationNodeCreationInformation();
navNodeCreationInfo.PreviousNode = collChildNavNode[0];
navNodeCreationInfo.Title = "My Navigation Node";
navNodeCreationInfo.Url = "http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx";
collChildNavNode.Add(navNodeCreationInfo);
clientContext.ExecuteQuery();
Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite")
Dim collNavNode As NavigationNodeCollection = clientContext.Web.Navigation.QuickLaunch
clientContext.Load(collNavNode)
clientContext.ExecuteQuery()
Dim collChildNavNode As NavigationNodeCollection = collNavNode(1).Children
clientContext.Load(collChildNavNode)
clientContext.ExecuteQuery()
Dim navNodeCreationInfo As New NavigationNodeCreationInformation()
navNodeCreationInfo.PreviousNode = collChildNavNode(0)
navNodeCreationInfo.Title = "My Navigation Node"
navNodeCreationInfo.Url = "http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx"
collChildNavNode.Add(navNodeCreationInfo)
clientContext.ExecuteQuery()
function getNavNodes() {
this.clientContext = new SP.ClientContext('/sites/MySiteCollection/MyWebSite');
this.collNavNode = clientContext.get_web().get_navigation().get_quickLaunch();
clientContext.load(collNavNode);
clientContext.executeQueryAsync(Function.createDelegate(this, this.getChildNavNodes), Function.createDelegate(this, this.onQueryFailed));
}
function getChildNavNodes() {
this.collChildNavNode = collNavNode.get_item(1).get_children();
clientContext.load(collChildNavNode);
clientContext.executeQueryAsync(Function.createDelegate(this, this.createNavNode), Function.createDelegate(this, this.onQueryFailed));
}
function createNavNode() {
var navNodeCreationInfo = new SP.NavigationNodeCreationInformation();
navNodeCreationInfo.set_previousNode(collChildNavNode.get_item(0));
navNodeCreationInfo.set_title('My Navigation ECMA Node');
navNodeCreationInfo.set_url('http://MyServer/sites/MySiteCollection/MyTargetListWebSite/Lists/MyTargetList/AllItems.aspx');
collChildNavNode.add(navNodeCreationInfo);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert("Created navigation node.");
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
前の例では、サイト コレクション内の別の Web サイトへのリンクが作成されます。サイト コレクションの外側の場所への外部リンクを作成するには、Add(NavigationNodeCreationInformation) メソッド (JavaScript: add(parameters)) を呼び出す前に、NavigationNodeCreationInformation オブジェクト (JavaScript: NavigationNodeCreationInformation) の IsExternal プロパティ (JavaScript: IsExternal) を true に設定します。
Silverlight アプリケーションのコンテキストでクライアント オブジェクトを作成する方法の詳細については、「Silverlight オブジェクト モデルを使用する」を参照してください。
関連項目
概念
クライアント オブジェクト、値オブジェクト、およびスカラー プロパティ
マネージ オブジェクト モデルと ECMAScript オブジェクト モデルの相違点
その他の技術情報
SharePoint Foundation 2010 のマネージ クライアント オブジェクト モデルの使用