Search This Blog

Tuesday, December 9, 2014

Server object model CRUD


   Item insert, update,delete and field update.

Insert item

Spsite site    =  spcontext.current.site;
SPWeb web = site.openweb();
            SPList list = web.Lists["Employee"];
            SPListItem listItem = list.Items.Add();
            listItem["Title"] = "Betty";
            listItem["Birthday"] = "1/1/1990";
            listItem["Male"] = 1;
            listItem["Position"] = "Tester";
            listItem.Update();   


    Update item

syntax:1

SPSite site = SPContext.Current.Site;
                    SPWeb web = site.OpenWeb();
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["BritCustomers"];
                    SPListItem item = list.GetItemById(Convert.ToInt32(TextBox4.Text));
                    item["First Name"] = TextBox1.Text;
                    item["Last Name"] = TextBox2.Text;
                    item["Age"] = Convert.ToInt32(TextBox3.Text);
                    item.Update();                                      //         item.Delete(); (if u want to teh delete the item use  this )
                    Label5.Text = "Data Successfully Updated";
                    TextBox1.Text = string.Empty;
                    TextBox2.Text = string.Empty;
                    TextBox3.Text = string.Empty;

                    TextBox4.Text = string.Empty;



syntax:2

SPsite site    =  SPcontext.current.site;
SPWeb web = site.openweb();
 SPList list = web.Lists["Employee"];

SPListItem Item = list.GetItemById(ItemID);

SPListItem itemToAdd = list.Items.Add();

            string listItemId= listitem.ID;

SPListItem item= list.GetItemById(listItemId);

            item["Birthday"] = "Changed Description";
            item.Update();


  Delete item

SPsite site    =  SPcontext.current.site;
SPWeb web = site.openweb();
 SPList list = web.Lists["Employee"];

            string listItemId= listitem.ID;

SPListItem itemToDelete = list.GetItemById(listItemId);
            itemToDelete.Delete();

examlpe2:

Edit an item
             for (int i = 0; i < itemCol.Count; i++)
            {
                SPListItem item = itemCol[i];
                if(item["ID"].ToString() == "2"// Find an item by ID then updating it
                {
                    item["Title"] = "Linda";
                    item["Birthday"] = "1/2/1990";
                    item["Male"] = 0; //the Yes/No field type, 0 refers to female and 1 refers to male
                    item["Position"] = "Tester";
                    item.Update();
                    break;
                }                
            }

Delete an item
          list.Items.DeleteItemById(7); 


SPField Update:

SPSite site = SPContext.Current.Site;
                    SPWeb web = site.OpenWeb();
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["BritCustomers"];

                    SPField field = list.Fields["Age"];
                    field.Title= "britcustomerage";

                    field.Update();
-

1 comment:

  1. Hi Ram,

    Its really very nice articles and helpful to me. Thank you very Much for posting :)

    ReplyDelete