Wednesday, May 4, 2011

How to Create List and Add Item to that List using SharePoint 2010 client object model?

In this post we will discuss how to create a list and also you can add item to that particular list using SharePoint 2010 client object model.

Also you can check out:

- How to Create a Content Type using Visual Studio 2010 in SharePoint 2010?

- How to create event receivers using Visual Studio 2010 in SharePoint 2010?

- How to hide ribbon in infopath 2010 in SharePoint 2010?

Here is a sample code to create a list and adding an item to that particular list using SharePoint 2010 object model. I have created a windows application and give reference to Microsoft.SharePoint.dll, Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll. On a button click I am doing the same.

Here is the code sample:
private void btnCreateList_Click(object sender, EventArgs e)
        {
            using (ClientContext context = new ClientContext("http://bsahoo3:8787/sites/Training"))
            {
           
            //Create a new list
            ListCreationInformation listCreationInformation = new ListCreationInformation();
            listCreationInformation.Title = "My Announcements List";
            listCreationInformation.Description += "Here is my list created by client object midel";
            listCreationInformation.TemplateType = (int)ListTemplateType.Announcements;
            listCreationInformation.QuickLaunchOption = Microsoft.SharePoint.Client.QuickLaunchOptions.On;
            List list = context.Web.Lists.Add(listCreationInformation);
            context.ExecuteQuery();

            //Create a new list item
            ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
            ListItem item = list.AddItem(listItemCreationInformation);
            item["Title"] = "First Announcement";
            item.Update();
            context.ExecuteQuery();
            }
        }

No comments:

Post a Comment