Use PowerShell to Access ListItems via SharePont 2013 REST
#Function to access SharePoint List Items via REST
function Get-ListItemsREST
{
param($siteurl, $listName);
$url = ($siteurl + "/_api/web/lists/getByTitle('" + $listName + "')/items");
$cookies = New-Object System.Net.CookieContainer;
$request = [System.Net.WebRequest]::Create($url);
$request.Method="GET";
$request.CookieContainer = $cookie;
#this header was what I had been missing for on premises authentication
$request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED","f");
$request.UseDefaultCredentials=$true;
$response=$request.GetResponse();
$stream = New-Object System.IO.StreamReader($response.GetResponseStream());
$responsestring = $stream.ReadToEnd();
[xml]$xmlresponse = $responsestring;
return $xmlresponse.feed.entry;
}
#SAMPLE USAGE
$listitems = Get-ServerList -siteurl "https://spsiteurl/" -listName "Announcements"
foreach ($item in $listitems)
{
$item.content.Properties;
}
Comments
Post a Comment