Home > SharePoint, SharePoint 2010 > SharePoint – Deploy webpart page using module

SharePoint – Deploy webpart page using module

In this article I am going to explain 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.

wp1

The next step is to create a visual webpart, we will be embedding this into our webpart page

wp2

For demonstration, I added a label control with text property set to “Cool Webpart”

wp3

Now add a new module to the project

wp4

This will create two files into the solution

wp5

Delete the sample text file. Add an aspx page.

wp6

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.

wp7

Click on the link will open up the page with webpart

wp8

Advertisements
  1. May 14, 2013 at 5:13 AM

    If you can not export the web part because it is out of box like document library how do you get the values between <![CDATA[
    ?????????????

    • A.m.a.L Hashim
      May 15, 2013 at 10:03 AM

      Use SharePoint designer and open the page.

    • A.m.a.L Hashim
      June 17, 2013 at 10:23 AM

      You can the page in SharePoint Designer and get the data.

  2. October 12, 2013 at 1:02 AM

    I’m getting an error that the masterpage does not exist. I just want to use the default. It works when the aspx page is under the _Layouts folder and it uses DynamicMasterPageFile but not when it’s in the module and uses MasterPageFile

  3. August 2, 2014 at 12:31 AM

    To my knowledge, this doesn’t work in SP 2013 (as a sandboxed solution) because of the presence of the code behind and the lack of WebPartPage. Does anyone know differently?

  4. Madhavi
    September 5, 2014 at 2:36 PM

    I am was able to create page with webpart but unable to edit that webpart

  1. No trackbacks yet.

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: