How to Select, Insert, Update and Delete SharePoint 2010 List items
This time i will show you different way to play with sharepoint 2010 list items. In this tutorial i will use a helper called cawl (Cawl4SharePoint). With cawl it is really easy to add, modify or delete list item.
Before begin first download cawl4sharepoint. In your visual web part project add this dll file as a referans. Dont forget to add this library in your wsp package.
Lets start with building a query.
Create Caml Query
cawl_QueryBuilder cawl = new cawl_QueryBuilder; cawl.Where("FirstName","=","Mike"); cawl.Where("Status","!=","Passive"); cawl.Get("Users"); Gridview1.Datasource= cawl.ListItemCollection(); Gridview1.Databind();
What we have created so far is that we select all items in the “Users” list where first name is equal to Mike and status is not equal to Passive.
There are a few function in cawl using these function querying will be very easy. Detailed information can be found at www.cawl4sharepoint.com
cawl_QueryBuilder cawl = new cawl_QueryBuilder; cawl.Set("Title", TextBox2.Text); cawl.Set("Stock", Convert.ToInt32(TextBox3.Text)); cawl.Set("Return Date",Convert.ToDateTime(TextBox4.Text)); cawl.Set("Employee", TextBox5.Text); cawl.Insert("Orders");
Here we add an item to “Orders” list where Title, Stock, Return Date and Employee is specified above.
cawl_QueryBuilder cawl = new cawl_QueryBuilder; cawl.Set("Status", "Passive"); cawl.Where("JoinDate";"<";"12.12.2010"); cawl.Update('Users');
Here we update all items status in the Users list as Passive where join date less then 12.12.2010.
cawl_QueryBuilder cawl = new cawl_QueryBuilder; cawl.Where("Status", "=", "inactive"); cawl.Delete("Users");
Here we delete all items in Users list where the status is inactive.
Best part of this helper is that you don’t need to use other tools to create caml query and use it inside the visual studio. Instead with cawl use it directly in your visual studio project.
You may also use standard way to do these actions but cawl help you save time and with simple syntax, your code look good and will be more understandable.