Create and delete SharePoint list views with PowerShell

Here’s how you can create and delete views in a SharePoint list or library using PowerShell (Is there anything that can’t be done through a PowerShell script?)

The script below creates a view called “TestView”.  It expects three command-line arguments:  site collection URL, the name of the view to create, and the list GUID.  The view that’s created is an exact replica of the “All Items” view (you can certainly modify the code as needed).  Here’s how you would call this script from the command line:


powershell  CreateView.ps1 "your_site_collection_URL" "TestView" "List GUID 6865306f-60e0-4889-addd-4fb9862e72e0"

Script code (use the button in the top right corner to copy it to the clipboard):


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.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 = "<Where><Gt><FieldRef Name='ID'/><Value Type='Counter'>0</Value></Gt></Where>"

 # 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 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(); ##ENFORCED DISPOSAL!!!

    

So far, so good?  Well, now that you’ve created a view, how do you delete it?  Follow the same logic, only instead of using the Add method of SPViewCollection object, we’ll be using the Delete method.

Same command line arguments as before:  site collection URL, name of the view, and list GUID.

Calling script from the command line:


powershell  DeleteView.ps1 "your_site_collection_URL" "TestView" "List GUID 6865346f-60e0-4889-addd-4fb3862572e0"

 


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

# reading command line arguments...

$siteURL = $args[0]

$strViewName = $args[1]

$ListGUID = $args[2] 
# creating a new site object and recursively searching through its lists

# 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))
{
foreach ($view in $list.Views)
{

If ( $view.Title.Contains($strViewName))
{

write-host "**********************************************"
write-host "Match found. Preparing to DELETE a view: ", $view.Title
write-host "List Title: ", $list.Title
write-host "List GUID: ", $list.ID

# you can insert a pause here if you like...

$list.Views.Delete($view.ID)

write-host "View ", $view.Title , " has been deleted successfully."

break

}
}

break

}

}

}
write-host "Done."

$site.Dispose(); ##ENFORCED DISPOSAL!!! 
 

Finally,  how do you find out the GUID of your list?  It’s fairly straightforward – you just need to access the SPList.ID property of your list.  Here’s a simple script that will output the GUIDs of all lists in your site collection:


[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
# write column headings

write-host "List URL ; Web URL ; Web Title ; List Title; List GUID ;"

# create a site object and recursively list all of its list objects and their details
 $site=new-object Microsoft.SharePoint.SPSite("http://your_site_collection_URL)  

  foreach ($web in $site.AllWebs)
 {
  foreach ($list in $web.Lists)
 {
 write-host $list.DefaultViewURL, ";", $web.URL , ";",  $web.Title , ";" , $list.Title , ";" , $list.ID

 }

 }

$site.Dispose(); ##ENFORCED DISPOSAL!!!

About these ads

2 Responses to “Create and delete SharePoint list views with PowerShell”


  1. 1 Kvepalai pigiau January 17, 2011 at 2:56 pm

    This really answered my problem, thank you!


  1. 1 SharePoint Filter List / Library by Querystring | SharePoint 2010 Performance Blog Trackback on February 28, 2012 at 1:34 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




RSS Information Week Headlines

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Joel Oleson Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Ben Curry’s Blog

  • I'm at the ATL users group Monday night, Nov 16th November 15, 2009
  • Upgrading to SharePoint Server 2010 November 3, 2009

RSS Mauro Cardarelli’s Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Susan Hanley’s KM Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint PS Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Lori Gowin’s Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Greg Kamer’s Blog

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 268,896 hits

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: