1

I tried to delete custom content type from Pages library but got an exception "content type is still in use". The I get to know that files should also be deleted from "Manage files which have no checked in version" in order to delete content type from Pages library.

How I can perform this step using powershell, any code hint can help me?

1
  • You would need to follow the same process as the UI, take ownership of them so they are checked out to you, then delete them. Commented Mar 2, 2016 at 13:20

2 Answers 2

1
clear-host

$web = get-spweb http://yourwebsite
$list = $web.Lists["your list"]

#get checked out files that have no major version
$checkedOutFiles = $list.CheckedOutFiles | ?{$_.Versions.Count -eq 0}

write-host ("Found "  + $checkedOutFiles.Count + " file(s)!")

foreach($file in $checkedOutFiles)
{
    write-host ("Parsing file: " + $file.LeafName)
    $file.TakeOverCheckOut()
    #deleting!
    $list.GetItemById($file.ListItemId).Recycle() | Out-Null    
}
1

He developed a short little set of PowerShell Functions that can take ownership of the files, then recursively check in all files that are being checked out by the system account. tryt his

#Add SharePoint Snapin if not using SharePoint's PowerShell Console
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}
#First function takes ownerhip of all unmanaged files with no version
function Take-Ownership{
param(
$spSite,
$spList
)
$site = Get-SPWeb -Identity $spSite
$list = $site.Lists[$spList]

$list.CheckedOutFiles | ForEach-Object{
$_.LeafName+ " had it's ownership taken from: "+$_.CheckedOutBy.DisplayName+"`n"
$_.TakeOverCheckOut()
}
}
#This Function takes a folder object and recursively checks in
#all files which are checked out by the System Account
function checkin-AllFiles{
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[Microsoft.SharePoint.SPFolder]
$currFolder
)

$currFolder.Files | ForEach-Object {
if ($_.CheckedOutByUser.UserLogin -eq "SHAREPOINT\system"){
$_.CheckIn("File checked in by administrator")
$_.Name+" has been checked in`n"
}
}
$currFolder.SubFolders| ForEach-Object{
checkin-AllFiles -currFolder $_
}
}
#This function basically combines the previous 2 for primary use
function Checkin-AllUnmanagedFiles{
param(
$site,
$library
)
$spSite = Get-SPWeb -identity $site
$spFolder = $spSite.Folders[$library]

Get-Date
"Taking ownership of the following Files`n"
Take-Ownership -spSite $site -spList $library
"-----------------------------------------------------------------------------`n"

    Get-Date
"Checking in the following files`n"
checkin-AllFiles -currFolder $spFolder
Get-Date
}

https://brenthafnersblog.wordpress.com/2012/10/16/manage-files-which-have-no-checked-in-version-list-larger-than-list-view-threshold/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.