Take control of files created on SharePoint that are not published

Did you know that if you’re a SharePoint Site collection owner you cannot see all the pages created? A few days ago, I notice that I should have a certain number of pages on the Site Pages library (80 or something) but only a few appear for my user. You can verify how many files you have on the particular library on the Site contents of your site.

This happens because the page was created but nothing was been done with the page. Let’s give some example of how this can happen. You create a page on SharePoint but before adding any text or anything like that you just close the browser or navigate somewhere else. SharePoint will create a page with a random name that is saved for your user. This page will only be visible to your user, therefore, all the SharePoint Site collection owners will not see this file and this will create unnecessary information.

You can takeover this files following this steps

  1. Go to Library Settings of Site Pages
  2. Under Permissions and Management select Manage files which have no checked in version
  3. Select the files you want to have control and click on Take Ownership of Selection

The script below allows you to take control of the same files using PowerShell rather than UI. In case that you are doing some more actions on the site via PowerShell.

$library = "Site Pages"
$siteUrl = "https://tricks365.sharepoint.com/sites/YO"

Connect-PnPOnline -Url $siteUrl -UseWebLogin
$context = Get-PnPContext
$list = Get-PnPList -Identity $library

# Load the list
$context.Load($list)
Invoke-PnPQuery

# Load the checked out files
$checkedOutFiles = $list.GetCheckedOutFiles()
$context.Load($checkedOutFiles)
Invoke-PnPQuery

foreach ($file in $checkedOutFiles) {
    #Takeover the file
    Write-Host ("{0} File TakeOver" -f $file.ServerRelativePath.DecodedUrl) 
    $file.TakeOverCheckOut()
    Set-PnPFileCheckedIn -Url $file.ServerRelativePath.DecodedUrl
}

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *