Sunday, July 19, 2009

Setting listview webpart properties in CAML

Scenario:
List View web parts are different than other kind of Sharepoint web part in their CAML representation.

Other web parts uses AllUsersWebPart tag which allow easy access to all the properties, so setting properties is easy.

List View webparts uses View Tag to place them on the page. Now we wanted to set some
of it properties which has different values from its default settings.

Solution:
Its was not that difficult to that but finding this undocumented solution was the difficult part :-)

Below example shows how to set AllowZoneChange property to false by default.

Sample Code ( Elements.xml) :

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="NewGUID" xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Default" Url="" Path="">
<File Url="default.aspx" NavBarHome="True">
<View List="Lists/Announcements" BaseViewID="0" WebPartZoneID="Left" WebPartOrder="1" Type="HTML">
<![CDATA[
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart</TypeName>
<Title>Announcements</Title>
<AllowRemove>false</AllowRemove>
<AllowZoneChange>false</AllowZoneChange>
<AllowMinimize>false</AllowMinimize>
<AllowConnect>false</AllowConnect>
<AllowEdit>false</AllowEdit>
<AllowHide>false</AllowHide>
</WebPart>
]]>
</View>
</File>
</Module>
</Elements>

7 comments:

Sahil Sagar September 9, 2009 at 12:49 AM  

Where do you place this xml?

Sandeep September 9, 2009 at 12:58 AM  

Sahil , this file is used in conjunction with a feature xml file . You can specify the name of manifest file as Elements.xml

Mohamed Hachem November 9, 2009 at 10:25 AM  

Hello Sandeep
I agree with you list view web part is little bit different than other web parts
best way I find to use it, is by adding it dynamically through the code
manager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);
LVWP = new ListViewWebPart();
LVWP.AllowZoneChange = true;
LVWP.ExportMode = WebPartExportMode.All;

by adding export all you will be able to export it then use the xml
the way you want.


Sub Teeka

Sandeep November 9, 2009 at 1:06 PM  

Mohamed,
You are right.. but this is when you know which page webpart is there and how many webparts will be there... if you set it in CAML , users can use it on any page they want and as many instances they want.. +- side of both approaches..

Anyway, thanks for adding another dimension :)

Sandeep

Unknown August 10, 2011 at 8:03 AM  

Hello Sandeep,
How to show only the columns required in the list view through provisioning? Is it possible?

Sandeep August 10, 2011 at 8:07 AM  

Hi Praveen

This is possible.

Options 1. Create a view in the list first with all the required columns and set the BaseViewID accordingly.

Options 2. Via code you can find the listview WP on the page and open the View XML via reflection and update it.

By the way , Murali said hi to you :)

Sandeep

gautamsheth November 22, 2012 at 12:56 AM  

So how do i do the same in Sharepoint 2010 ?