Skip to content
Amal Hashim
All posts
Article

SharePoint - Deploy a web part page with a web part using a module

Create a web part page and place a visual web part on it through a module's Elements.xml in Visual Studio 2010.

2 min read#SharePoint#SharePoint 2010#Visual Studio

You can deploy a web part page with a web part already on it, with no manual editing after the install. The trick is a module whose Elements.xml adds the web part to a zone on the page.

Steps

  1. Create an Empty SharePoint project in Visual Studio 2010.

New empty SharePoint project

  1. Add a visual web part. We will place it on the page later.

Adding a visual web part

  1. For the demo, drop a label on the web part and set its text to "Cool Webpart".

Label control with the text Cool Webpart

  1. Add a new module to the project.

Adding a module

  1. The module creates two files in the solution.

Files created by the module

  1. Delete the sample text file and add an aspx page.

New aspx page in the module

  1. Replace the page markup with the following. It defines one web part zone, wpzMyZone.
html
<%@ 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>
  1. In the code behind, make sure the page inherits from WebPartPage.
csharp
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)
        {
        }
    }
}
  1. Update the module's Elements.xml. The AllUsersWebPart element puts the web part into the zone.
xml
<?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>
  1. Deploy the solution. The new page appears in the SitePages library.

New web part page in the SitePages library

  1. Open the page. The web part is already in the zone.

Web part page showing the Cool Webpart label