Sunday, September 13, 2009

Add Web Part inside a Master Page in MOSS 2007

Imagine that you are designing a master page for your MOSS site and you have a requirement to have a functionality that is not possible out of the box. You brain storm on it and concludes that only custom web part can solve your problem.
But now your concern is how you can make this custom web part as part of your master page?
It’s easy! Just follow the steps given below to add a web part inside a master page.

1. Deploy the web part in your web application as you do normally.
2. Check whether the web part is showing up in the web part gallery.
3. Create a web part page by navigating to the site settings, create web part page.
4. Add your custom web part on the web part page.
5. Start SharePoint designer and open the newly created web part page.
6. You will now find two entries corresponding to the web part you added on the page
i. In the page declaration you will find a entry which looks something like this:


<%@ Register TagPrefix="WpNs0" Namespace="MyNamespace.MyClass" Assembly=" MyNamespace.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"%>

ii. Click on the web part section in the page and you will see an entry like this:


<WpNs0:WebPartName runat="server" ID="g_34cdbb90_38c6_4561_9182_8fbc0d2423a6" ExportMode="All" Title="WebPartTitle" __MarkupType ……………..

7. Copy these entries in separate notepad, and close the page opened in the SharePoint designer.
8. Now open the master page in SharePoint designer and add the first entry in the declaration section and second entry at the place where you want to add the web part to.
9. Save the page, check in and publish it and that’s it. Your job is done.


Do let us know in case you are facing any issue following the steps mentioned.
You can email us at sharepointkings@gmail.com

12 comments:

Unknown said...

Interesting Series on surveys. I have a question. How would I modify the Graphical Summary view to show only movie surveys for a certain department? In my case, it is by client, where the client name is provided by survey respondent.

Sebas said...

Great Information! thanks...

SharePoint Kings said...

@ROX Please ask the question on survey article so that other user will also share ideas with you as you requirement is particular.


@biosAward: Thanks

jag said...

Hi , Thanks a lot. It worked really well for me.

Is this the only way we can add a webpart to master page. By adding this way the webpart will become static. Also i have to add whole webpart section into wepart, which makes my masterpage big in number of lines whatever.

Just in case if we have a better way? Thanks again

_gil said...

Hi,

we made add the webpart and it worked great.

But, when we open any page on design view with share point designer it gives us a Master Page error. And if we remove the webpart it works fine, is there any way to fix this or to add a webpart on all pagesnot in the master page?

Thanks

SharePoint Kings said...

_gil,

Its default behavior because your webpart execute some code which need sharepoint environment so its not work that way.

Hope new office (2010) may resolve this problem :)

Buddhika Malwana said...

Hi,

Thank you very much for this post. It worked fine for me also.

Buddhika

Bhavini said...

Thanks for this post it was very clearly written.

However I already have pages that are linked to the master but when I add the webpart in the default.master the changes are not replicated throughout the other pages...are you able to help?

Thanks :)

SharePoint Kings said...

Bhavini,

it looks like you had not add webpart in different masterpage.


check which is your current masterpage and add webpart in that master page.

simplest way is check view source and check your webpart text appears or not:)

Bhavini said...

This is the url it is referring to, the master page is sitting within _catalogs/masterpage/default.master

would the following be the correct reference:

<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>

?

SharePoint Kings said...

bhavini,

you can check by opening that page sharepoint designer.

karunakar Solmon Raju said...

good artical,
I have aquestion.
default.master doesn't contains webpart zone. simply copying two entries will allow sharepoint.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com