Question

I am trying to execute a REST query on a Sharepoint 2013 list with Powershell, but I'm always getting back more results than I want. Even if I filter the query to only get back 1 list item, the query is always giving me back the first page (100 results) of the whole list.

When I execute the REST url in Postman, I am getting back the results I want. But somehow Powershell is returning more results than I would expect with the query filter.

Here is my sample code with Invoke-WebRequest:

$webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$webSession.Credentials = $SPCredentials
$webSession.Headers.Add("Accept", "application/json;odata=verbose")

# This query should give me back 1 item
$webRequest = Invoke-WebRequest -Uri "http://mysharepointsite.com/_api/web/lists/getByTitle('ListName')/items?`$filter=substringof('parameter', PropertyName)" -Method Get -WebSession $webSession

# This is needed to avoid an error.
$data = $webRequest.Content.ToString().Replace("""ID""", "_ID") | ConvertFrom-Json

Write-Host $data.d.results.Count

Output: 100

When I try the same query with Invoke-RestMethod:

$response = Invoke-RestMethod -Uri "http://mysharepointsite.com/_api/web/lists/getByTitle('ListName')/items?`$filter=substringof('parameter', PropertyName)" -Credential $SPCredentials -Headers @{ "Accept" = "application/json;odata=verbose" } 

Write-Host $response

Output: I'm not sure how to get the count from this response, but I can see that the response contains more than 1 list item.

Am I doing something wrong here?

Was it helpful?

Solution

I had to escape the $ sign with a `backtick.

OTHER TIPS

_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Source')/items?$select=column&$top=1000";

$top is a great way of fetching the desired count from the list using rest api.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top