Saturday, June 23, 2012

Add Retrieve SharePoint list items using Object Model

In this article we will discuss how to add and retrieve list items using SharePoint 2010 object model. You can also check my last article on Client Object Model SharePoint 2010Also check how to Create List and Add Item to that List using SharePoint 2010 client object model?

Before adding an item to a SharePoint List, you can follow an article on how to create SharePoint 2010 list.  But if you want to create list using SharePoint Designer then you can visit this article
Once you create the list here is the object model code to add an item to list:

Code to add an item to list:
 try
            {
                SPSite myTopSite = SPContext.Current.Site;
                {
                    SPWeb myTopWeb = SPContext.Current.Site.RootWeb;
                    {
                        SPList listMyCustomList = myTopWeb.Lists.TryGetList("MyCustomList");

// TryGetList() is an useful method introduced in SharePoint 2010.

                        if (listMyCustomList != null)
                        {                        
                            //Add item to list
                            myTopWeb.AllowUnsafeUpdates = true;
                            SPListItem newItem = listMyCustomList.Items.Add();
                            newItem["Title"] = "My Title";
                            newItem["Description"] = "My Short Description";
                            newItem.Update();
                            myTopWeb.AllowUnsafeUpdates = false;                        
                        }
                    }
                }
            }
            catch(Exception ex)
            {
             
            }  

Here the list name is MyCustomList which has 2 columns as: Title and Description

In the next section we will discuss how to retrieve list items using SharePoint 2010 object model.

 SPWeb myTopWeb = SPContext.Current.Site.RootWeb;
                {
                    SPList listMenu = myTopWeb.Lists["MyCustomList"];

                    SPQuery objquery = new SPQuery();

                    objquery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'> Fewlines4Biju</Value></Eq></Where>";

                    SPListItemCollection items = listMenu.GetItems(objquery);

                    foreach (SPListItem item in items)
                        if (item != null)
                        {                          
                            if (item["Title"] != null)
                            {
                                if (!string.IsNullOrEmpty(item["Title"].ToString()))
                                {
                                    string myTitle = item["Title"].ToString();
                                }
                            }
                        }
                }

Here we have used CAML query with SPQuery class to retrieve items from SharePoint List. Here it will retrieve all list items whose Title is "Fewlines4Biju".