SharePoint

How to use CAML Query on List in SharePoint 2010

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 = new SPQuery();
            query.Query = string.Format(@"<Where>
                                                <Eq>
                                                     <FieldRef Name='EmployeeName'/>
                                                     <Value Type='Text'>{0}</Value>
                                                </Eq>
                                          </Where>", currentUser);

            SPListItemCollection listItems = leaveStorage.GetItems(query);

Standard

One thought on “How to use CAML Query on List in SharePoint 2010

Leave a Reply

Your email address will not be published. Required fields are marked *