1

I'm using SP2010 and trying to understand how to pass a filter to Invoke-RestMethod. I recently learned about adding the ` character before the filter. But the script below doesn't have an error or return anything. If I remove the tick mark before the filter it returns all the items when there should be only one item that matches 'Movies'.

$ListItem = Invoke-RestMethod "https://mysite/market/_vti_bin/listdata.svc/Products?`$filter=Title eq 'Movies'"
$ListItem| ForEach-Object {$_.content.properties}

How do I make the filter work when using the Invoke-RestMethod?

1

You syntax is correct - but you need to include an Accept header to tell the service if you want the results as XML or JSON. To get XML results use this code:

$ListItem = Invoke-RestMethod "https://mysite/market/_vti_bin/listdata.svc/Products?`$filter=Title eq 'Movies'" -Headers @{"Accept"="application/atom+xml" }
$ListItem.Title     # Gets the value of the Title property 

To get the results as JSON use this:

$result = Invoke-RestMethod "https://mysite/market/_vti_bin/listdata.svc/Products?`$filter=Title eq 'Movies'" -Headers @{"Accept"="application/json;odata=verbose" }
$ListItem = $result.d.results
$ListItem.Title     # Gets the value of the Title property 

Finally, you will of course also need to authenticate with SharePoint. For Windows authentication, simply use the parameter -Credential or -UseDefaultCredentials

| improve this answer | |
  • 1
    Thank you Lars. This is perfect. – gsr51 Oct 24 '19 at 15:45

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.