Monday, May 5, 2014

How to add web part to master page in SharePoint 2013

I have a question, Can we add web part to master page??
Generally we can add web parts in web part zones, But master page doesn't have web part zone.
So can we add web part to master page?


"A Web Part is a server control that provides a specific piece of SharePoint functionality, and a Web Part zone is a container that determines the layout, behavior, and other properties of the Web Parts contained in that zone."

Web Part zones are available on page layouts but not on master pages—the purpose of zones is to allow authors to modify Web Parts, and authors typically don't edit a master page.

This means we can not modify the web parts which are added in master page but we can add web part to master page.

Here my requirement, I have one custom web part, which displays date and time. Now i want to add this web part to master page.

Step 1:

Activate SharePoint publishing infrastructure feature under site collection feature.
Activate SharePoint publishing feature under manage site feature.

Step 2:

Open SharePoint site in SharePoint designer 2013 and check out master page's html page.

Now open SharePoint site in IE, Click on site actions  --> Design Manager


Step 3:

In design manager page, click on Edit Master Pages. It will show master pages, click on your master page which was checked out in previous step.




Step 4:

Click on "Snippets" in master page preview, it will open snippet gallery.


Step 5:

In Snippet Gallery, Click on Other Web parts and select your custom web part or else you can select OTB web part also. Once you select your web part, you can see code under HTML Snippet.


Copy the snippet by clicking on "Copy to clipboard".


Step 6:

I want to show this web part between site title and search box control.

Now open your master page in SharePoint designer 2013 and find "<div class="ms-tableCell ms-verticalAlignTop">
<div id="searchInputBox">" div tag.

Now add one more div tag before these two div tags.

<div class="ms-tableCell ms-verticalAlignTop"> </div>


Then page HTML snippet in this div tag.

<div class="ms-tableCell ms-verticalAlignTop">
<!--CS: Start Clock Snippet-->
<!--SPM:<%@Register Tagprefix="ac401823a"  Namespace="ClockWebPart.VisualWebPart1" Assembly="ClockWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9aa41227deb2659" %>-->
<!--MS:<ac401823a:ClockWebpart runat="server" LeftMargin="5" TopMargin="5" Description="Local clock using jQuery" ImportErrorMessage="&#60;%$Resources:core,ImportErrorMessage;%&#62;" Title="Clock" __MarkupType="vsattributemarkup" __WebPartId="{a95b03f7-6800-4eba-8457-99d8d4f6ebeb}" WebPart="true" IsClosed="false" ID="g_a95b03f7_6800_4eba_8457_99d8d4f6ebeb">-->
<!--PS: Start of READ-ONLY PREVIEW (do not modify)-->
<div id="ctl00_g_a95b03f7_6800_4eba_8457_99d8d4f6ebeb" WebPart="true" __WebPartId="{a95b03f7-6800-4eba-8457-99d8d4f6ebeb}" __MarkupType="vsattributemarkup">
</div>
<!--PE: End of READ-ONLY PREVIEW-->
<!--ME:</ac401823a:ClockWebpart>-->
<!--CE: End Clock Snippet-->
</div>


Now save your master page and click on "Preview in browser".


Step 7:

Check in the master page and publish major version then every user can able to see the changes in master page.


Thanks for snippet...  :).

7 comments: