Thursday, May 12, 2016

Take ownership of SharePoint files with no checked in version, check them in, and publish with Powershell


This script will take ownership of all files in a library, then set whatever required metadata needs to be set, then check the items in.

Handy if a user has uploaded a ton of data using Explorer View, but never updated the metadata, and never actually checked in the files.  

if ((Get-PSSnapin | ? { $_.Name -eq "Microsoft.SharePoint.PowerShell" }) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

function BrowseItems($list, $Locationterm, $Departmentterm) {
    $files = $list.CheckedOutFiles
    foreach($file in $files)
    {
               $file.TakeOverCheckOut() #Take ownership of checked out document
    }


    foreach ($item in $list.Items) {
        $itemFile = $item.File

        if ($itemFile -ne $null) {
            if ($itemFile.CheckOutStatus -ne "None") {

                $LocationtaxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["GAC Location"]
                $DepartmenttaxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["GAC Department"]
                $fileName = $itemFile.Name
                $userName = $itemFile.CheckedOutByUser.Name;

                $item["Title"] = $fileName
                $LocationtaxField.SetFieldValue($item,$Locationterm)
                $DepartmenttaxField.SetFieldValue($item,$Departmentterm)
                $item.update()

               
                #Write-Host "Doing automatic CheckIn on the item" -f green -NoNewline
                $itemFile.CheckIn("Automatic CheckIn (Administrator)")
                #Write-Host " Done!" -f Yellow

                if( $list.EnableVersioning -and $list.EnableMinorVersions) {
                    #Write-Host "Doing automatic Publish on the item" -f green -NoNewline
                    $itemFile.Publish("Automatic Publish (Administrator)")
                    Write-Host "." -f Yellow
                }
            }
        }
    }
}

$url = "http://somesite.com/sites/teamsite/subweb"

$web = Get-SPWeb $url
$site = $web.Site

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")

$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]

$Locationgroup = $termStore.Groups["Locations"]
$LocationtermSet = $Locationgroup.TermSets["Location"]
$Locationterms = $LocationtermSet.GetAllTerms()
$Locationterm = $Locationterms | ?{$_.Name -eq "SomeCity"}

$Departmentgroup = $termStore.Groups["Departments"]
$DepartmenttermSet = $Departmentgroup.TermSets["Department"]
$Departmentterms = $DepartmenttermSet.GetAllTerms()
$Departmentterm = $Departmentterms | ?{$_.Name -eq "SomeDepartment"}

$list = $web.lists["Some Document Library"]

BrowseItems $list $Locationterm $Departmentterm

2 comments:

  1. Hi, I am just wondering what are the things that i need to update in order for this script to run on our SharePoint. Is it only the "URL".

    ReplyDelete
    Replies
    1. Hi Romnick,

      Looking at the script a second time, the two fields for GAC Location and GAC Department were pieces of Managed Metadata that needed to be cleared in our case before we could take ownership of them. That's what the first half of the script does.

      We needed to set a specific Location and Department, which is what the last few lines of code does.

      If you don't have to update data from the Term Store, this script would be much smaller. Mainly just:

      if ((Get-PSSnapin | ? { $_.Name -eq "Microsoft.SharePoint.PowerShell" }) -eq $null) {
      Add-PSSnapin "Microsoft.SharePoint.PowerShell"
      }

      function BrowseItems($list, $Locationterm, $Departmentterm) {
      $files = $list.CheckedOutFiles
      foreach($file in $files)
      {
      $file.TakeOverCheckOut() #Take ownership of checked out document
      }

      And the

      #Write-Host "Doing automatic CheckIn on the item" -f green -NoNewline
      $itemFile.CheckIn("Automatic CheckIn (Administrator)")
      #Write-Host " Done!" -f Yellow

      if( $list.EnableVersioning -and $list.EnableMinorVersions) {
      #Write-Host "Doing automatic Publish on the item" -f green -NoNewline
      $itemFile.Publish("Automatic Publish (Administrator)")
      Write-Host "." -f Yellow

      Hope that helps!

      Delete