Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How it is possible to add a filter to a list with PowerShell? (I want to add a filter to multiple lists)

Is there any tutorial, articel or example?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

To simply add filter to default view of existing list you can use:

$web = Get-SPWeb -Identity "http://mysamplesite"
$list = $web.GetList("http://mysamplesite/Lists/mysamplelist")
$query = "<Where><Eq><FieldRef Name='MyField' /><Value Type='Text'>MyFilterValue</Value></Eq></Where>"    
$view = $list.DefaultView;
$view.Query = $query
$view.Update();

You can always use different approach and add new view to list:

$web = Get-SPWeb -Identity "http://mysamplesite"
$list = $web.GetList("http://mysamplesite/Lists/mysamplelist")
$query = "<Where><Eq><FieldRef Name='MyField' /><Value Type='Text'>MyFilterValue</Value></Eq></Where>"
$view = $list.DefaultView;
$fields = $list.DefaultView.ViewFields.ToStringCollection()
$list.Views.Add("NewView", $fields, $query, 100, $true, $false)

Note: This script copies fields from default view.

This is all just a simple example so feel free to ask additional questions.

share|improve this answer
    
Thanks! Great example! –  LaPhi Jul 27 '11 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.