Create a List View with PowerShell

1. Grab the GUID of the List. Simple way to do it is to go your existing List you want to create the view in, click the “Create View” button (but don’t actually create it in the UI) and copy the value of the ListID querystring variable. You then need to clean it up a bit. For example, take the bold portion of the following URL:

/site/_layouts/ViewType.aspx?List=<strong>%7B8DF8879E%2D5046%2D406D%2DBCBD%2D50C4777AF50D%7D</strong>&Source=http%3A%2F%2Fmy%2Eitgroove%2Enet%2Fsite%2FMyList%2FForms%2FAllItems%2Easpx

Take the extracted string and delete the portions in red, which are the first and last three characters (which are the HTML encoded characters { and } respectively), and replace the instances of %2D in green (HTML encoded characters to indicate a blank space) with a single blank space .

%7B8DF8879E%2D5046%2D406D%2DBCBD%2D50C4777AF50D%7D

So we are left with the string 8DF8879E 5046 BCBD 50C4777AF50D , which is a properly formatted GUID and is your List ID.

2. Plunk the following script into a new text file called CreateListView.ps1 and put it on your SharePoint server:

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
# reading command line arguments
$siteURL = $args[0]
$strViewName = $args[1]
$ListGUID = $args[2]
# enter your CAML query for the view here...
$strQuery = "0"
# create a new SPsite object and recursively go through all webs
# until a matching list GUID is found
$site=new-object Microsoft.SharePoint.SPSite($siteURL)
foreach ($web in $site.AllWebs)
{
foreach ($list in $web.Lists)
{
$ListTempGUID = $list.ID.ToString()
if ($ListTempGUID.Contains($ListGUID))
{
write-host "************"
write-host "Match has been found. Preparing to create a view: ", $strViewName
write-host "List Title: ", $list.Title
write-host "List GUID: ", $list.ID
$fields = $list.Views["All Items"].ViewFields.ToStringCollection()
$result = $list.Views.Add($strViewName, $fields, $strQuery, 100, $True, $False , "HTML", $False)
write-host "View ", $strViewName , " was created successfully."
break
}
}
}
write-host "Done."
$site.Dispose();

3. To execute the PowerShell, go to your PowerShell command line and execute the following. Note that you need to replace the following sections in green:

– “your_site_collection_URL” with the URL of your site collection e.g. “http://my.itgroove.net/sitecollectionname”
– the “TestView” with your new list name
– the ListID GUID with the List GUID we just extracted

powershell CreateListView.ps1 “your_site_collection_URL” “TestView” “List GUID 8DF8879E 5046 406D BCBD 50C4777AF50D

Syntax Formatting for PowerShell in Visual Studio 2010

PowerShell is an essential tool for advanced SharePoint development & administration. Unfortunately Visual Studio doesn’t offer PowerShell code syntax highlighting or Intellisense natively. You can however use Adam Driscoll’s plugin PowerGUI VSX from Codeplex, which runs on top of PowerGUI :

PowerGUI V requires the free, standalone application PowerGUI. Please download the correct version:

Some useful sources for SharePoint PowerShell scripts and advice:
http://sharepointpsscripts.codeplex.com/releases/view/40582
http://powergui.org/servlet/KbServlet/download/2812-102-4534/SharePoint2010PowerShell.pdf
http://blog.falchionconsulting.com/

[learn_more caption=”From TechNet: ” state=”open”] Windows PowerShellâ„¢ command-line interface is a new command-line tool and supporting scripting language from Microsoft that complements Cmd.exe in the Windows administration context. In the SharePoint administration context, Windows PowerShell supersedes the Stsadm.exe administration tool. Moving forward, you should use Windows PowerShell scripting technology to develop any new command-line scripts in SharePoint Foundation 2010.[/learn_more]

 

  • 1
  • 2