Learn how to get a collection of list items using a CAML query. The list items are displayed in a SharePoint style table inside the web part. This example also shows how to get the default Edit Form URL from the list and provide a link to the Edit Form from each list item Title.
Prerequisites
This example requires a SharePoint list named 'City' to exist in the current site. The 'City' list must have two fields: Title and State. In this example the State field is a single line text value limited to 2 characters.

Syntax
| Get List Items Using a CAML Query | Copy Code |
|---|---|
|
## Get the city list #set( $list = $web.lists.get_item("City") ) ## Build a CAML query #set( $caml = '<Where><Eq><FieldRef Name="State" /><Value Type="Text">IA</Value></Eq></Where>' ) #set( $caml = "$!{caml}<OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy>" ) #set( $query = $SPUtility.CreateSPQuery($caml) ) ## Get the list items #set( $listItems = $list.GetItems($query) ) ## Get edit url information #set( $sourceUrlEncoded = $HttpUtility.UrlEncode($request.RawUrl) ) #set( $editForm = $list.Forms.get_item($PAGETYPE_PAGE_EDITFORM) ) #foreach($item in $listItems) #beforeall <table class="ms-listviewtable" cellpadding="3" cellspacing="0" border="0" width="100%"> <tr class="ms-viewheadertr"> <th class="ms-vh2-nofilter">Title</th> <th class="ms-vh2-nofilter">State</th> <th class="ms-vh2-nofilter">Created</th> <th class="ms-vh2-nofilter">Modified</th> </tr> #odd <tr class=""> #even <tr class="ms-alternating"> #each <td class="ms-vb2"><a href="$web.Url/$editForm.Url?ID=$item.ID&Source=$sourceUrlEncoded">$item.Title</a></td> <td class="ms-vb2">$item.get_item("State")</td> <td class="ms-vb2">$item.get_item("Created")</td> <td class="ms-vb2">$item.get_item("Modified")</td> #after </tr> #afterall </table> #nodata No list items found #end | |
Output
