Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. function Reset-SPFileContentType ($WebUrl, $ListName, $OldCTName, $NewCTName)
  2. {
  3. #Get web, list and content type objects
  4. $web = Get-SPWeb $WebUrl
  5. $list = $web.Lists[$ListName]
  6. $oldCT = $list.ContentTypes[$OldCTName]
  7. $newCT = $list.ContentTypes[$NewCTName]
  8. $newCTID = $newCT.ID
  9.  
  10. #Check if the values specified for the content types actually exist on the list
  11. if (($oldCT -ne $null) -and ($newCT -ne $null))
  12. {
  13. #Go through each item in the list
  14. $list.Items | ForEach-Object {
  15. #Check if the item content type currently equals the old content type specified
  16. if ($_.ContentType.Name -eq $oldCT.Name)
  17. {
  18. #Check the check out status of the file
  19. if ($_.File.CheckOutType -eq "None")
  20. {
  21. #Change the content type association for the item
  22. $_.File.CheckOut()
  23. write-host "Resetting content type for file" $_.Name "from" $oldCT.Name "to" $newCT.Name
  24. $_["ContentTypeId"] = $newCTID
  25. $_.Update()
  26. $_.File.CheckIn("Content type changed to " + $newCT.Name, 1)
  27. }
  28. else
  29. {
  30. write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
  31. }
  32. }
  33. else
  34. {
  35. write-host "File" $_.Name "is associated with the content type" $_.ContentType.Name "and shall not be modified"
  36. }
  37. }
  38. }
  39. else
  40. {
  41. write-host "One of the content types specified has not been attached to the list"$list.Title
  42. }
  43. $web.Dispose()
  44. }
  45. 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