Office 365: Comment créer une document library qui utilise les ContentTypeHub avec PowerShell et CSOM
On a vu précédemment comment gérer le Content Type Hub dans un Tenant Office 365:
Cette solution permet donc de publier un ensemble de types de contenu à travers le tenant Office 365.
Mais comment utiliser avec PowerShell ce type de contenu en créant une liste documentaire ayant pour type de contenu ces CTHub ?
Nous allons voir dans cet exemple, un script PowerShell basé sur le composant CSOM qui permet les actions suivantes:
- Créer une document library vide
- Ajouter 3 types de contenu (à partir des GUID que je connais, Excel, PowerPoint et Word)
- Sauvegarder cette document library dans les modèles de la collection de sites pour l’utiliser à tous les niveaux de la collection
Script PowerShell
[string]$username = "AdminAccount@tenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SCRIPTSREPO\ExportedPWD-$($username).txt"
[string]$ListTemplateName = "List Template Gallery"[string]$CompanyDocLibInternalName = "_SPOCTHub_COMPANY_Documents_"
[string]$CompanyDocLibTitle = "COMPANY Documents"
[string]$CompanyDocLibDescription = "COMPANY Documents Library with COMPANY Templates"# Get needed information from end user
[string]$SiteCollectionURL = https://tenant.sharepoint.com/sites/yoursitecoll
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)function Load-DLLandAssemblies
{
[string]$defaultDLLPath = ""# Load assemblies to PowerShell session
$defaultDLLPath = "C:\SCRIPTSREPO\NUGET_CSOM\microsoft.sharepointonline.csom.16.1.6008.1200\lib\net40-full\Microsoft.SharePoint.Client.dll"
[System.Reflection.Assembly]::LoadFile($defaultDLLPath)$defaultDLLPath = "C:\SCRIPTSREPO\NUGET_CSOM\microsoft.sharepointonline.csom.16.1.6008.1200\lib\net40-full\Microsoft.SharePoint.Client.Runtime.dll"
[System.Reflection.Assembly]::LoadFile($defaultDLLPath)$defaultDLLPath = "C:\SCRIPTSREPO\NUGET_CSOM\microsoft.sharepointonline.csom.16.1.6008.1200\lib\net40-full\Microsoft.Online.SharePoint.Client.Tenant.dll"
[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}Function AddCompanyContentType()
{
Param(
[Microsoft.SharePoint.Client.ClientContext]$Context
)<#
CT to add
Content Type name: Company Excel - ID: 0x01010046DD666F4125264BBBFC7EA3596C1371
Content Type name: Company Word With Logo - ID: 0x0101005D8D60776F8605419F84F7FF61E87E1A
Content Type name: Company PowerPoint - ID: 0x010100DE0DA8C046A65F499A445B0B74C55DADCT to remove
Content Type name: Document - ID: 0x0101
#>
[Microsoft.SharePoint.Client.List]$list = $context.web.lists.GetByTitle($CompanyDocLibInternalName)$ctIDCompanyWord = "0x010100DE0DA8C046A65F499A445B0B74C55DAD"
$ctCompanyWord = $context.web.contenttypes.getbyid($ctIDCompanyWord)
$context.load($ctCompanyWord)$ctIDCompanyPPT = "0x0101005D8D60776F8605419F84F7FF61E87E1A"
$ctCompanyPPT = $context.web.contenttypes.getbyid($ctIDCompanyPPT)
$context.load($ctCompanyPPT)$ctIDCompanyExcel = "0x01010046DD666F4125264BBBFC7EA3596C1371"
$ctCompanyExcel = $context.web.contenttypes.getbyid($ctIDCompanyExcel)
$context.load($ctCompanyExcel)$context.load($list)
$context.load($list.contenttypes)$context.executeQuery()
$list.ContentTypesEnabled = $true$AddctCompanyWord = $list.ContentTypes.AddExistingContentType($ctCompanyWord)
$list.update()
$AddctCompanyPPT = $list.ContentTypes.AddExistingContentType($ctCompanyPPT)
$list.update()
$AddctCompanyexcel = $list.ContentTypes.AddExistingContentType($ctCompanyExcel)
$list.update()write-host " >> info: Enabled multiple content types"
#send the request containing all operations to the server
try{
$context.executeQuery()
write-host "info: added the Company content types to the list" -foregroundcolor green
}
catch{
write-host "info: $($_.Exception.Message)" -foregroundcolor red
}$ctIDDocument = "0x0101"
$ctNameDocument = "Document"
$ctCollectionInCompanyDocLib = $list.contenttypes
$context.load($ctCollectionInCompanyDocLib)
$context.executeQuery()
foreach($CompanyDocLibCT in $ctCollectionInCompanyDocLib)
{
if ($CompanyDocLibCT.Name -eq $ctNameDocument)
{
$CTCompanyDocLibToRemove = $true
$ctDocument = $CompanyDocLibCT
write-host " >> CT Name In the Company DocLib Before removal:", $CompanyDocLibCT.Name, "- ID:" $CompanyDocLibCT.ID
break
}
}
if($CTCompanyDocLibToRemove -eq $true)
{
$ctDocument.DeleteObject();
try{
$context.executeQuery()
write-host "info: Removal of Document CT to the list" -foregroundcolor green
}
catch{
write-host "info: $($_.Exception.Message)" -foregroundcolor red
}
}
$list.Title = $CompanyDocLibTitle
$list.OnQuickLaunch = $true
$list.EnableVersioning = $true
$list.update()
write-host " >> info: Update the DocLib name"
try{
$context.executeQuery()
write-host "info: Update the listName to set normal name" -foregroundcolor green
}
catch{
write-host "info: $($_.Exception.Message)" -foregroundcolor red
}
$list.SaveAsTemplate("_Company_Documents_With_Template.stp","_Company Documents","Company Document with Templates",$false)
write-host " >> info: Save List as Template"
try{
$context.executeQuery()
write-host "info: Save List as Template" -foregroundcolor green
}
catch{
write-host "info: $($_.Exception.Message)" -foregroundcolor red
}
}Function Create-CompanyDocLib()
{
Param(
[Microsoft.SharePoint.Client.ClientContext]$Context
)
$CompanyDocLibTemplateType = 101$lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$lci.title = $CompanyDocLibInternalName
$lci.description = $CompanyDocLibDescription
$lci.TemplateType = $CompanyDocLibTemplateType
$list = $context.web.lists.add($lci)
$context.load($list)
#send the request containing all operations to the server
try{
$context.executeQuery()
write-host " >>> info: Company DocLib Created $($CompanylistTitle)" -foregroundcolor green
AddCompanyContentType -Context $Context}
catch{
write-host "info: $($_.Exception.Message)" -foregroundcolor red
}Write-Host " ---------------------------------------------------------"
}
Function Create-CompanyDocumentsWithCTHub([string]$SitecollectionURLToUse)
{
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($SitecollectionURLToUse)$creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($Username, $secureStringPwd)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName,$creds.Password)
$ctx.RequestTimeout = 1000000 # milliseconds
#$ctx.Load($ctx.Web)
$spoweb = $ctx.Web
$ctx.Load($spoweb)
$ctx.ExecuteQuery()Write-Host
Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreenWrite-Host " "
Write-Host " ---------------------------------------------------------"
Write-Host " >>>> # Server Version:" $ctx.ServerVersion " # <<<<<<" -ForegroundColor Green
Write-Host " ---------------------------------------------------------"
Write-Host " "Create-CompanyDocLib -Context $ctx
}cls
Load-DLLandAssemblies
Create-CompanyDocumentsWithCTHub $SiteCollectionURL
Une fois le script modifié selon vos besoin, vous aurez dans la collection une nouvelle liste documentaire apparaissant dans le menu de navigation avec les Type de contenus choisis.Si ces content types ont aussi un document modèle associés, celui-ci s’ouvrira dans Office Online directement.
Liens utiles:
- http://www.sharepointfire.com/2016/01/get-sharepoint-online-list-templates-powershell/
- http://www.sharepointdiary.com/2014/01/how-to-upload-list-template-using-powershell.html
- http://stackoverflow.com/questions/37151982/sharepoint-online-powershell-csom-create-list-from-custom-template
- https://blogs.technet.microsoft.com/fromthefield/2014/02/19/office-365-powershell-script-to-upload-files-to-a-document-library-using-csom/
- http://www.sharepointfire.com/2016/01/create-new-document-library-sharepoint-online-powershell/
- http://www.c-sharpcorner.com/blogs/how-to-delete-the-content-type-from-the-list-using-csom-in-sharepoint-2013
Conclusion
Il ne vous reste plus qu’à communiquer autour de cette nouvelle liste documentaire reprenant vos standards internes et disponible dans toute la collection avec la création du modèle de liste.
Fabrice Romelard [MBA Risk Management]
Commentaires
Enregistrer un commentaire