Monday, October 10, 2011

SharePoint Using Custom Page with visual web part as Default View

Download Sample Project


We may encounter a situation where we want a custom view to show list and another web part on same page.  To achieve this on custom list definition created using visual studio, we need to follow the instructions listed below.

Create new "Empty SharePoint Project". Use trust level as "Deploy as Farm Solution".

Add new "List Definition" named "CustomListDefinition". Choose type of List Definition as "Custom List" or whatever you require.

Add new mapped folder "Template\Layouts"

Add new folder in Layouts folder named "CustomDefaultView". Add new Application page named "CustomListForm.aspx" inside this folder.

Add new Visual web part named "MyVisualWebpart". Design your visual web part as required.

Now edit "CustomListForm.aspx". Remove "AutoEventWireup" and "CodeBehind" attributes from Page directive. In code behind file inherit your page from "WebPartPage" instead of LayoupPageBase. 

Register "Microsoft.SharePoint.WebPartPages" namespace as 


<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


Register your visual part inside webpage as 

<%@ Register Tagprefix="MyWebParts" Namespace="CustomDefaultView.MyVisualWebpart" Assembly="$SharePoint.Project.AssemblyFullName$" %>

Inside PlaceHolderMain add this code


<MyWebParts:MyVisualWebpart runat="server" ID="SearchIndividualsWebPart" >

        </MyWebParts:MyVisualWebpart>

Add web part zone below the above line

 <WebPartPages:WebPartZone runat="server" FrameType="None" ID="Main" Title="loc:Main" />

Open "Schema.xml" file under "CustomListDefinition". Find View Element  as highlighted in below image

now replace SetupPath="pages\viewpage.aspx"  with 
SetupPath= "layouts\CustomDefaultView\CustomDefaultView.aspx".

We replaced "pages\viewpage.aspx" this with our custom layout page. So now the when list default view is created SharePoint will use our custom layout page.

Now we need to register our custom aspx page in web.config as safe control. For this create "Empty Element". Select empty element and press F4 to open properties window. In Safe Control Entries property dialog create your safe entry. For more information on this visit Safe Control Entries in Confog File.

Build your project and deploy. Now create new List from your custom definition. You will see visual web part over the list. 

1 comment:

  1. This article gave me an insight into SharePoint from scratch.Its Simple to Understand and complete.I Enjoyed learning and thanks for sharing with us. Check out this link too its also helped me to complete my task. Its having a wonderful explanation on create and deploy webparts in sharepoint.

    http://mindstick.com/Articles/9e1d1fe1-7e51-4d01-86ec-f1f679631a7e/?Create%20and%20Deploy%20Visual%20WebPart%20in%20SharePoint%202010

    Thanks

    ReplyDelete

Popular Posts