Tuesday, 19 November 2013

programmatically Insert,update,delete in sharepoint list using c#

Share it Please
Add New Item :-
                   public void AddNewItem()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList listEmpInsert = web.Lists["Employee"];
                        web.AllowUnsafeUpdates = true;
                        SPListItem EmpInsert = listEmpInsert.Items.Add();
                        EmpInsert["EmpName"] = "Mohamed";
                        EmpInsert["Age"] = "28";
                        EmpInsert["Address"] = "Chennnai";
                        EmpInsert.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }  
Update the Item:-
                        public void updateExistingItem()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList lstupdate = web.Lists["Employee"];
                        web.AllowUnsafeUpdates = true;
                        int listItemId = 1;
                        SPListItem itemToUpdate = lstupdate.GetItemById(listItemId);
                        itemToUpdate["EmpName"] = "Mohamed sithik";
                        itemToUpdate["Age"] = "30";
                        itemToUpdate["Address"] = "Bangalore";
                        itemToUpdate.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
Delete the Item
                           public void DelteItem() 
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList lstdelete = web.Lists["Employee"]; 
                        web.AllowUnsafeUpdates = true;
                        int listItemId = 1;
                        SPListItem itemToDelete = lstdelete.GetItemById(listItemId);
                        itemToDelete.Delete();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
Get all the Item:-
              public void ReteriveallItem()  
        {
            SPSite mySite = new SPSite(SPContext.Current.Site.ID);
            SPWeb myWeb = mySite.OpenWeb();
            SPList myList = myWeb.Lists["Employee"];
            DataTable dt = ConvertSPListToDataTable(myList); 
           
        }
        private static DataTable ConvertSPListToDataTable(SPList oList)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = oList.Items.GetDataTable();
                foreach (DataColumn c in dt.Columns)
                    c.ColumnName = System.Xml.XmlConvert.DecodeName(c.ColumnName);
                return (dt);
            }
            catch
            {
                return (dt);
            }
        }

No comments:

Post a Comment

Followers

Follow The Author