A quick look at Listviews and Listview webparts in SharePoint 2010

I always wondered on how list views and list view webparts are related. Very recently I explored on this and found something interesting to share...

Let me put the scenario in a problem statement...

We have a List: MyList. We have a custom view associated, named: MyListview


There are many pages in our SharePoint site where we are using list view webparts to display the content using MyListview.


Now, we need to add a field/column: testcolumn in MyList and that should be added to all the list view webparts.

In order to solve this issue, I actually divided the problem in 2 parts:

     1. Adding the column to a list view

     2. Adding the column to a list view web part.

 Adding the column to a list view is fairly simple. We can achieve that using PowerShell

 $web = Get-SPWeb http://myTestsite
 $list = $web.Lists["MyList "]
 $column = $list.Fields["testcolumn "]
 $list.Views["MyListview "].Add($column)


But how can I ensure that the field is getting added in an existing list view web part, as by default even if I add that column in MyListview, it won't reflect in the existing web part.  When we are adding a list view web part; we refer a view using which list items are displayed. Then why it is that, any changes made on that list view web part are not reflecting to the actual listview.

Undoubtedly, it’s a copy of that list view which is being used. Again we are having 2 questions:

1. Why do we maintain a copy of list view and not refer to the actual list view?

2. If it’s a copy then, how can we identify them?


When we are using list view webparts, it’s an obvious expectation , that we should be able to customize the list view web parts in our SharePoint pages.

Now, if we are not using the copy and instead referring the same list view, then we are restricting the users to customize list view web parts independently and in turn all the pages which are using MyListview will not have an independent UI experience.

But then we should have some way to update those list view webparts. There should be some relation between listviews and listview web parts.

In order to further explore on this area, I used some PowerShell scripts and SharePoint Manager 2010.

I executed the below powershell script to identify all the listviews associated with MYList

 #Get Site Url (http://MyTestSite) 
 $web = Get-SPWeb http://MyTestSite 
 #Get List instance
 $list = $web.Lists["MyList"] 
 #Get the collection of views
 $viewlist = $list.Views
 $viewlist | select Title,id

When I executed the above code, I got the output something like the following:


And Then I used SharePoint Manager 2010 to have a glimpse on the list and list views.

Both the results are really very interesting. One of the list view is not having a title. Gotcha !!

So, what it concludes is, list view webparts are actually nothing but hidden list views, they do not have a "Title" and the hidden property for these listviews are set to true.

Now we are all set to add the the testcolumn, in the webparts. We can use following powershell code to achieve this....

 #Get Site Url (http://MyTestSite) 
 $web = Get-SPWeb http://MyTestSite 
 #Get List Name
 $list = $web.Lists["MyList"]
 #Get the field to be added
 $spField = $List.Fields["testcolumn"] 
 #Get the collection of views
 $viewlist = $list.Views

 #Loop through each view to add the desired field
 for($i = 0; $i -lt $viewlist.Count; $i++)
 $viewnam = $viewlist[$i]
 $spView = $viewnam 
 Write-Host $spView,': ',$spView.ID


But, what if I want to update any specific list webpart on a page. Well If there is only one list view webpart, it is still easy to do it on the fly. But, if we have to do it in 1 or 2 pages, why waste time on writing code, should do it manually, shouldn't we ;)