Forum Discussion
Download image from news list, getpreview.ashx
We need to download content and images from our Sharepoint news items to put these into our kiosk solution. Getting the text from the items is simple however for the images we cannot find where they are stored. All images for a news item (and other pages) are consumed and stored somewhere and then rendered in sharepoint via layouts/15/getpreview.ashx? which has a webguid, siteguid and file guid passed as parameters.
my question is twofold, where are the images saved - in a hidden list somewhere? - we cant find one.
is is possible to download an image via some mechanism?
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
- David BargnaIron Contributor
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() - Thomas BermanIron Contributor
News posts create a new folder in your Site Assets library - which contain the images associated with that news post.
Question 1. Go to Site Assets > Site Pages > Folder with your news post's title. There you'll find the images associated with that news post.
Question 2. You can use PowerShell to grab that item (the image) and download the filestream then write it to your disk. You can also download through the UI.
- David BargnaIron Contributor
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