リストを作成、更新、または削除する
最終更新日: 2011年4月29日
適用対象: SharePoint Foundation 2010
この記事の内容
リストの作成および更新
リストへのフィールドの追加
リストの削除
クライアント オブジェクト モデルを使用して行うリストの作成、更新、および削除は、サーバー オブジェクト モデルを使用してこれらの操作を行う場合と同じように動作します。ただし、クライアントの処理は、ExecuteQuery() メソッドまたは ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) メソッド (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を呼び出すまで完了しません。
リストの作成および更新
リスト オブジェクトを作成するには、ListCreationInformation クラス (JavaScript: ListCreationInformation) を使用してそのプロパティを定義し、そのオブジェクトを ListCollection クラス (JavaScript: ListCollection) の Add(ListCreationInformation) メソッド (JavaScript: add(parameters)) に渡します。次の例では、新しいお知らせリストを作成しています。
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class CreateList
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "My Announcements List";
listCreationInfo.TemplateType = (int)ListTemplateType.Announcements;
List oList = oWebsite.Lists.Add(listCreationInfo);
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class CreateList
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim clientContext As New ClientContext(siteUrl)
Dim oWebsite As Web = clientContext.Web
Dim listCreationInfo As New ListCreationInformation()
listCreationInfo.Title = "My Announcements List"
listCreationInfo.TemplateType = CInt(ListTemplateType.Announcements)
Dim oList As List = oWebsite.Lists.Add(listCreationInfo)
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';
function createList() {
var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('My Announcements List');
listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
this.oList = oWebsite.get_lists().add(listCreationInfo);
clientContext.load(oList);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
var result = oList.get_title() + ' created.';
alert(result);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
リストを追加した後でリストを更新する必要がある場合は、前の例に変更を加えた次の例に示すように、リスト プロパティを設定し、Update() メソッド (JavaScript: update()) を呼び出してから、ExecuteQuery() または ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を呼び出します。
.
.
.
.
List oList = oWebsite.Lists.Add(listCreationInfo);
oList.Description = "New Announcements List";
oList.Update();
clientContext.ExecuteQuery();
.
.
.
.
Dim oList As List = oWebsite.Lists.Add(listCreationInfo)
oList.Description = "New Announcements List"
oList.Update()
clientContext.ExecuteQuery()
.
.
.
.
this.oList = oWebsite.get_lists().add(listCreationInfo);
oList.set_description('New Announcements List');
oList.update();
clientContext.load(oList);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
リストへのフィールドの追加
リストのフィールド コレクションにフィールドを追加するには、FieldCollection クラス (JavaScript: FieldCollection) の Add(Field) メソッド (JavaScript: add(field)) または AddFieldAsXml(String, Boolean, AddFieldOptions) メソッド (JavaScript: addFieldAsXml(schemaXml, addToDefaultView, options)) を使用します。次の例では、フィールドを作成し、更新してから、ExecuteQuery() (JavaScript: executeQueryAsync(succeededCallback, failedCallback)) を呼び出しています。
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class Program
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("Announcements");
SP.Field oField = oList.Fields.AddFieldAsXml("<Field DisplayName='MyField' Type='Number' />",
true, AddFieldOptions.DefaultValue);
SP.FieldNumber fieldNumber = clientContext.CastTo<FieldNumber>(oField);
fieldNumber.MaximumValue = 100;
fieldNumber.MinimumValue = 35;
fieldNumber.Update();
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class AddFieldToList
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim clientContext As New ClientContext(siteUrl)
Dim oList As SP.List = clientContext.Web.Lists.GetByTitle("My Announcements List")
Dim oField As SP.Field = oList.Fields.AddFieldAsXml( _
"<Field DisplayName='MyField' Type='Number' />", _
True, AddFieldOptions.DefaultValue)
Dim fieldNumber As SP.FieldNumber = clientContext.CastTo(Of FieldNumber)(oField)
fieldNumber.MaximumValue = 100
fieldNumber.MinimumValue = 35
fieldNumber.Update()
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';
function addFieldToList() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
this.oField = oList.get_fields().addFieldAsXml('<Field DisplayName=\'MyField\' Type=\'Number\' />', true, SP.AddFieldOptions.defaultValue);
var fieldNumber = clientContext.castTo(oField,SP.FieldNumber);
fieldNumber.set_maximumValue(100);
fieldNumber.set_minimumValue(35);
fieldNumber.update();
clientContext.load(oField);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
var result = oField.get_title() + ' added.';
alert(result);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
前の例では、クライアント コンテキスト オブジェクトの CastTo<T>(ClientObject) メソッド (JavaScript: castTo(obj, type)) を使用して、フィールドを FieldNumber 型 (JavaScript: FieldNumber) としてキャストしています。この処理は、クエリを実行する前に行う必要があります。クエリの実行前に行わないと、クライアント オブジェクト モデルは、返されたオブジェクト oField の本当の型がわからず、既定で Field (JavaScript: Field) を型として使用します。
リストの削除
リストを削除するには、リスト オブジェクトの DeleteObject() メソッド (JavaScript: deleteObject()) を呼び出します。以下に例を示します。
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class DeleteList
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
List oList = oWebsite.Lists.GetByTitle("My Announcements List");
oList.DeleteObject();
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class DeleteList
Shared Sub Main()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim clientContext As New ClientContext(siteUrl)
Dim oWebsite As Web = clientContext.Web
Dim oList As List = oWebsite.Lists.GetByTitle("My Announcements List")
oList.DeleteObject()
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';
function deleteList() {
var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
this.listTitle = 'My Announcements List';
this.oList = oWebsite.get_lists().getByTitle(listTitle);
oList.deleteObject();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
var result = listTitle + ' deleted.';
alert(result);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Microsoft SharePoint Foundation 2010 Silverlight オブジェクト モデルのコンテキスト内でのリストおよびその他のクライアント オブジェクトの操作に関する詳細および例については、「Silverlight オブジェクト モデルを使用する」を参照してください。