0

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?

0

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

| improve this answer | |
0
_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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.