Monday 8 October 2012

Provision web part page with web part through module in SharePoint 2010

Steps
1- Create an Empty SharePoint (2010) project and name it “WebpartProvision” as below.





2-Chose the option as Deploy as farm solution in next step as below





3-Add a Visualwebpart and name it as HelloWorld as below.

















4-In the Source View of  HelloWorld Webpart  write the below code as shown in image
<asp:Label ID="lblTest" runat="server" Text="Helloworld..."></asp:Label>





























5-Add a module to the solution and name it as TestWebpartPage as below.




























6-Rename the sample.txt file in the module to “TestWebpartPage.aspx”, remove all default contents and add the following lines of code to it.


<%-- _lcid="1033" _version="14.0.4762" _dal="1" --%>
<%-- _LocalBinding --%>
<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c"  %>
<%@ 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" %>
<%@ 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" %>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
       <SharePoint:ListItemProperty Property="BaseName" maxlength="40" runat="server"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
       <WebPartPages:WebPartZone runat="server" title="loc:TitleBar" id="TitleBar" AllowLayoutChange="false" AllowPersonalization="false"><ZoneTemplate>
       <WebPartPages:TitleBarWebPart runat="server" AllowEdit="True" AllowConnect="True" ConnectionID="00000000-0000-0000-0000-000000000000" Title="Web Part Page Title Bar" IsIncluded="True" Dir="Default" IsVisible="True" AllowMinimize="False" ExportControlledProperties="True" ZoneID="TitleBar" ID="g_1fd4faac_5930_46ea_b1ae_9ea28d5e746d" HeaderTitle="Home" AllowClose="False" FrameState="Normal" ExportMode="All" AllowRemove="False" AllowHide="True" SuppressWebPartChrome="False" DetailLink="" ChromeType="None" HelpLink="" MissingAssembly="Cannot import this Web Part." PartImageSmall="" HelpMode="Modeless" FrameType="None" AllowZoneChange="True" PartOrder="2" Description="" PartImageLarge="" IsIncludedFilter="" __MarkupType="vsattributemarkup" __WebPartId="{1FD4FAAC-5930-46EA-B1AE-9EA28D5E746D}" WebPart="true" Height="" Width=""></WebPartPages:TitleBarWebPart>
</ZoneTemplate></WebPartPages:WebPartZone>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
       <style type="text/css">
       Div.ms-titleareaframe {
       height: 100%;
       }
       .ms-pagetitleareaframe table {
       background: none;
       }
  </style>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
       <meta name="GENERATOR" content="Microsoft SharePoint" />
       <meta name="ProgId" content="SharePoint.WebPartPage.Document" />
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <meta name="CollaborationServer" content="SharePoint Team Web Site" />
       <script type="text/javascript">
// <![CDATA[
       var navBarHelpOverrideKey = "WSSEndUser";
// ]]>
       </script>
       <SharePoint:UIVersionedContent ID="WebPartPageHideQLStyles" UIVersion="4" runat="server">
              <ContentTemplate>
<style type="text/css">
body #s4-leftpanel {
       display:none;
}
.s4-ca {
       margin-left:0px;
}
</style>
              </ContentTemplate>
       </SharePoint:UIVersionedContent>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderSearchArea" runat="server">
       <SharePoint:DelegateControl runat="server"
              ControlId="SmallSearchInputBox"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderLeftActions" runat="server">
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageDescription" runat="server">
       <SharePoint:ProjectProperty Property="Description" runat="server"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderBodyRightMargin" runat="server">
       <div height="100%" class="ms-pagemargin"><img src="/_layouts/images/blank.gif" width="10" height="1" alt="" /></div>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderNavSpacer" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
              <table cellpadding="4" cellspacing="0" border="0" width="100%">
                           <tr>
                                  <td id="_invisibleIfEmpty" name="_invisibleIfEmpty" valign="top" width="100%">                    
                        <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="MiddleWebPartZone" Title="loc:MiddleWebPartZone" />                      
                    </td>                             
                           </tr>               
                           <script type="text/javascript" language="javascript">if(typeof(MSOLayout_MakeInvisibleIfEmpty) == "function") {MSOLayout_MakeInvisibleIfEmpty();}</script>
              </table>
</asp:Content>



7-In the Elements.xml file of the module TestWebpartPage add a tag  AllUsersWebPart” inside the file tag and add the below lines of code to it.




<AllUsersWebPart ID="HelloWorldWebpart" WebPartZoneID="MiddleWebPartZone" WebPartOrder="1">
        <![CDATA[
                           <webParts>
                                  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
                      <metaData>
                             <type name="WebpartProvision.HelloWorld.HelloWorld, $SharePoint.Project.AssemblyFullName$" />
                             <importErrorMessage>ImportErrorMessage;</importErrorMessage>
                      </metaData>
                      <data>
                             <properties>                                                
                                   <property name="Title"
                                                                      type="string">HelloWorldWebpart</property>
                                   <property name="Description"
                                                                      type="string">HelloWorldWebpartDescription</property>
                                   <property name="TitleIconImageUrl"
                                                                      type="string">WebPartTitleIconImageUrl</property>
                                   <property name="CatalogIconImageUrl"
                                                                      type="string">WebPartCatalogIconImageUrl</property>
                             </properties>
                      </data>
               </webPart>
                           </webParts>
                           ]]>
      </AllUsersWebPart>




8- The Elements.xml file should look as below.


























9-Now deploy the solution and browse the page
http://Host:Port/testwebpartpage/testwebpartpage.aspx

No comments:

Post a Comment