Recherche de site Web

PowerShell pour ajouter/supprimer des champs au type de contenu dans SharePoint


Si vous devez ajouter un champ à un type de contenu ou supprimer un champ du type de contenu existant, utilisez ce script PowerShell :

Script PowerShell pour ajouter un champ au type de contenu :


#Get Web Object
$web = Get-SPWeb "https://sharepoint.company.com"

#Get Content Type and Field
$ContentType=$web.ContentTypes["Content-Type-Name"]
$FieldToAdd=$web.Fields["Field-Name"]

#Add Field to Content type
$FieldLink=New-Object Microsoft.SharePoint.SPFieldLink($FieldToAdd)
$ContentType.FieldLinks.Add($FieldLink)
$ContentType.Update()

Ajoutons un peu de gestion des erreurs et créons une fonction réutilisable pour ajouter une colonne de site au type de contenu à l'aide de PowerShell !

PowerShell pour ajouter une colonne de site au type de contenu dans SharePoint


#Add SharePoint Snap-in
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

Function Add-FieldToContentType([Microsoft.SharePoint.SPWeb] $web, [string]$ContentTypeName, [string]$SiteColumnName)
{
 #Get Content Type and Field (Site column) objects 
 $ContentType=$web.ContentTypes[$ContentTypeName]
 #Check if the content type exists
 if($ContentType -ne $null)
 {
  #Check if the content type has the Field already
  if(!$ContentType.Fields.ContainsField($SiteColumnName))
  {
   #Check if the site column exists
   if($web.Fields.ContainsField($SiteColumnName))
   {
    $FieldToAdd=$web.Fields.GetField($SiteColumnName)#[$SiteColumnName]
    #Add Site column to the content type
    $FieldLink= New-Object Microsoft.SharePoint.SPFieldLink($FieldToAdd)
    $ContentType.FieldLinks.Add($FieldLink)
    $ContentType.Update($true)
    Write-Host "Site Column Field Added to the Content Type!" -ForegroundColor Green
   }
   else
   {
    Write-Host "Site Column Not Found!"  -ForegroundColor Red
   }
  }
  else
  {
   Write-Host "Field Exists Already!" -ForegroundColor Red
  }
 }
 else
 {
  Write-Host "Content type not found!" -ForegroundColor Red  
 }
}

Supprimer le champ du type de contenu à l'aide de PowerShell :

Voici comment supprimer une colonne de site d'un type de contenu par programmation


Function Remove-FieldFromContentType([Microsoft.SharePoint.SPWeb] $web, [string]$ContentTypeName, [string]$FieldNameToRemove)
{
 #Get Content Type and Field (Site column) objects 
 $ContentType=$web.ContentTypes[$ContentTypeName]
 #Check if the content type exists
 if($ContentType -ne $null)
 {
  #Check if the content type has the Field 
  if($ContentType.Fields.ContainsField($FieldNameToRemove))
  {
   #Rempve the Field from the content type
   $ContentType.FieldLinks.Delete($FieldNameToRemove)
   $ContentType.Update($true)
   Write-Host "Field removed from the Content Type!" -ForegroundColor Green
  }
  else
  {
   Write-Host "Field Doesn't Exists in the Content Type!" -ForegroundColor Red
  }
 }
 else
 {
  Write-Host "Content type not found!" -ForegroundColor Red  
 }
}

Maintenant, appelons la fonction respective pour ajouter ou supprimer la colonne du site du type de contenu à l'aide de PowerShell :


#Configuration parameters
$WebURL="https://portal.crescent.com"
$ContentTypeName="CrescentInvestments"
$FieldName="FullName"  # Internal Name of the field

#Get the Web
$Web = Get-SPWeb -Identity $WebURL 

#Call the method to Add field to content type
Add-FieldToContentType $Web $ContentTypeName $FieldName
#Remove-FieldFromContentType $Web $ContentTypeName $FieldName

et le résultat est ici :

Cela met à jour par programme le type de contenu donné. Voici mon autre article sur le script PowerShell pour ajouter une colonne de site à SharePoint : Créer une colonne de site dans SharePoint à l'aide de PowerShell

Articles connexes