Change Content Type With PowerShell

Objective:

Change the content type of many files in a single library.

Solution:

Use the following PowerShell script to change the content type instead of editing each file one at a time. In my example, I change the content type from “Document” to “Process”.


function Reset-SPFileContentType ()
{
#Get web, list and content type objects
$web = Get-SPWeb -Identity http://spsite
$list = $web.Lists["Documents"]
$oldCT = $list.ContentTypes["Document"]
$newCT = $list.ContentTypes["Process"]
$newCTID = $newCT.ID

#Check if the values specified for the content types actually exist on the list
if (($oldCT -ne $null) -and ($newCT -ne $null))
{
#Go through each item in the list
$list.Items | ForEach-Object {
#Check if the item content type currently equals the old content type specified
if ($_.ContentType.Name -eq $oldCT.Name)
{
#Check the check out status of the file
if ($_.File.CheckOutType -eq "None")
{
#Change the content type association for the item
$_.File.CheckOut()
write-host "Resetting content type for file" $_.Name "from" $oldCT.Name "to" $newCT.Name
$_["ContentTypeId"] = $newCTID
$_.Update()
$_.File.CheckIn("Content type changed to " + $newCT.Name, 1)
}
else
{
write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
}
}
else
{
write-host "File" $_.Name "is associated with the content type" $_.ContentType.Name "and shall not be modified"
}
}
}
else
{
write-host "One of the content types specified has not been attached to the list"$list.Title
}
$web.Dispose()
}
Reset-SPFileContentType

Credit to Phil Childs’ article for SharePoint 2010. The script also works for SP 2013. 

Library Paging – First Page Button

Adding a “First Page” button is a simple work around for getting back to the first page when paging through documents. This is especially handy if you use document sets.

  1. Edit the page and add a Script Editor web part.
  2. Click EDIT SNIPPET.embed
  3. Embed the following code:

<!DOCTYPE html>
<html>
<body>

<input type="button" value="First Page" onclick="location.href=''"/>

</body>
</html>

I moved the Script Editor web part down below the library web part and changed the Direction to Right to Left.

scripteditor

Final product:

docsetbutton.PNG

Next page:

docsetbutton1.PNG

Unlock Documents With PowerShell

One of my users was constantly getting locked out of his own documents. Upon saving a document, he would receive the following error:

The file “filename” is locked for exclusive (or shared) use by “his username””

The issue was that his user only had “contribute” permissions. While that should be enough, he needed “edit” permissions to avoid locking his own documents.

While troubleshooting, I used a PowerShell script that unlocks documents that are locked for shared use.


Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Variables for Web and File URLs
$WebURL ="http://twainstlportal.twainfinancial.com/sites/crmdocs/BD"
$FileURL = "http://sp2013/sites/company/filename.docx"

#Get Web and File Objects
$web = Get-SPWeb $WebURL
$File = $web.GetFile($FileURL)

#Check if File is Checked-out
if ($File.CheckOutType -ne "None")
{
Write-host "File is Checked Out to user: " $File.CheckedOutByUser.LoginName
Write-host "Checked Out Type: " $File.CheckOutType
Write-host "Checked Out On: " $File.CheckedOutDate

#To release from checkout, ask the checked out user to check in
#$File.Checkin("Checked in by Administrator")
#Write-host "File has been Checked-In"
}

#Check if File is locked
if ($File.LockId -ne $null)
{
Write-host "File is Loked out by:" $File.LockedByUser.LoginName
Write-host "File Lock Type: "$file.LockType
Write-host "File Locked On: "$file.LockedDate
Write-host "File Lock Expires on: "$file.LockExpires

#To Release the lock, use:
#$File.ReleaseLock($File.LockId)
#Write-host "Released the lock!"
}

Change the filename to the URL of the locked document. Uncomment line 32 and 33 to unlock the document.