none
Creating VIEWS using PowerShell Script RRS feed

  • Question

  • In SharePoint Online, we have many Document Libraries and have a need to create several Views. Requirements for Views are, that they need to have filters. 

    Lets say the Library content is 15000. That is 15000/5000 = 3 times more than the threshold.

    So create 3 Views.

    In all Views, be sure to select ID column. Under the filter option,

    A. Check "Show items only when the following is true.

    B. Select ID - is greater than or equal to 1

    AND

    C. Select ID - is less than or equal to 4,999

    For the next View,

    A. Check "Show items only when the following is true.

    B. Select ID - is greater than or equal to 5000

    and so on....

    This is a repetitive and a tedious task. Wondering if I can write a Power Shell Script to accomplish this task?

    Thanks a bunch!

    Mayank

    Friday, July 12, 2019 4:17 PM

All replies

  • Hi Mayankguru,

    Yes, you can create view in the document library by PowerShell script in sharepoint online.

    There is PowerShell script below for your reference.

    #Set parameter values
    
    $SiteURL="https://tenantname.sharepoint.com/sites/sitename"
    
    $ListName="libraryname"
    
    $ViewName="viewname"
    
    $ViewFields=@("Title","ID") # Comma separated - Internal Names
    
    $ViewQuery="<Where><And>
    
    <Geq><FieldRef Name = 'ID' /><Value Type = 'Integer'>1</Value></Geq>
    
    <Leq><FieldRef Name='ID' /><Value Type='Integer'>4999</Value></Leq>
    
    </And></Where>"
    
    $ItemLimit = "30"
    
     $IsDefaultView="$Flase"
    
                   
    
                   
    
                    $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 View exists in list already
    
            $ViewColl=$List.Views
    
            $Ctx.Load($ViewColl)
    
            $Ctx.ExecuteQuery()
    
            $NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
    
            if($NewView -ne $NULL) 
    
            {
    
                Write-host "View '$ViewName' already exists in the List!" -f Yellow
    
            }else{
    
                $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
    
                $ViewCreationInfo.Title = $ViewName
    
                $ViewCreationInfo.Query = $ViewQuery
    
                $ViewCreationInfo.RowLimit = $ItemLimit
    
                $ViewCreationInfo.ViewFields = $Viewfields
    
                $ViewCreationInfo.SetAsDefaultView = $IsDefaultView
    
     
    
                #sharepoint online powershell create view
    
                $NewView =$List.Views.Add($ViewCreationInfo)
    
                $Ctx.ExecuteQuery()   
    
                
    
                Write-host "New View Added to the List Successfully!" -ForegroundColor Green 
    
            }


    For ID 1-4999:

    $ViewQuery="<Where><And>

    <Geq><FieldRef Name = 'ID' /><Value Type = 'Integer'>1</Value></Geq>

    <Leq><FieldRef Name='ID' /><Value Type='Integer'>4999</Value></Leq>

    </And></Where>"

    For ID 5000-9999:

    $ViewQuery="<Where><And>

    <Geq><FieldRef Name = 'ID' /><Value Type = 'Integer'>5000</Value></Geq>

    <Leq><FieldRef Name='ID' /><Value Type='Integer'>9999</Value></Leq>

    </And></Where>"

    For ID 10000-14999:

    $ViewQuery="<Where><And>

    <Geq><FieldRef Name = 'ID' /><Value Type = 'Integer'>10000</Value></Geq>

    <Leq><FieldRef Name='ID' /><Value Type='Integer'>14999</Value></Leq>

    </And></Where>"

    For more detailed information, refer to the article below.

    SharePoint Online: PowerShell to Create a List View.

    http://www.sharepointdiary.com/2016/05/sharepoint-online-powershell-to-create-list-view.html

    Best regards,

    Sara Fan


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Monday, July 15, 2019 7:23 AM
    Moderator
  • Hi Mayankguru,

    If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.

    Best regards,

    Sara Fan

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, July 18, 2019 9:17 AM
    Moderator