Friday, September 21, 2012

Add Custom Web Part to Page Layout in SharePoint 2010


In this article we will discuss how you can add web your custom web part to sharepoint page layout. There might be different ways to add custom web part to page layout in SharePoint 2010, but here we will discuss how we can add by modifying the onet.xml file.

First of all after deploying your custom web part to the SharePoint web site, Please visit this link to get the 4 part assembly name of the web part which is very much important.

Once you got the 4 part assembly name, Now go to the appropriate XML folder to modify the onet.xml file which in inside the SiteTemplates directory. Here I am going to add my custom web part to a publishing web site. So my onet.xml is located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\SiteTemplates\BLANKINTERNET\XML (Please change according to your site template).

In the next step take a back up of the onet.xml file, before modifying anything.

Now open the onet.xml file in Visual studio (Visible clear even you can open in notepad also) and search for <Modules> tag. Then check for <Module tag. Rememner we have mo add web part inside <Module> tag.

Check for the module name, means usually the module name has the link with page layout name (correct me if I am wrong). So if your page is using Home page layout means you have to add in the module whose name is "Home".

If you want to add more than one web parts then you have to add one by one in the onet.xml file inside the module tag.
So inside <file tag add the below webpart add code below the <property> tag.

<AllUsersWebPart WebPartZoneID="MyRightZone" WebPartOrder="1">
          <![CDATA[
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <assembly>TestVisualWebPartForLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a565f7cce9e306f5</assembly>
      <type name="TestVisualWebPartForLayout.VisualWebPart1.VisualWebPart1, TestVisualWebPartForLayout, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a565f7cce9e306f5" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="ExportMode" type="exportmode">All</property>
        <property name="HelpUrl" type="string" />
        <property name="Hidden" type="bool">False</property>
        <property name="TitleUrl" type="string" />
        <property name="AllowConnect" type="bool">True</property>
        <property name="Description" type="string">My WebPart</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="AllowMinimize" type="bool">True</property>
        <property name="Title" type="string">MyHelloWorld</property>
        <property name="ChromeType" type="chrometype">Default</property>
        <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>
        <property name="Width" type="string" />
        <property name="Height" type="string" />
        <property name="HelpMode" type="helpmode">Modeless</property>
        <property name="CatalogIconImageUrl" type="string" />
        <property name="AllowEdit" type="bool">True</property>
        <property name="TitleIconImageUrl" type="string" />
        <property name="Direction" type="direction">NotSet</property>
        <property name="AllowClose" type="bool">True</property>
        <property name="ChromeState" type="chromestate">Normal</property>
      </properties>
    </data>
  </webPart>
  </webParts>
]]>
        </AllUsersWebPart>

As described above carefully give the assembly name adn type name. Also you can set the web part properties inside the <properties> tag.  Also another mail properties is WebPartZoneID, you have to give the zoneid which is present in the layout.

Once you will modify this save the onet.xml file and do an IISRESET which is mandatory to take into effect.

No comments:

Post a Comment