How Can I Add a Content Type to Asset Library Programmatically
-
31-01-2021 - |
Question
I need to add a content type whenever an Asset Library is provisioned. How can I do this ? We have SharePoint 2016 On-Prem
Regards, Bismarck
La solution
here is the script, change the $Site, $ListName, $ContentTypeName
:
asnp *sharepoint*
$Site = Get-SPSite https://sharepoint.dev.cz
$ListName = "Site Assets"
$ContentTypeName = "Picture"
$Web = $Site.OpenWeb()
Write-Host "Checking site:" $Web.Title
$docLibrary = $Web.Lists[$ListName]
if ($docLibrary -ne $null)
{
$docLibrary.ContentTypesEnabled = $true
$docLibrary.Update()
$ctToAdd = $Web.ContentTypes[$ContentTypeName]
$ct = $docLibrary.ContentTypes.Add($ctToAdd)
Write-Host "Content type" $ct.Name "added to list" $docLibrary.Title
$docLibrary.Update()
}
else
{
Write-Host "The list" $ListName "does not exist in site" $Site.Title
}
$site.Dispose()
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange