How to create custom view pages with a custom web part for the list definition - SharePoint WSS 3.0 / MOSS 2007
The view pages for the list are created based on the “ViewPage.aspx” in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\Pages. The ViewPage.aspx inserts the DataFormWebpart to display the items in the View. Now What if you have a custom web part in which you want to display the items in the view and wants to insert the custom web part to the view page of all the view created for the list.
Following are the steps to achieve this requirement. So…. Get ready to bend the SharePoint without bending the rules J
1. Copy the OOB “ViewPage.aspx” and place it in the same folder as “CustomViewPage.aspx”
2. Create the custom web part which shows the items of the view with your custom approach.
3. Create a feature to provision the “CustomViewpage.aspx” along with your custom web part.
4. Create a custom list definition. In the schema.xml file of the definition for all the views mention the “SetupPath” to refer your “CustomViewPage.aspx”. With this step all the views mentioned in the list definition will use the “CustomViewPage.aspx” to create the view pages and as well all the views which you create after the creation of the list will use the “customViewPage.aspx” as the base page to create the view pages.
Step 1:
Copy the OOB “ViewPage.aspx” and place it in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\Pages folder as “CustomViewPage.aspx”
Step 2:
Create the custom web part which shows the items of the view with your custom approach. Basically use the GetViewfromUrl(“Lists/<Listname>/<View_Name.aspx>”) method to get the view and then retrieve the items in the web part. The code snippet for the custom web part looks like follows :
SPList list = SPContext.Current.List;
String strViewName = <get the current view name from the URL>
SPView view = SPContext.Current.Web.GetViewFromUrl("Lists/list/ "+strViewName);
SPListItemCollection listItems = list.GetItems(view);
.
.
.
.
After getting the list items then do your custom approach of displaying the items.
Build the web part DLL and place it in the GAC.
Step 3:
Create a feature to provision the “CustomViewPage.aspx” along with your custom web part. By activating this feature the “Customviewpage.aspx” will be embedded with your custom web part. The elements manifest file content looks like follows :
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Module Name="CustomViewPage" Url="" Path="">
<File Url="Pages/CustomViewPage.aspx" Type="Ghostable">
<AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1">
<![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Assembly>CustomWebpartforView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e3483ddc8b0d437a</Assembly>
<TypeName> CustomWebpartforView.MyCustomview</TypeName>
<FrameType>None</FrameType>
<IsVisible>true</IsVisible>
<Title>Custom Web part for View</Title>
</WebPart>
]]>
</AllUsersWebPart>
</File>
</Module>
</Elements>
Step 4:
Now the base custom view page is available along with the web part. Now how to integrate the custom view page with the list definition. Create a custom List definition and for all the views defined in the schema mention the “SetupPath” to use the “CustomViewPage.aspx”. Now the custom view page will be used as the base view page for all the views defined in the list definition and as well the views created later also will be using the “CustomViewpage.aspx” as the base page. The sample snippet for declaring the views inside the schema.xml is as follows :
<View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,All_Tasks;" DefaultView="TRUE" MobileView="True" MobileDefaultView="False" SetupPath="pages\Customviewpage.aspx" ImageUrl="/_layouts/images/issues.png" Url="AllItems.aspx"><!-- _locID@DisplayName="camlidT8" _locComment=" " -->
Install and activate the feature of your custom list definition.
Now with all these steps, when you create a list based on this custom list definition then all the views created will be using the “CustomViewPage.aspx” to create the view pages and your custom web part will be rendered instead of the OOB dataformwebpart. The views which you will be creating after the creation of the List (Through UI, Through Object Model) will also use the “CustomViewpage.aspx” as the base page to create view pages.
Comments
Anonymous
April 20, 2009
How will you apply this template for the existing pages. I created a new template and need to apply the same for the existing pages. Your sample works fine if I create a new discussion list after the change in the schema.xml. Help will be highly appreciated.Anonymous
July 20, 2010
Hi! When I try to activate the feature with the step3 element manifest file, I get an error: cannot extract Pages/CustomViewPage.aspx from module CustomViewPage. What Url do I have to give to Module?Anonymous
August 18, 2010
Has anyone had any luck with this? I cannot get it to work. More specifically:
- When I activate the feature using the element.xml given ablove, sharePoint complains that the path does not exist. That is, the Pages path is not available to the feature on activcation. Am I missing something here?
- Anonymous
September 24, 2013
This won't work, people. Bogus post,