Forum Discussion
Download image from news list, getpreview.ashx
- Jun 13, 2019
thanks, that's we assumed, however we have 70 news posts, and only 2 site assets folders, some images are stored in the 'images' folder.
the solution we came up works well, as we can specify the image size to download and send to our kiosk system via powershell without having to worry about the original location of the image.
just for completion, here is the script to download images, metadata and stick them into a json which are kiosk consumes.
#start
$cred = New-Object System.Management.Automation.PsCredential ....
Connect-PnPOnline 'https://xxxxxx.sharepoint.com/sites/news' -Credentials $cred
#kiok json
$simple_json=@{
"lang"="en"
"featured"=$true
"subtitle"=$title
"description"=$desc
"links"=$url
"contacts"=$contactemail
"visuals"=@{
"public images"=@{
"hide"=$false
"id"=$imageurl
"main_visual"=$true
}
}
"release_date"=$date
"title"=$title
}
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.password )
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$rootpath="\\somewhere\"
$stuff=@()
#get items
Foreach ($item in $newslist)
{
# desctription.
if ($item.FieldValues.Description -ne $null){
#http content.
$simple_json.description=$item.FieldValues.Description
$simple_json.title=$item.fieldvalues.Title
$simple_json.visuals.'public images'.id="$rootpath$($item.fieldvalues.Title).jpg"
$webClient.DownloadFile($item.FieldValues.BannerImageUrl.Url, "$rootpath$($item.fieldvalues.Title).jpg")
$stuff+=$simple_json
}
}
$webClient.Dispose()
$stuff | ConvertTo-Json |Out-File kiosk.json -Encoding utf8
not an ideal solution, but its the best we could come up with...
$userName = "username"
$password = "pwd"
$pageUrl = "https://xxx.sharepoint.com/_layouts/15/getpreview.ashx?guidSite=2bdaba4a-9050-4df1-a642-xxxxxxecfc&guidWeb=bb46b281-1410-4eff-8256-2fe997a63560&guidFile=2aebd28b-d5e6-4885-a7b5-1dc455ed44e4"
$image = "c:\junk\someimage.jpg"
#create secure password
$sPassword = $password | ConvertTo-SecureString -AsPlainText -Force
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $sPassword)
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$webClient.DownloadFile($pageUrl, $limage)
$webClient.Dispose()