Compartilhar via


How to: Create, Update, and Delete Lists

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

Creating, updating, and deleting lists through the client object model works similarly to how you perform these tasks using the server object model, although client operations do not complete until you call the ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) method.

Creating and updating a list

To create a list object, use the ListCreationInformation class to define its properties, and then pass this object to the Add(ListCreationInformation) method of the ListCollection class. The following example creates a new announcements list.

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

If you need to update the list after the list has been added, you can set list properties and call the Update() method before calling ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler), as shown in the following modifications of the previous example.

            .
            .
            .
            .
            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()

Adding a field to a list

Use the Add(Field) method or AddFieldAsXml(String, Boolean, AddFieldOptions) method of the FieldCollection class to add a field to the field collection of a list. The following example creates a field and then updates it before calling ExecuteQuery().

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

The previous example uses the CastTo<T>(ClientObject) method of the client context object to cast the field as a FieldNumber type, which must be done before executing the query. Otherwise, the client object model does not know the real type of the returned object oField and, by default, uses Field as the type.

Deleting a list

To delete a list, call the DeleteObject() method of the list object, as shown in the following example.

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

For information and examples about working with lists and other client objects within the context of the Microsoft SharePoint Foundation 2010 Silverlight object model, see Using the Silverlight Object Model.

See Also

Concepts

SharePoint Client Object Creation

SharePoint 2010 Client Object Model Guidelines

Data Retrieval Overview

How to: Retrieve Lists

Common Programming Tasks in the Managed Client Object Model

Other Resources

Client Class Library