SharePoint: Deploy Site Pages with Webpart with WSP

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=, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" 
Assembly="Microsoft.SharePoint, Version=, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" 
Assembly="System.Web.Extensions, Version=, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
Assembly="Microsoft.SharePoint, Version=, 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">

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="">
  <Module Name="Webpartpage" Url="SitePages">
    <File Path="Webpartpage\MyPage.aspx" Url="MyPage.aspx"  
          Type="GhostableInLibrary" IgnoreIfAlreadyExists="FALSE">
      <AllUsersWebPart WebPartOrder ="0" WebPartZoneID ="wpzMyZone">
            <webPart xmlns="">
                <type name="WebpartPages.MyWebpart.MyWebpart, WebpartPages, 
                Version=, Culture=neutral, PublicKeyToken=132122a1200767d5" />
                <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                  <property name="Title" type="string">MyWebpart</property>
                  <property name="Description" type="string">Mywebpart</property>

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!!!!

(Visited 59 times, 1 visits today)