Guest User

Untitled

a guest
Dec 11th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. function Set-SPOContentType
  2. {
  3.  
  4. param (
  5. [Parameter(Mandatory=$true,Position=1)]
  6. [string]$Username,
  7. [Parameter(Mandatory=$true,Position=2)]
  8. $AdminPassword,
  9. [Parameter(Mandatory=$true,Position=3)]
  10. [string]$Url,
  11. [Parameter(Mandatory=$true,Position=4)]
  12. [string]$ListTitle,
  13. [Parameter(Mandatory=$true,Position=5)]
  14. [string]$ContentTypeName,
  15. [Parameter(Mandatory=$true,Position=6)]
  16. [string]$CTFieldInternalName
  17. )
  18.  
  19. $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  20. $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
  21. $ctx.ExecuteQuery()
  22.  
  23. $rootWeb = $ctx.Web
  24. #$sites = $rootWeb.Webs
  25.  
  26. $ctx.Load($rootWeb)
  27. #$ctx.Load($sites)
  28. $ctx.ExecuteQuery()
  29.  
  30. #
  31. # -1- for each subsites in SC
  32. # foreach($site in $sites)
  33. # {
  34. # $ctx.Load($site)
  35. # $ctx.ExecuteQuery()
  36. #
  37. Write-Host ". Site name: "$rootWeb.Title -f Cyan # Write-Host $site.Title -f Cyan
  38. try
  39. {
  40. $ll=$rootWeb.Lists.GetByTitle($ListTitle)
  41.  
  42.  
  43. $ctx.Load($ll)
  44. $ctx.Load($ll.ContentTypes)
  45. $ctx.ExecuteQuery()
  46. Write-Host ".. List name: " $ll.Title
  47. foreach($cc in $ll.ContentTypes)
  48. {
  49. $ctx.Load($cc)
  50. $ctx.Load($cc.FieldLinks)
  51. $ctx.Load($cc.Fields)
  52. $ctx.ExecuteQuery()
  53.  
  54. if($cc.Name -eq $ContentTypeName)
  55. {
  56. Write-Host "... CT name: "$cc.Name -f Cyan
  57. if($cc.ReadOnly -eq $true)
  58. {
  59. Write-Host -f Cyan "... ." $cc.Name" CT - ReadOnly is"$cc.ReadOnly" ...setting to False"
  60. $cc.ReadOnly =$false
  61. $cc.Update($false)
  62.  
  63. foreach($field in $cc.FieldLinks)
  64. {
  65. if($field.Name -eq $CTFieldInternalName)
  66. {
  67. $ctx.Load($field)
  68. $ctx.ExecuteQuery()
  69. Write-Host -f Yellow "... .. Field Name:" $field.Name "Required:" $field.Required "...setting to False"
  70. $field.Required =$false
  71. Write-Host -f Green "... .. Required:" $field.Required
  72. $cc.Update($false)
  73. }
  74. }
  75. Write-Host -f Cyan "... setting CT ReadOnly back to True"
  76. $cc.ReadOnly =$true
  77. $cc.Update($false)
  78. $ctx.ExecuteQuery()
  79. Write-Host "___"
  80. }
  81.  
  82. }
  83.  
  84. }
  85.  
  86. }
  87. catch
  88. {
  89. write-host "$($_.Exception.Message)" -foregroundcolor Yellow
  90. }
  91. }
  92.  
  93.  
  94. # Paths to SDK. Please verify location on your computer.
  95. # Download https://www.microsoft.com/en-us/download/details.aspx?id=42038
  96. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  97. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  98.  
  99. # Insert the credentials and the name of the admin site
  100. $Username="user@MS"
  101. $AdminPassword=Read-Host -Prompt "Password" -AsSecureString
  102. $AdminUrl="https://SITE.sharepoint.com"
  103. $ListTitle="DocumentsTest"
  104. $ContentTypeName="Document"
  105. $CTFieldInternalName="WorkFax"
  106.  
  107.  
  108. Set-SPOContentType -Username $Username -AdminPassword $AdminPassword -Url $AdminUrl -ListTitle $ListTitle -ContentTypeName $ContentTypeName -CTFieldInternalName $CTFieldInternalName
Add Comment
Please, Sign In to add comment