Búsqueda de sitios web

SharePoint Online: ocultar un botón de la cinta mediante una acción personalizada en PowerShell


Requisito: Botón Ocultar nuevo documento de SharePoint Online en la cinta.

PowerShell agregará una acción personalizada para ocultar el botón Nuevo documento de una biblioteca

Aquí está el PowerShell para ocultar el nuevo botón en la biblioteca de documentos de SharePoint Online:


#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"
  
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName ="Team Documents"
$Location = "Ribbon.Documents.New.NewDocument"
$CustomActionTitle ="HideNewButton"

Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the List
    $List = $Ctx.Web.lists.GetByTitle($ListName)

    #Get Existing Custom Actions
    $UserCustomActions= $List.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    #Check if the CustomAction Exists already
    $CustomAction = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle }

    If($CustomAction -eq $Null)
    {
        #Add new custom action
        $UserCustomAction = $List.UserCustomActions.Add()

        #Set the Properties of the custom action
        $UserCustomAction.Name = $CustomActionTitle
        $UserCustomAction.Title = $CustomActionTitle
        $UserCustomAction.Location = "CommandUI.Ribbon" 
        $UserCustomAction.CommandUIExtension = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location='$($Location)'/></CommandUIDefinitions></CommandUIExtension>"
        $UserCustomAction.Sequence = 1000
        #$UserCustomAction.RegistrationType = "List" # On Web or Site Scope, Use these two properties
        #$UserCustomAction.RegistrationId = 101
        $UserCustomAction.Update()
        $Ctx.ExecuteQuery()

        Write-Host -f Green "Custom Action Added Successfully!"
    }
    Else
    {
         write-host -f Yellow "Custom Custom Already Exists!"
    }
}
Catch {
        write-host -f Red "Error Adding Custom Action!" $_.Exception.Message
}

Esto oculta el botón de nuevo documento de la biblioteca de documentos determinada en SharePoint Online. De manera similar, puede ocultar cualquier botón en la cinta de SharePoint Online, como el botón de carga, el botón de edición de página, el botón de nuevo elemento, etc.

PowerShell para eliminar la acción personalizada:

Si desea deshacer los cambios eliminando la acción personalizada creada, utilice este script de PowerShell.


#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName ="Team Documents"
$CustomActionTitle="HideNewButton"

Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get Existing Custom Actions
    $List = $Ctx.Web.lists.GetByTitle($ListName)
    $UserCustomActions= $List.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    #Check if the CustomAction Exists
    $CustomActions = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle }

    If($CustomActions -ne $Null)
    {
        ForEach($CustomAction in $CustomActions)
        {
            $CustomAction.Deleteobject()
            Write-Host -f Green "Custom Action Removed Successfully:" $CustomAction.Name
        }
        $Ctx.ExecuteQuery()
    }
    Else
    {
         write-host -f Yellow "Custom Actions Does Not Found!"
    }
}
Catch {
        write-host -f Red "Error Removing Custom Action!" $_.Exception.Message
}

También puede eliminar todas las acciones personalizadas de la lista o biblioteca determinada eliminando la clase Where en la línea n.° 22.

También puede ocultar un botón en la barra de herramientas de SharePoint Online usando JSON: ¿Cómo ocultar un botón en la lista de SharePoint Online o en la barra de herramientas de la biblioteca de documentos?

Artículos relacionados