Export (0) Print
Expand All

How to: Create, Update, and Delete Lists

SharePoint 2010

Last modified: April 29, 2011

Applies to: SharePoint Foundation 2010

In this article
Creating and updating a list
Adding a field to a list
Deleting a list

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.

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();
        }
    }
}

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

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();
        }
    }
}

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.

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();
        }
    }
}

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.

Show:
© 2014 Microsoft