In this post, I will show you how to deploy a Web Part Page with content added to the page, specifically a custom Visual Web Part. This method should work for other web parts, including out of the box SharePoint Web Parts. This post focuses on how to provision the Web Part on a page, it does not explain how to create the Web Part.

Here is a view of the Object Explorer window:

ProvisionWp01

Here is the code for MyWebPartPage.aspx:

<%@ 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=”MyWebPartPage.aspx.cs” Inherits=”DeployWebPartOnPage.MyWebPartPage.MyWebPartPage” MasterPageFile=”~masterurl/default.master” %>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>
<WebPartPages:WebPartZone runat=”server” ID=”wpzMyZone”>
<ZoneTemplate></ZoneTemplate>
</WebPartPages:WebPartZone>
</asp:Content>

Here is the code for MyWebPartPage.aspx.cs:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace DeployWebPartOnPage.MyWebPartPage
{
public partial class MyWebPartPage : WebPartPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}

Here is the code for Elements.xml:

    <?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/&#8221; >
<Module Name=”MyWebPartPage” Url=”SitePages”>
<File Path=”MyWebPartPage\MyWebPartPage.aspx” Url=”MyWebPartPage.aspx”
Type=”GhostableInLibrary” IgnoreIfAlreadyExists=”FALSE”>
<AllUsersWebPart WebPartOrder =”0″ WebPartZoneID =”wpzMyZone”>
<![CDATA[
<?xml version=”1.0″ encoding=”utf-8″?>
<webParts>
<webPart xmlns=”http://schemas.microsoft.com/WebPart/v3″&gt;
<metaData>
<type name=”DeployWebPartOnPage.HelloWorldWP.HelloWorldWP, DeployWebPartOnPage,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=00a467d3ecefd6d1″ />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name=”Title” type=”string”>HelloWorldWP</property>
<property name=”Description” type=”string”>HelloWorldWP Description</property>
</properties>
</data>
</webPart>
</webParts>
]]>
</AllUsersWebPart>
</File>
</Module>
</Elements>

The only issues that i ran into while deploying this are that i forgot to modify the feature to include the Web Part Page and i did not have the correct assembly info in Elements.xml. I was unable to add the .aspx file directly to the MyWebPartPage module so i had to drag and drop it where i wanted within the Object Explorer window. Then when i deployed i was getting a “cannot find/deploy web part” sort of a message. That is because i had not corrected the namespace value of the Web Part Page after the drag and drop.

Advertisements