Tagged: PowerShell

SharePoint Online: Approve Pages in Pending Status using PnP PowerShell

When publishing is enabled in SharePoint Online, all the edits of the documents/pages will have the minor versions (or drafts). All changes to any document isn’t available to users with read only permissions, until its Published. Published documents becomes major versions such as 1.0, 2.0, 3.0, etc.

Document will have multiple statuses: Scheduled, Draft, Pending, and Approved.

You can publish the individual document by right click on the document to publish –> In the context menu, choose More –> and then click on Publish.

You can use PowerShell to Publish multiple documents at ones:

 
#################################################################################################### 
# 
# Author.......: David Shvartsman 
# Date.........: 05/07/2021 
# Description..: SharePoint Online: Approve Pages in Pending Status using PnP PowerShell 
# 
####################################################################################################
CLS
$SiteURL = "https://contoso.sharepoint.com/sites/site1"
$LibraryName ="Library1"

Write-host "Processing $($SiteURL) ..."
Connect-PnPOnline -Url $SiteURL -Credentials $credential -IgnoreSslErrors

#Get All Files from the document library - In batches of 500 and _ModerationStatus equel 2 (Approval Status: Pending)
$ListItems = Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_["_ModerationStatus"] -eq 2} 
$DocumentsData=@()
Write-host "Number of items found in '$($LibraryName)' $($ListItems.count)"
ForEach($Item in $ListItems)
{
    #Collect Documents Data
    $DocumentsData += New-Object PSObject -Property @{
        FileName = $Item.FieldValues['FileLeafRef']
        FileURL = $Item.FieldValues['FileRef']
        Status = $Item.FieldValues._ModerationStatus
    }
    Set-PnPFileCheckedIn -Url $Item.FieldValues['FileRef'] -CheckinType OverwriteCheckIn -Comment "Automated Approval" -Approve
}
$DocumentsData | Export-csv "C:\temp\PendingPages.csv"


Write-host "Processing $($SiteURL) completed."
Write-host ""
Write-host ""
Write-host ""
Write-host ""

###################################################################################################

The values of ‘_ModerationStatus’ property are:

0 – Approved

2 – Pending

3 – Draft

4 – Scheduled

You can use the script to select pages in different Approval Status and Approve and/or Publish those using Set-PnPFileCheckedIn PnP command.

That was another page in the Chronicles of SharePoint Bits, happy scripting!

SharePoint Online: Using PowerShell to delete alerts in site/sub sites using CSOM

Metalogix Content Matrix is a great tool to migrate from SharePoint on-premises to the the Cloud. As part of migration to SharePoint Online we had to re-migrate some of the content multiple times. If users had alerts set up on the migrated items, those alerts will trigger as content gets re-migrated. Th prevent flood of the emails to the users we needed to deleted the migrated alerts.
Here is a PowerShell script to delete alerts in SharePoint Online. This PowerShell script will delete all alerts in root web and all sub webs in SharePoint Online site.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 09/14/2018
#  Description..: SharePoint Online: Delete alerts in site/sub sites using CSOM
#
####################################################################################################
CLS
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" -ErrorAction Stop
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" -ErrorAction Stop

#use the $AlertsExportFile to store existing Alers
$AlertsExportFile = "C:\temp\Alerts.csv"
if (Test-Path $AlertsExportFile) {
    Remove-Item $AlertsExportFile -Confirm:$false
}

$UserName = Read-Host "Please enter User Name"
$Password = Read-Host "Enter a Password" -AsSecureString
$SiteUrl = Read-Host "Please enter SharePoint Online Site URL"

$credentials= New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)

$ctx= New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = $credentials

$web = $ctx.Web
$ctx.Load($web)
$ctx.Load($web.Webs)
$siteUsers=$web.SiteUsers
$ctx.Load($siteUsers)
$siteUsers=$web.SiteUsers
$ctx.Load($siteUsers)
$alerts=$web.Alerts
$ctx.Load($alerts)
$ctx.ExecuteQuery()
Write-Host "Processing $($web.Url)"
#List all Alerts for RootWeb site
if ($alerts.Count -gt 0) {
    $alerts |SELECT @{Label="SiteUrl";Expression={"$($web.Url)"}},AlertFrequency, AlertTime, AlertType, AlwaysNotify, DeliveryChannels, EventType, Filter, ID, UserID, Status | Export-Csv $AlertsExportFile -Append -NoTypeInformation
}
foreach($alert in $alerts) {
	$web.Alerts.DeleteAlert($alert.ID)
	$web.Update()
	$ctx.ExecuteQuery()
}
#enumerate through seub webs
foreach ($webSub in $web.Webs) {
    Write-Host "Processing $($webSub.Url)"
    $alertsSub = $Null
    $ctxSub= New-Object Microsoft.SharePoint.Client.ClientContext($webSub.Url)
    $ctxSub.Credentials = $credentials
    $webSub = $ctxSub.Web
    $ctxSub.Load($webSub)
    $alertsSub=$webSub.Alerts
    $ctxSub.Load($webSub)
    $ctxSub.Load($alertsSub)
    $ctxSub.ExecuteQuery()
    #List all Alerts for each sub web
    if ($alertsSub.Count -gt 0) {
        $alertsSub |SELECT @{Label="SiteUrl";Expression={"$($webSub.Url)"}},AlertFrequency, AlertTime, AlertType, AlwaysNotify, DeliveryChannels, EventType, Filter, ID, UserID, Status | Export-Csv $AlertsExportFile -Append -NoTypeInformation
    }
    foreach($alert in $alertsSub){
	    $webSub.Alerts.DeleteAlert($alert.ID)
	    $webSub.Update()
	    $ctxSub.ExecuteQuery()
    }
}

###################################################################################################

All you will need is re-migrate the alerts and you all set!

That was another page in the Chronicles of SharePoint Bits, happy scripting!

PowerShell: working with JSON files

With introduction of Office 365 and SharePoint online, Microsoft increasingly using JSON file format to transfer data to and from the new platform. For example, Bulk UPA Custom Profile Properties Update API for SharePoint Online. For SharePoint administrator is natural to fall back to PowerShell to perform those tasks and automate the integration between the on-Premises environment and SharePoint Online.  Windows PowerShell 3.0 higher includes couple of commandlets (ConvertFrom-Json and ConvertTo-Json) to convert PowerShell objects to JSON format and back. One of the used cases is a fast and easy way to interact with web services.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 05/21/2018
#  Description..: Using PowerShell with JSON
#
#################################################################################################### 
#Save PowerShell object to the file in JSON format
$users | Where {$_name -like 'test'} | Select-Object $selectObject -skip 10 | 
            Select-Object -First 5 | ConvertTo-Json > C:\temp\users.json


#Read File in JSON format into PowerShell object
$users = Get-Content 'C:\temp\users.json' | Out-String | 
            ConvertFrom-Json | SELECT -expand Value | SELECT UserPrincipalName 

#Read Web Request response:
$WebRequestURL = "http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=BMI"
Invoke-WebRequest $WebRequestURL | ConvertFrom-Json  | select *


#################################################################################################### 

That was another page in the Chronicles of SharePoint Bits, happy scripting!