Question

i need help with readonly content type on list.

I have sitecollection ex. "sites/SC".

In that sitecollection i have 1000 subwebs ex. "sites/SC/web1".

In each subweb i have one document library ex. "DOCS" (all subwebs are same).

In that library "DOCS" i have content type "Document" and that content type is set to readonly=True (need to be readonly=False).

After that i need to change one field in that list content type from Required=True to Required=False and set that list content type back to Readonly=True.

Need powershell script which:

  1. loop all subwebs
  2. get list by name
  3. get list CT by name
  4. if list CT readonly is set to true, set readonly to false
  5. get CT field by name
  6. if field Required is set to true, change Required to false
  7. change CT back to readonly=true
  8. proceed to next subweb

Thanks

Was it helpful?

Solution

    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 Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.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
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top