Tuesday, March 12, 2013

SharePoint PowerShell Create View Based on Existing View

The following PowerShell script crawls the specified lists, copies the fields from the All Items views and creates a new view named "Created By Me" and sets it as default. Tested with SharePoint 2010 but should work with 2013 as well. Tested with list but not with Document Libraries.

I used this script as a base.


$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
 Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

$web = Get-SPWeb -Identity "http://portal/sites/Requests"
$lists=$web.Lists["User Request", "Hardware Request",  "Employee Request"]
$SourceView="All Items"
$NewViewName="Created By Me"
$NewViewDefault=$true


foreach($list in $lists) {

 $view = $list.Views[$SourceView]
 $Viewfields = $list.Views[$SourceView].ViewFields.ToStringCollection()
 $viewRowLimit="100"
 $viewPaged=$true
 $viewDefaultView=$NewViewDefault

# Setting the Query for the View
 $viewQuery = ""
 $viewName = $NewViewName

# Finally – Provisioning the View
 $myListView = $list.Views.Add($viewName, $viewFields, $viewQuery, 100, $True, $False, "HTML", $False)

# You need to Update the View for changes made to the view
# Updating the List is not enough
 $myListView.DefaultView = $True
 $myListView.Update()
 $list.Update()
}

$web.Dispose()

5 comments:

  1. Hi Tobias,
    http:///_catalogs/users/simple.aspx
    http:///_catalogs/users/detail.aspx
    i want to update this views with new columns lets say Department and BU, i can do it manually but i have 400 site collections. i want to automate this process powershell,
    any help ?

    ReplyDelete
  2. Please refer my post to create list, fields and view SharePoint list using powershell script:
    http://sharepointquicksolutions.blogspot.in/2014/06/create-sharepoint-list-using-powershell.html

    http://sharepointquicksolutions.blogspot.in/2014/06/create-view-in-list-using-powershell.html

    Please provide your valuable feedback.

    ReplyDelete
  3. To learn math in playing game than tap to the given game that have the technique to learn cool math in this cool math games junction and your kids love plya nad learn in the math playground.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. You are here with the useful program. Programs like this will simplifies the work. This code is like a guide from an expert in essay services free gift . Thanks for sharing.

    ReplyDelete