Búsqueda de sitios web

SharePoint Online: ¿Cómo obtener ID de tipo de contenido usando PowerShell?


Los tipos de contenido en SharePoint se usan para definir los metadatos y los comportamientos de un documento o elemento en una lista y se identifican mediante un ID único conocido como ID de tipo de contenido. Mi otro artículo habla sobre Cómo encontrar la identificación del tipo de contenido en SharePoint, usando la interfaz de usuario de SharePoint y PowerShell para SharePoint local; Esta publicación proporciona una secuencia de comandos para obtener el tipo de contenido por identificación mediante programación para SharePoint Online.

¿Cómo encontrar el ID del tipo de contenido en SharePoint Online?

Una forma de obtener el ID del tipo de contenido en SharePoint Online es utilizar la interfaz de usuario de SharePoint Online. Así es cómo:

Para obtener el ID del tipo de contenido en SharePoint Online, puede ir a Lista/Tipos de contenido del sitio >> Elija su tipo de contenido >> ¡La URL tendrá el parámetro “Ctype” con el ID del tipo de contenido!

SharePoint Online: obtener ID de tipo de contenido mediante CSOM

Aquí está el script de PowerShell para obtener el ID del tipo de contenido en SharePoint usando CSOM:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ContentTypeName="Crescent Project Report"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get content types of the web
    $ctx.Load($ctx.Web.ContentTypes)
    $Ctx.ExecuteQuery()

    #Get the content type by its name
    $ContentType = $Ctx.Web.ContentTypes | Where {$_.Name -Match $ContentTypeName}
    $Ctx.Load($ContentType)    
    $Ctx.ExecuteQuery()

    #sharepoint online get content type id
    write-host -f Green "Content Type ID:" $ContentType.Id
}
Catch {
    write-host -f Red "Error Getting Content Type ID!" $_.Exception.Message
}

Obtener ID de tipo de contenido de la lista de SharePoint Online

Obtengamos la identificación del tipo de contenido para un tipo de contenido de lista:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$ListName="Projects"
$ContentTypeName="Project Portfolio"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the List and Its content types
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.load($List)
    $Ctx.load($List.ContentTypes)
    $Ctx.ExecuteQuery()

    #Get the content type by its name
    $ContentType = $List.ContentTypes | Where {$_.Name -Match $ContentTypeName}
    $Ctx.Load($ContentType)    
    $Ctx.ExecuteQuery()

    #sharepoint online get content type id
    write-host -f Green "Content Type ID:" $ContentType.Id
}
Catch {
    write-host -f Red "Error Getting Content Type ID!" $_.Exception.Message
}

Este script obtiene el ID del tipo de contenido del nombre mediante PowerShell.

PnP PowerShell para obtener ID de tipo de contenido en SharePoint Online

Utilice el cmdlet Get-PnPContentType para recuperar el tipo de contenido y sus propiedades.


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Project Proposal V1"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName

#Get Content Type ID
$ContentType.Id.ToString() 

De manera similar, use el parámetro "Lista" para obtener el ID del tipo de contenido en el nivel de la lista. P.ej.


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Project Documents"
$ContentTypeName ="Crescent Invoice Template V2"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

#Get Content Type ID
$ContentType.Id.ToString() 

Reemplace las variables con la URL del sitio, la lista y los nombres del tipo de contenido que desea recuperar.

En conclusión, obtener el ID del tipo de contenido en SharePoint Online se puede realizar a través de la interfaz de usuario o mediante PowerShell. El ID de tipo de contenido es un identificador único para un tipo de contenido y se utiliza para hacer referencia a él mediante programación. Al utilizar los métodos anteriores, puede recuperar fácilmente el ID del tipo de contenido para un tipo de contenido específico en SharePoint Online.

Artículos relacionados