Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Reset-SPFileContentType ($WebUrl, $ListName, $OldCTName, $NewCTName)
- {
- #Get web, list and content type objects
- $web = Get-SPWeb $WebUrl
- $list = $web.Lists[$ListName]
- $oldCT = $list.ContentTypes[$OldCTName]
- $newCT = $list.ContentTypes[$NewCTName]
- $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 –WebUrl <Site URL> –ListName <Document library display name> –OldCTName <Content type to be replaced> –NewCTName <Content type to replace it with>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement