Need to add Add existing content type directly to SPO list using Powershell : this the code :
function Add-ContentType($loginUsername,$password,$url,$listTitle,$contentTypeID)
{
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($loginUsername, $password)
$ctx.Load($ctx.Web.Lists)
$ctx.ExecuteQuery()
$contentType=$ctx.Web.ContentTypes.GetById($contentTypeID)
$ctx.Load($contentType)
$ll=$ctx.Web.Lists.GetByTitle($listTitle)
$ctx.load($ll)
$ctx.load($ll.ContentTypes)
$ctx.ExecuteQuery()
$ll.ContentTypesEnabled=$true
$addedContentType=$ll.ContentTypes.AddExistingContentType($contentType)
$ll.Update()
try
{
$ctx.ExecuteQuery()
Write-Host "Adding content type " $addedContentType.Name " to " $ll.Title
}
catch [Net.WebException]
{
Write-Host $_.Exception.ToString()
}
}
the $ctx
have:
Web : Microsoft.SharePoint.Client.Web
Site : Microsoft.SharePoint.Client.Site
RequestResources : Microsoft.SharePoint.Client.RequestResources
FormDigestHandlingEnabled : True
ServerVersion : 16.0...
Url : https://Site/sites/aaa
ApplicationName : .NET Library
ClientTag :
DisableReturnValueCache : False
ValidateOnClient : True
AuthenticationMode : Default
FormsAuthenticationLoginInfo :
Credentials : Microsoft.SharePoint.Client.SharePointOnlineCredentials
WebRequestExecutorFactory : Microsoft.SharePoint.Client.DefaultWebRequestExecutorFactory
PendingRequest : Microsoft.SharePoint.Client.ClientRequest
HasPendingRequest : True
Tag :
RequestTimeout : 180000
StaticObjects : {[Microsoft$SharePoint$SPContext$Current, Microsoft.SharePoint.Client.RequestContext]}
ServerSchemaVersion : 15.0.0.0
ServerLibraryVersion : 16.0...
RequestSchemaVersion : 15.0.0.0
TraceCorrelationId : 0a4dc19d-c084-3000-c314-44a6737157a6
the problem is :
$ctx.Web.Lists =is empty
$ctx.Web.Lists.GetByTitle($listTitle) empty also.
any idea .