How to use CAML Query on List in SharePoint 2010


Shahjada Talukdar MasooM

The Query schema of Collaborative Application Markup Language (CAML) is used in various ways within the context of Microsoft SharePoint Foundation to define queries against list data. For example, you can assign CAML query strings to the Query property of several classes in the server and client object models in order to return data.
In the below example , I am going to show you how to filter from SP List like SQL Server Select Query from a table . The below code is equivalent to SQL Query like ::: SELECT * FROM EmployeeTable WHERE EmployeeName=@employeeName

SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
            SPList leaveStorage = web.Lists["LeaveStorage"];
               
            SPQuery query = newSPQuery();
            query.Query = string.Format(@"<Where>
                                                <Eq>
                                                     <FieldRef Name='EmployeeName'/>
                                                     <Value Type='Text'>{0}</Value>
                                                </Eq>
                                          </Where>", currentUser);

            SPListItemCollection listItems = leaveStorage.GetItems(query);

View original post