Showing posts with label Update list item. Show all posts
Showing posts with label Update list item. Show all posts

Saturday 30 July 2016

CRUD Operation in SharePoint list using Server Side Object Model (SSOM) and C#

Introduction 

Create list item, Update list item, Read data from list, Delete list item in SharePoint using C#

Code 
    1. .ascx page code with some basic fields  
    2.   
    3.   
    4.   
    5. <table>
    6.   
    7. <tr>  
    8.   
    9. <td> </td>  
    10.   
    11. <td>List CRUD</td>  
    12.   
    13. <td> </td>  
    14.   
    15. </tr>  
    16.   
    17. <tr>  
    18.   
    19. <td>Title</td>  
    20.   
    21. <td>  
    22.   
    23. <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>  
    24.   
    25. </td>  
    26.   
    27. <td> </td>  
    28.   
    29. </tr>  
    30.   
    31. <tr>  
    32.   
    33. <td>Name</td>  
    34.   
    35. <td>  
    36.   
    37. <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
    38.   
    39. </td>  
    40.   
    41. <td> </td>  
    42.   
    43. </tr>  
    44.   
    45. <tr>  
    46.   
    47. <td>Age</td>  
    48.   
    49. <td>  
    50.   
    51. <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>  
    52.   
    53. </td>  
    54.   
    55. <td> </td>  
    56.   
    57. </tr>  
    58.   
    59. <tr>  
    60.   
    61. <td>Update</td>  
    62.   
    63. <td>  
    64.   
    65. <asp:TextBox ID="txtUpdate" runat="server"></asp:TextBox>  
    66.   
    67. </td>  
    68.   
    69. <td> </td>  
    70.   
    71. </tr>  
    72.   
    73. <tr>  
    74.   
    75. <td> </td>  
    76.   
    77. <td>  
    78.   
    79. <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" Width="75px" />  
    80.   
    81. <asp:Button ID="btnCancel" runat="server" Text="Cancel" Width="75px" />  
    82.   
    83. <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" Width="75px" />  
    84.   
    85. <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" Width="75px" />  
    86.   
    87. <asp:Button ID="btnRetrieve" runat="server" OnClick="btnRetrieve_Click" Text="Retrieve" Width="75px" />  
    88.   
    89. </td>  
    90.   
    91. <td> </td>  
    92.   
    93. </tr>  
    94.   
    95. </table>  
    96.   
    97.   
    98. .ascx.cs Page code  
    99.   
    100.   
    101. protected void btnAdd_Click(object sender, EventArgs e)  
    102.   
    103. {  
    104.   
    105. //Create SPSite object  
    106.   
    107. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    108.   
    109. {  
    110.   
    111. //create SPWeb object  
    112.   
    113. using (SPWeb RootWeb = RootSite.OpenWeb())  
    114.   
    115. {  
    116.   
    117. //create splist object  
    118.   
    119. SPList spList = RootWeb.Lists["ListCRUD"]; //ListCrud is a sharepoint list name  
    120.   
    121. SPListItem spListItem = spList.AddItem(); //create splistitem object to add item  
    122.   
    123. spListItem["Title"] = txtTitle.Text;  
    124.   
    125. spListItem["Name"] = txtName.Text;  
    126.   
    127. spListItem["Age"] = Convert.ToInt32(txtAge.Text);  
    128.   
    129. spListItem.Update(); //item update  
    130.   
    131. }  
    132.   
    133. }  
    134.   
    135. }  
    136.   
    137.   
    138. protected void btnUpdate_Click(object sender, EventArgs e)  
    139.   
    140. {  
    141.   
    142. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    143.   
    144. {  
    145.   
    146. using (SPWeb RootWeb = RootSite.OpenWeb())  
    147.   
    148. {  
    149.   
    150. int id = Convert.ToInt32(txtUpdate.Text);  
    151.   
    152. SPList spList = RootWeb.Lists["ListCRUD"];  
    153.   
    154. SPListItem spListItem = spList.GetItemById(id);  
    155.   
    156. spListItem["Title"] = txtTitle.Text;  
    157.   
    158. spListItem["Name"] = txtName.Text;  
    159.   
    160. spListItem["Age"] = Convert.ToInt32(txtAge.Text);  
    161.   
    162. spListItem.Update();  
    163.   
    164. }  
    165.   
    166. }  
    167.   
    168. }  
    169.   
    170.   
    171. protected void btnRetrieve_Click(object sender, EventArgs e)  
    172.   
    173. {  
    174.   
    175. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    176.   
    177. {  
    178.   
    179. using (SPWeb RootWeb = RootSite.OpenWeb())  
    180.   
    181. {  
    182.   
    183. int id = Convert.ToInt32(txtUpdate.Text);  
    184.   
    185. SPList spList = RootWeb.Lists["ListCRUD"];  
    186.   
    187. SPQuery spQuery = new SPQuery();  
    188.   
    189. spQuery.Query = string.Concat("<Where>",  
    190.   
    191. "<Eq>",  
    192.   
    193. "<FieldRef Name='ID' />",  
    194.   
    195. "<Value Type='Counter'>" + id + "</Value>",  
    196.   
    197. "</Eq>",  
    198.   
    199. "</Where>");  
    200.   
    201. spQuery.ViewFieldsOnly = true;  
    202.   
    203. spQuery.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Name' /><FieldRef Name='Age' />";  
    204.   
    205. SPListItemCollection spListColl = spList.GetItems(spQuery);  
    206.   
    207. DataTable dt = new DataTable();  
    208.   
    209. dt = spListColl.GetDataTable();  
    210.   
    211.   
    212. txtTitle.Text = dt.Rows[0]["Title"].ToString();  
    213.   
    214. txtName.Text = dt.Rows[0]["Name"].ToString();  
    215.   
    216. txtAge.Text = dt.Rows[0]["Age"].ToString();  
    217.   
    218. }  
    219.   
    220. }  
    221.   
    222. }  
    223.   
    224.   
    225. protected void btnDelete_Click(object sender, EventArgs e)  
    226.   
    227. {  
    228.   
    229. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    230.   
    231. {  
    232.   
    233. using (SPWeb RootWeb = RootSite.OpenWeb())  
    234.   
    235. {  
    236.   
    237. int id = Convert.ToInt32(txtUpdate.Text);  
    238.   
    239. SPList spList = RootWeb.Lists["ListCRUD"];  
    240.   
    241. SPListItem spListItem = spList.GetItemById(id);  
    242.   
    243. spListItem.Delete();  
    244.   
    245. spList.Update();  
    246.   
    247. }  
    248.   
    249. }  
    250.   
    251. }