Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ozkary/0e448626361cdecdc7bb085fe135f3a2 to your computer and use it in GitHub Desktop.
Save ozkary/0e448626361cdecdc7bb085fe135f3a2 to your computer and use it in GitHub Desktop.
With PowerShell, we can use the invoke-RestMethod cmdlet to make Restful API calls to get JSON data. When using the SharePoint Restful APIs, the cmdlet returns the JSON data in string format which is not the intended result.
#set the headers content type
$contentType = 'application/json;odata=verbose'
$headers = @{}
$headers["Accept"] = $contentType
$headers["Content-Type"] = $contentType
$uri = "ozkary.sharepoint.com/GetByTitle('LIST-NAME')/items"
$data = Invoke-RestMethod -Method GET -Uri $uri -UseDefaultCredentials -Headers $headers
#returns string
$data.GetType()
#ERROR - returns error that there are duplicate attributes
#(Id, ID) which is the SharePoint default
$json = ($data | ConvertFrom-Json )
#TIP - this works because we replace ID attribute to a #different name
#it should parse the string and return a JSON object
$json = ($data.replace("ID","IDTemp") | ConvertFrom-Json )
#we should be able to access the results array
$json.d.results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment