Búsqueda de sitios web

PowerShell para agregar y quitar campos al tipo de contenido en SharePoint


Si necesita agregar un campo a un tipo de contenido o eliminar un campo del tipo de contenido existente, use este script de PowerShell:

Script de PowerShell para agregar un campo al tipo de contenido:


#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()

¡Agreguemos algo de manejo de errores y creemos una función reutilizable para agregar una columna de sitio al tipo de contenido usando PowerShell!

PowerShell para agregar una columna de sitio al tipo de contenido en 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  
 }
}

Eliminar campo del tipo de contenido usando PowerShell:

Así es como puede eliminar una columna de sitio de un tipo de contenido mediante programación


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  
 }
}

Ahora, llamemos a la función respectiva para agregar o eliminar la columna del sitio del tipo de contenido usando 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

y el resultado va aquí:

Esto actualiza mediante programación el tipo de contenido dado. Aquí está mi otra publicación sobre el script de PowerShell para agregar una columna de sitio a SharePoint: Crear una columna de sitio en SharePoint usando PowerShell

Artículos relacionados