Búsqueda de sitios web

SharePoint Online: Agregar columna de moneda a la lista usando PowerShell


Requisito: Agregar la columna de moneda a la lista de SharePoint Online.

¿Cómo crear un campo de moneda en la lista de SharePoint Online?

De manera similar al campo numérico, la columna Moneda en SharePoint Online captura valores de moneda como metadatos y agrega opciones de formato de moneda.

  1. Vaya a su sitio de SharePoint Online y navegue hasta la lista de destino donde desea agregar la columna Moneda.
  2. En la pestaña Lista, haga clic en el botón "Crear columna" en la cinta.
  3. Proporcione el nombre de su nueva columna y especifique el tipo como "Moneda".

  4. Desplácese hacia abajo, complete otros valores opcionales, como valores mínimos y máximos, decimales, formato de moneda, etc., y haga clic en "Aceptar" para crear un campo de moneda en la lista de SharePoint Online.

PowerShell para agregar la columna de moneda a la lista en 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"

#Custom function to add column to list
Function Add-CurrencyColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE"
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $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)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            $FieldSchema = "<Field Type='Currency' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues'/>"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="TotalInvestment"
$DisplayName="Total Investment"
$Description="Enter the Total Investment"

#Call the function to add column to list
Add-CurrencyColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description 

Para admitir parámetros adicionales, como valores mínimos y máximos, decimales y formato de moneda, puede agregarlos a los parámetros y al esquema de campo. Aquí está el guión:


#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"

#Custom function to add column to list
Function Add-CurrencyColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues = "FALSE",
        [Parameter(Mandatory=$true)] [string] $Min,
        [Parameter(Mandatory=$true)] [string] $Max,
        [Parameter(Mandatory=$true)] [string] $Decimals,
        [Parameter(Mandatory=$true)] [string] $LCID
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $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)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            $FieldSchema = "<Field Type='Currency' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' Min='$Min' Max='$Max' Decimals='$Decimals' LCID='$LCID' />"
            
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="TotalInvestment"
$DisplayName="Total Investment"
$Description="Enter the Total Investment"
$Min="1"
$Max="100000"
$Decimals="2"
$LCID="1081" #India

#Call the function to add column to list
Add-CurrencyColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Min $Min -Max $Max -Decimals $Decimals -LCID $LCID 

PnP PowerShell para agregar la columna de moneda a la lista de SharePoint Online

Aquí está PnP PowerShell para agregar un campo de moneda a la lista de SharePoint Online. Reemplace las variables con los valores apropiados para su sitio de SharePoint Online, nombre de lista y propiedades de campo de moneda.


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

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

#Add currency Field to list
Add-PnPField -Type Currency -List $ListName -DisplayName "Total Budget" -InternalName "TotalBudget" -AddToDefaultView

De manera similar, puedes agregar un campo de moneda desde el esquema XML:


#Define XML Schema for currency Field
$FieldXML= "<Field Type='Currency' Name='TotalBudget' ID='$([GUID]::NewGuid())' DisplayName='Total Budget' Required ='FALSE' Decimals='2' LCID='1033'></Field>"

#Add Currency Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

Agregar un campo de moneda a una lista de SharePoint Online es una forma sencilla y eficaz de almacenar y administrar datos financieros. Con esta guía paso a paso, puede agregar fácilmente una columna de moneda a su lista y configurar sus ajustes para cumplir con sus requisitos específicos.

Artículos relacionados