Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Set-SPOContentType
- {
- param (
- [Parameter(Mandatory=$true,Position=1)]
- [string]$Username,
- [Parameter(Mandatory=$true,Position=2)]
- $AdminPassword,
- [Parameter(Mandatory=$true,Position=3)]
- [string]$Url,
- [Parameter(Mandatory=$true,Position=4)]
- [string]$ListTitle,
- [Parameter(Mandatory=$true,Position=5)]
- [string]$ContentTypeName,
- [Parameter(Mandatory=$true,Position=6)]
- [string]$CTFieldInternalName
- )
- $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
- $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
- $ctx.ExecuteQuery()
- $rootWeb = $ctx.Web
- #$sites = $rootWeb.Webs
- $ctx.Load($rootWeb)
- #$ctx.Load($sites)
- $ctx.ExecuteQuery()
- #
- # -1- for each subsites in SC
- # foreach($site in $sites)
- # {
- # $ctx.Load($site)
- # $ctx.ExecuteQuery()
- #
- Write-Host ". Site name: "$rootWeb.Title -f Cyan # Write-Host $site.Title -f Cyan
- try
- {
- $ll=$rootWeb.Lists.GetByTitle($ListTitle)
- $ctx.Load($ll)
- $ctx.Load($ll.ContentTypes)
- $ctx.ExecuteQuery()
- Write-Host ".. List name: " $ll.Title
- foreach($cc in $ll.ContentTypes)
- {
- $ctx.Load($cc)
- $ctx.Load($cc.FieldLinks)
- $ctx.Load($cc.Fields)
- $ctx.ExecuteQuery()
- if($cc.Name -eq $ContentTypeName)
- {
- Write-Host "... CT name: "$cc.Name -f Cyan
- if($cc.ReadOnly -eq $true)
- {
- Write-Host -f Cyan "... ." $cc.Name" CT - ReadOnly is"$cc.ReadOnly" ...setting to False"
- $cc.ReadOnly =$false
- $cc.Update($false)
- foreach($field in $cc.FieldLinks)
- {
- if($field.Name -eq $CTFieldInternalName)
- {
- $ctx.Load($field)
- $ctx.ExecuteQuery()
- Write-Host -f Yellow "... .. Field Name:" $field.Name "Required:" $field.Required "...setting to False"
- $field.Required =$false
- Write-Host -f Green "... .. Required:" $field.Required
- $cc.Update($false)
- }
- }
- Write-Host -f Cyan "... setting CT ReadOnly back to True"
- $cc.ReadOnly =$true
- $cc.Update($false)
- $ctx.ExecuteQuery()
- Write-Host "___"
- }
- }
- }
- }
- catch
- {
- write-host "$($_.Exception.Message)" -foregroundcolor Yellow
- }
- }
- # Paths to SDK. Please verify location on your computer.
- # Download https://www.microsoft.com/en-us/download/details.aspx?id=42038
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
- # Insert the credentials and the name of the admin site
- $Username="user@MS"
- $AdminPassword=Read-Host -Prompt "Password" -AsSecureString
- $AdminUrl="https://SITE.sharepoint.com"
- $ListTitle="DocumentsTest"
- $ContentTypeName="Document"
- $CTFieldInternalName="WorkFax"
- Set-SPOContentType -Username $Username -AdminPassword $AdminPassword -Url $AdminUrl -ListTitle $ListTitle -ContentTypeName $ContentTypeName -CTFieldInternalName $CTFieldInternalName
Add Comment
Please, Sign In to add comment