SharePoint Online: How to create a List view with PowerShell!

This time I’m sharing a new PowerShell Script that allows to create a List View in a SharePoint Online list using the Client Side Object Model (CSOM) in a PowrShell Script. You can download the PowerShell script from the following location: How to create a List View in SharePoint Online using PowerShell

############################################################################################################################################

#Script that allows to create a new view in a SharePoint Online List

# Required Parameters:

#  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

#  -> $sPassword: Password for the user.

#  -> $sSiteUrl: SharePoint Online Site Url.

#  -> $sListName: Name of the list where the new view is going to be added.

#  -> $sViewName: Name of the view to be added.

############################################################################################################################################

 

$host.Runspace.ThreadOptions = "ReuseThread"

 

#Definition of the function that allows to create a new view in a SharePoint Online list

function Create-NewListViewSPO

{

    param ($sSiteUrl,$sUserName,$sPassword,$sListName,$sViewName)

    try

    {    

        #Adding the Client OM Assemblies        

        Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"

        Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

 

        #SPO Client Object Model Context

        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)

        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)  

        $spoCtx.Credentials = $spoCredentials      

 

        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

        Write-Host "Adding the View $sViewName to the List $sListName !!" -ForegroundColor Green

        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green        

 

        #Getting the list to be updated with a new view        

        $spoList=$spoCtx.Web.Lists.GetByTitle($sListName)

        $spoCtx.Load($spoList)

 

        #Defining the new List View

        $spoViewCreationInformation=New-Object Microsoft.SharePoint.Client.ViewCreationInformation

        $spoViewCreationInformation.Title=$sViewName

        $spoViewCreationInformation.ViewTypeKind= [Microsoft.SharePoint.Client.ViewType]::None        

        $spoViewCreationInformation.RowLimit=30

        $spoViewCreationInformation.SetAsDefaultView=$true        

        $spoViewCreationInformation.ViewFields=@("Title","Created","Modified")        

 

        #Getting the collection of views of the List

        $spoListViews=$spoList.Views

        $spoCtx.Load($spoListViews)             

        $spoCtx.ExecuteQuery()                

        $spListViewToAdd=$spoListViews.Add($spoViewCreationInformation)

 

        #Adding the view to the List

        $spoCtx.Load($spListViewToAdd)                

        $spoCtx.ExecuteQuery()

        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

        Write-Host "View $sViewName added to the List $sListName !!" -ForegroundColor Green

        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green  

        $spoCtx.Dispose()

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()   

    }    

}

 

#Required Parameters

$sSiteUrl = "https://<O365Domain>.sharepoint.com/<SPO_Site>" 

$sUserName = "<O365User>@<O365Domain>.onmicrosoft.com" 

$sListName= "<SPO_List_Name>"

$sViewName="<SPO_View_Name>"

#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

$sPassword=convertto-securestring "<SPO_Password>" -asplaintext -force

 

Create-NewListViewSPO -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName -sViewName $sViewName

 

The result you will get once you execute the script in a SharePoint Online site is the following one:

image

Anuncios

2 pensamientos en “SharePoint Online: How to create a List view with PowerShell!

  1. Pingback: SharePoint & Office 365: Resumen de posts (I)! | Pasión por la tecnología...

  2. Pingback: SharePoint & Office 365: Resumen de posts (I)! - Blog de Juan Carlos González en Geeks.MS

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s