SharePointAds TextOnly

Wednesday 8 May 2013

SharePoint 2010 : Deploy webpart page embedded with Visual webpart using SharePoint feature

Are you looking for a solution to deploy webpart pages using SharePoint feature? Do you need to use Visual webparts part in to the page? Following is the solution for your answers. Here I am going to explain to how to deploy webpart pages with visual webpart using module feature.

Create new SharePoint project, select "Empty SharePoint Project" template


On next window, provide local SharePoint site url and select "Deploy as a farm solution" as a trust level. Click on Finish button
The next step is to create Visual Webpart, right click on project and add new item

Add sample label control in webpart's ascx for demonstration, here you can design your webpart

Now add new module feature in the project. Right click on project and add new item


Now delete sample.txt file from module and add aspx page. Right click on module and add new item
Select Text file template from General section and name it as MyPage.aspx.

Same as above, add new class file for aspx page

Solution Explorer window will be like this

Open MyPage.ASPX page and paste following lines, registered SharePoint required assemblies and Project assemblies

<%@ 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="asp" Namespace="System.Web.UI"
Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>
<%@ 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="MyWebPartPages.MyPage" masterpagefile="~masterurl/default.master" %>

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



Then open MyPage.aspx.cs class file and paste following lines

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;

namespace MyWebPartPages
{
    public partial class MyPage : WebPartPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

Make sure that page inherits from WebPartPage

Next step is to embed visual webpart with the webpart page. For that open Module's Elements.xml file and update it as follow
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Module Name="MyWebpartPage" Url="SitePages">
  <File Path="MyWebpartPage\MyPage.aspx" Url="MyPage.aspx"

        Type="GhostableInLibrary" IgnoreIfAlreadyExists="FALSE">
      <AllUsersWebPart WebPartOrder ="0" WebPartZoneID ="myWebpartZone">
        <![CDATA[
          <webParts>
            <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
              <metaData>
                <type name="MyWebPartPages.MyVisualWebPart.MyVisualWebPart, $SharePoint.Project.AssemblyFullName$" />
                <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
              </metaData>
              <data>
                <properties>
                  <property name="Title" type="string">MyVisualWebPart</property>
                  <property name="Description" type="string">MyVisualWebPart</property>
                     
                </properties>
              </data>
            </webPart>
          </webParts>
        ]]>
      </AllUsersWebPart>
    </File>
  </Module>
</Elements>


Build and deploy the solution and open "Site Pages"  library.


Click on MyPage file and you will see the output webpart page



1 comment:

  1. It’s very easy to find out any matter on web as compared to books, as I fount this article at this web site.

    Podiatry Websites and Design Exclusive to Your Area. Websites come with 100% custom design.

    ReplyDelete