How to create a webpart page and embed a webpart into it using elements xml file.
To start with, create an Empty SharePoint project in visual studio 2010.
The next step is to create a visual webpart, we will be embedding this into our webpart page
For demonstration, I added a label control with text property set to “Cool Webpart”
Now add a new module to the project
This will create two files into the solution
Delete the sample text file. Add an aspx page.
Now open the aspx page and paste following markup
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="WebpartPages.MyPage" masterpagefile="~masterurl/default.master" %> <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> <WebPartPages:WebPartZone runat="server" ID="wpzMyZone"> <ZoneTemplate></ZoneTemplate> </WebPartPages:WebPartZone> </asp:Content>
Open the code behind and ensure the page inherits WebPartPage
using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; namespace WebpartPages { public partial class MyPage : WebPartPage { protected void Page_Load(object sender, EventArgs e) { } } }
Now open up the elements xml file and update it as below
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="Webpartpage" Url="SitePages"> <File Path="Webpartpage\MyPage.aspx" Url="MyPage.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="FALSE"> <AllUsersWebPart WebPartOrder ="0" WebPartZoneID ="wpzMyZone"> <![CDATA[ <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <metaData> <type name="WebpartPages.MyWebpart.MyWebpart, WebpartPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=132122a1200767d5" /> <importErrorMessage>Cannot import this Web Part.</importErrorMessage> </metaData> <data> <properties> <property name="Title" type="string">MyWebpart</property> <property name="Description" type="string">Mywebpart</property> </properties> </data> </webPart> </webParts> ]]> </AllUsersWebPart> </File> </Module> </Elements>
Deploy the solution. Now open up the SitePages library, there you can see the new webpart page we just created.
Hope this helps...Happy Coding!!!!
No comments:
Post a Comment