0

I'm running SharePoint 2010 and I have a requirement to be able to query SharePoint from Powershell without having the SharePoint snap-in. I have been experimenting with some code that I found but I'm having trouble filtering the list items. The script below always displays all the items in the output when I'm expecting one item. What's the best way to filter list items?

$url = "https://mysite/market/_vti_bin/listdata.svc/Products?$filter=Title eq 'Movies'"

$request = [System.Net.WebRequest]::Create($url) 
$request.UseDefaultCredentials = $true 
$request.Accept = ‘application/json;odata=verbose’

$response = $request.GetResponse() 
$reader = New-Object System.IO.StreamReader $response.GetResponseStream() 
# ConvertFrom-Json : Cannot convert the Json string because a dictionary converted from it contains duplicated keys ‘Id’ and ‘ID’. 
$response = $reader.ReadToEnd() 
#$response = $reader.ReadToEnd() -creplace ‘"ID":’, ‘"DummyId":’

$result = ConvertFrom-Json -InputObject $response 
$result.d.results | select Id, Title
1

Modify the url as below, add `before $filter:

$url = "http://sp10/sites/test/_vti_bin/listdata.svc/Products?`$filter=Title eq 'Movies'"

$request = [System.Net.WebRequest]::Create($url) 
$request.UseDefaultCredentials = $true 
$request.Accept = ‘application/json;odata=verbose’

$response = $request.GetResponse() 
$reader = New-Object System.IO.StreamReader $response.GetResponseStream() 
# ConvertFrom-Json : Cannot convert the Json string because a dictionary converted from it contains duplicated keys ‘Id’ and ‘ID’. 
$response = $reader.ReadToEnd() 
#$response = $reader.ReadToEnd() -creplace ‘"ID":’, ‘"DummyId":’

$result = ConvertFrom-Json -InputObject $response 
$result.d.results | select Id, Title

This is the test list data:

enter image description here

This is the filtered result in PowerShell:

enter image description here

  • Thank you Jerry! This works great now. Is there any reason this same filter wouldn't work with Invoke-RestMethod? $ListItem = Invoke-RestMethod "sp10/sites/test/_vti_bin/listdata.svc/Products?`$filter=Title eq 'Movies''" -UseDefaultCredentials $ListItem| ForEach-Object {$_.content.properties} – gsr51 Oct 24 at 12:19
  • See my other related question which is similar but uses Invoke-RestMethod. sharepoint.stackexchange.com/questions/271214/… – gsr51 Oct 24 at 12:36
  • @gsr51,you are welcome. Invole-RestMethod question has been answered. – Jerry_MSFT Oct 25 at 2:10

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.