While using REST in SharePoint, the most important thing is to build the URI. The format of URI is discussed in my last post. Now lets work with the URI format to return different results from list.
Lets create a Contacts list with one more additional column DOB of type Date. Add few items in it, the list will look like below.
Lets create a Contacts list with one more additional column DOB of type Date. Add few items in it, the list will look like below.
- Get all the items from list
jQuery(function($) {
ReadAllItems();
});
function ReadAllItems()
{
jQuery.ajax({
url:_spPageContextInfo.siteAbsoluteUrl+"/_api/web/lists/getByTitle('Contacts')/items",
type:"GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
DisplayItems(data);
},
error: function (err) {
alert(JSON.stringify(err));
}
});
}
function DisplayItems(data)
{
var results = data.d.results;
var html="<table width='70%'><tr><td>ID</td><td>FirstName</td><td>LastName(Title)</td><td>Company</td><td>DOB</td>"
for(var i=0; i<results.length; i++) {
html+="<tr>"
html+="<td>"+results[i].ID+ "</td>";
html+="<td>"+results[i].FirstName+ "</td>";
html+="<td>"+results[i].Title+ "</td>";
html+="<td>"+results[i].Company+ "</td>";
html+="<td>"+results[i].DOB+ "</td>";
html+="</tr>"
}
html+="</table>";
$('#divMain').html(html);
}
The result of above code will look like this
- Get All Items with selected columns
Replace the url paramter above with the code below
url:_spPageContextInfo.siteAbsoluteUrl+"/_api/web/lists/getByTitle('Contacts')/items"
+"?$select=Id,FirstName",
- Order by FirstName ASC
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$orderby=FirstName",
- Order by FirstName DESC
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$orderby=FirstName desc",
- Get The Top rows
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$top=2",
- Filter by Column
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=FirstName eq 'Akash'",
- Startswith
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=startswith(FirstName,'Ak')",
- Substringof
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=substringof('k',FirstName)",
- AND Condition
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=startswith(FirstName,'A') and Title eq 'Karda'",
- OR Condition
url:_spPageContextInfo.siteAbsoluteUrl+"/_api/web/lists/getByTitle('Contacts')/items"
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=startswith(FirstName,'N') or Title eq 'Karda'",
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=startswith(FirstName,'N') or Title eq 'Karda'",
- Date Time Column Filter
url:_spPageContextInfo.siteAbsoluteUrl+"/_api/web/lists/getByTitle('Contacts')/items"
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=DOB eq '2012-05-17T07:00:00Z'",
+"?$select=Id,FirstName,Title,Company,DOB"
+"&$filter=DOB eq '2012-05-17T07:00:00Z'",
No comments:
Post a Comment