Basically in SPO, default.aspx are discontinued however while migration default pages also get migrated. Here this piece of code finding default pages as it may set up Homepage of sites.
Here, we are finding those default pages across site and subsites and approving it along with the particular user who created or modified.
- #Passing inputs through CSV file
- # Pass Site URL and Library Name as Inputs.
- Param(
- [Parameter(Mandatory = $true)]
- [string] $Csv)
- # Pass your DLL 's
- Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
- Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
- Import - Module Microsoft.Online.SharePoint.PowerShell
- # Not required
- for on premise site - Start
- $userId = "[email protected]"
- $pwd = Read - Host - Prompt "Enter password" - AsSecureString
- $creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $root = Import - Csv $Csv
- foreach($record in $root) {
- $TargetSite = $record.SharePointUrl
- $LibraryName = $record.LibraryName
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($TargetSite)
- # Not required
- for on premise site - Start
- $ctx.credentials = $creds
- # Iterating across subsites and finding
- default pages.
- $web = $ctx.Web
- $ctx.Load($web.Webs)
- $ctx.ExecuteQuery()
- if ($web.Webs.Count - gt 0) {
- foreach($AllWeb in $web.Webs) {
- try {#
- Get all files from the Library
- $List = $AllWeb.Lists.GetByTitle($LibraryName)
- $q = New - Object Microsoft.SharePoint.Client.CamlQuery
- $q.ViewXml = "<View Scope='RecursiveAll' />"
- $Items = $List.GetItems($q)
- $ctx.Load($Items)
- $ctx.ExecuteQuery()
- $FileName = "default.aspx"
- Foreach($Item in $Items) {
- $File = $Item["FileRef"].Substring($Item["FileRef"].LastIndexOf("/") + 1)
- if ($File - eq $FileName) {
- if ($Item["_ModerationStatus"] - eq "2" - and($Item["Editor"].LookupValue - eq("xxxUserName") - or $Item["Editor"].LookupValue - eq("xxxUserName"))) {
- $Item["_ModerationStatus"] = "0"
- $Item.Update()
- $ctx.ExecuteQuery()
- write - host "done"
- }
- }
- }
- } catch {
- $_ | Out - File errors.txt - Append
- }
- }
- }
- if ($web.Webs.Count - eq 0) {
- try {
- #Get all files from the Library
- $List = $AllWeb.Lists.GetByTitle($LibraryName)
- $q = New - Object Microsoft.SharePoint.Client.CamlQuery
- $q.ViewXml = "<View Scope='RecursiveAll' />"
- $Items = $List.GetItems($q)
- $ctx.Load($Items)
- $ctx.ExecuteQuery()
- $FileName = "default.aspx"
- Foreach($Item in $Items) {
- $File = $Item["FileRef"].Substring($Item["FileRef"].LastIndexOf("/") + 1)
- if ($File - eq $FileName) {
- if ($Item["_ModerationStatus"] - eq "2" - and($Item["Editor"].LookupValue - eq("xxxUserName") - or $Item["Editor"].LookupValue - eq("xxxUserName"))) {
- $Item["_ModerationStatus"] = "0"
- $Item.Update()
- $ctx.ExecuteQuery()
- write - host "done"
- }
- }
- }
- } catch {
- $_ | Out - File errors.txt - Append
- }
- }
- }
Moderation Status is a 4-byte integer indicating the moderation approval status of a list item.
Configurations can require moderation approval to publish a list item or allow automatic approval. A published list item MUST have a Moderation Status of 0. The following are all possible valid values for Moderation Status.
Value | Description |
0 | The list item is approved. |
1 | The list item has been denied approval. |
2 | The list item is pending approval. |
3 | The list item is in the draft or checked out state. |
4 | The list item is scheduled for automatic approval at a future date. |
RecursiveAll gets all files and all folders under the specified location.
Hope this may help you! Happy Coding.