Powershell and Sharepoint – Ep. 2 – Updating a picture library on O365 from Flickr site

…this post was published on 2016 July 24.

Hi

​bellow is the script ready to be used to update a picture library in Sharepoint Online with information from Flicrk.com

$results = @()
Import-Module MSOnline
$global:cred = Get-Credential
Connect-MsolService -Credential $global:cred

$url = “https://romeodonca-public.sharepoint.com”
$mylist =”pl_Wallpaper”
Add-Type -Path “your_path\Microsoft.SharePoint.Client.dll”
Add-Type -Path “your_path\Microsoft.SharePoint.Client.Runtime.dll”
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.username, $cred.Password) 
$clientContext.Credentials = $cred
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
$lists = $web.Lists
$clientContext.Load($lists)
$clientContext.ExecuteQuery()
#$lists | fl Title
$mylist =$lists.GetByTitle($mylist)
$clientContext.Load($mylist)
$clientContext.ExecuteQuery()

$items = $mylist.GetItems($query)
$clientContext.Load($items)
$clientContext.ExecuteQuery()
cls
Write-Host -BackgroundColor Yellow -ForegroundColor Red In total $items.count fotografii
cls
foreach ($item in $items){
if($item.FieldValues.FlickrCode -gt 1)
{
######## reading from flickr based on flickrID
$URI = “https://www.flickr.com/photos/romeodonca/”+$item.FieldValues.FlickrCode+”/”
$HTML = Invoke-WebRequest -Uri $URI
$content = $html.ParsedHtml.getElementsByTagName(‘span’) | Where-Object { $_.className -eq ‘fave-count-label’}

####### initializing faves on Flickr versus Onsite
$onflickr = $content.textContent
$onsite = $item.FieldValues.FlickrFaves
########### checking if the image have receive new faves onFlickr
if ($onsite -lt $onflickr){
$cucat = $onflickr – $onsite
“The image with ID “+$item.FieldValues.ID + ” and the title “+$item.FieldValues.Title + ” received more “+$cucat + ” faves”
$Final_Faves = $onflickr

###### defining the row for log file
    $row = new-object PSObject -Property @{
    ID = $item.FieldValues.ID;
    FileName = $item.FieldValues.FileLeafRef;
    Title = $item.FieldValues.Title;
    Initial_Faves = $onsite;
    Final_Faves = $onflickr
    }
    $results +=$row
    $item[“FlickrFaves”]=  $Final_Faves
    $item.update()
$clientContext.ExecuteQuery()
}
}

}

$resultsnumber = $results.Count
if ( $resultsnumber -eq 0 ){
cls
Write-Host -BackgroundColor White -ForegroundColor Red ”                                                                                           “
Write-Host -BackgroundColor White -ForegroundColor Red ”                                                                                           “
Write-Host -BackgroundColor White -ForegroundColor Red ”                                                                                           “
Write-Host -BackgroundColor White -ForegroundColor Red (”  OOOOpssss, nothing new on Flickr. Come back later, maybe some one liked your photos! 🙁  “).ToUpper()
Write-Host -BackgroundColor White -ForegroundColor Red ”                                                                                           “
Write-Host -BackgroundColor White -ForegroundColor Red ”                                                                                           “
Write-Host -BackgroundColor White -ForegroundColor Red ”                                                                                           “

}

$results

############ Details regarding Followers and Following

$URI = “https://www.flickr.com/photos/romeodonca”
$HTML = Invoke-WebRequest -Uri $URI
$contentfollow = $html.ParsedHtml.getElementsByTagName(‘P’) | Where-Object { $_.className -eq ‘followers truncate’}
(($contentfollow.innerText).Split(“•”))[0]
(($contentfollow.innerText).Split(“•”))[1]
$contentphotos = $html.ParsedHtml.getElementsByTagName(‘P’) | Where-Object { $_.className -eq ‘photo-count’}
$contentphotos.innerText
$URI = “https://www.flickr.com/photos/romeodonca/albums”
$HTMLalbum = Invoke-WebRequest -Uri $URI
$contentalbum = $htmlalbum.ParsedHtml.getElementsByTagName(‘div’) | Where-Object { $_.className -eq ‘view photo-list-album-view requiredToShowOnServer’}

for ($i = 0; $i -le ($contentalbum.count-1) ; $i++)
{
(((((($contentalb​um[$i]).innerHTML).split(“=”))[1]).replace(‘class’,’ ‘)).replace(‘”‘,’ ‘)).trim()
}​

This entry was posted in Office 365, Powershell, Sharepoint Online, YouTube. Bookmark the permalink.