Set the Title of a ListViewWebPart

Something that has bothered me repeatedly in the past is that ListViewWebParts didn’t seem to allow their titles to be set via CAML. The View element for a site (i.e. in the ONET.xml file) doesn’t have a title element, and the ‘Name’ and ‘DisplayName’ elements don’t set the web part’s title. Instead, the ListViewWebPart always seemed to pick up the name of the list it referred to. This was a problem if you had a page that showed two ListViewWebParts refering to the same list. E.g.:

<View List="Lists/Tasks" BaseViewID="7" WebPartZoneID="Left" WebPartOrder="5" />
<View List="Lists/Tasks" BaseViewID="9" WebPartZoneID="Left" WebPartOrder="6" />

would result in:

How can we set those titles to be different?

With other web parts, it wouldn’t be a problem. When adding them to a page in the ONET.xml file, you specify what the webpart’s assembly, class and properties all are:

<AllUsersWebPart List="301" WebPartZoneID="BlogNavigator" WebPartOrder="2">
 <![CDATA[
 <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
 <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
 <TypeName>Microsoft.SharePoint.WebPartPages.BlogMonthQuickLaunch</TypeName>
 <Title>My Title Here</Title>
 </WebPart>
 ]]>
</AllUsersWebPart>

Great. But nothing in the CAML documentation says that you can do this with a <View> element, just within <AllUsersWebPart> elements. But what if you can? I did a regex search over 14 Hive, and found the following example:

<View List="303" BaseViewID="0" WebPartZoneID="BlogNavigator" WebPartOrder="1">
 <![CDATA[
 <webParts>
 <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
 <metaData>
 <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" />
 <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
 </metaData>
 <data>
 <properties>
 <property name="AllowConnect" type="bool">True</property>
 <property name="ChromeType" type="chrometype">None</property>
 </properties>
 </data>
 </webPart>
 </webParts>
 ]]>
</View>

Ooo – interesting. This isn’t quite what I need – it’s using the XsltListViewWebPart, and unfortunately I’m dealing with an old SharePoint 2007-type list. I wondered, though – would the same approach work with the ListViewWebPart?

Well, a quick search turned up the answer – a definite yes – and I wish I’d known about this 3 years ago. The following form of web part declaration:

<View List="Lists/Tasks" BaseViewID="7" WebPartZoneID="Left" WebPartOrder="5">
 <![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>Enquiry Tasks</Title>
 </WebPart>
 ]]>
</View>
<View List="Lists/Tasks" BaseViewID="9" WebPartZoneID="Left" WebPartOrder="6">
 <![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>Request Specific Tasks</Title>
 </WebPart>
 ]]>
</View>

results in …

Perfect! Problem solved, and now I can control all sorts of other settings (such as Chrome, or allowing close) for these web parts too!

EDIT: I just found another example, this time on MSDN. It’s even more minimal than the one I found in 14 – it doesn’t define an assembly or class. However, it does not work. I tested it, and got the error:

The <Assembly> element is required. Add this element to the Web Part file (.dwp), and then try to import the Web Part.

That’s a pretty clear error. So, do not follow that article, but use the fuller forms shown above.

EDIT 2: I also note that the properties in my old-fashioned 2007 ListViewWebPart aren’t the same as in the 2010 XsltListViewWebPart – Sandeep lists some of the properties available here.

Advertisements
Set the Title of a ListViewWebPart

5 thoughts on “Set the Title of a ListViewWebPart

  1. Hello Andy,

    I’m Nick from MAPILab ( http://www.mapilab.com ) – the vendor of software solutions for Microsoft SharePoint.

    We are looking for SharePoint evangelists who are also involving in blogging or who are author or contributor at SharePoint-related web-sites.

    We have released a new workflow solution: HarePoint Workflow Extensions for SharePoint 2010:
    http://www.harepoint.com/Products/HarePointWorkflowExtensions/Default.aspx

    Our purpose is creation of reviews for this new solution and their evaluation from professional side.

    Please, note that we don’t looking for any king of advertising or ‘paid’ review – we just interested to offer you the evaluation of the solution as a subject for your posts/articles. Of course we would like to place links to the best reviews from the product homepage.

    You are welcome to contact me back with any additional questions.

    Yours faithfully,
    Nick Kharchenko
    Marketing Dept.

  2. Hello Andy!

    Thanks for your post. It helped me very much. One thing is only: Could you remove anchor-tags in your code samples

    Thank you!

  3. Pieter Veenstra says:

    I tried this however the Add Item links seem to disappear when you set the title of the web part this way.

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s