Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like to add a Web Part to part of a Master Page, but only in the case of the home page of the site. Otherwise it is not needed.

enter image description here

How can I handle this? (see attached image) Do I have to create multiple master pages to accomodate this, or can I create a special Page Layout, etc. Do I have options?

share|improve this question
add comment

5 Answers

up vote 4 down vote accepted

In the head section of the master page you need to define a tag prefix in the style of:

<%@ Register Tagprefix="Yourwebpart" Namespace="yournamespace" Assembly="yourassemblyname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Also don't forget to change the PublicKeyToken and version number.

In the location where you want to place the webpart all you need to do insert:

<Youwebpart:YourClassNameOfTheWebPart id="mycustomwebpart" runat="server"/>

This is all because a web part is nothing more than a custom web control. If you have additional properties you can specify that too:

<Youwebpart:YourClassNameOfTheWebPart id="mycustomwebpart" YourPropertyName="PropertyValue" runat="server"/>


To add an image only to the start page you can also use a delegate control on the master page. For this you need to add to the master page a content place holder:

<asp:ContentPlaceHolder id="ShowOnlyMyImage" runat="server"/%gt;

The id "ShowOnlyMyImage" is only for reference you can change it to any id you like.

In the page layout you like to use for the home page you need define: <asp:Content ID="MyAdditionlImage" ContentPlaceHolderID="ShowOnlyMyImage" runat="server">

ContentPlaceHolderID and ID here are also used only for reference. The ContentPlaceHolderID must match the ID in the MasterPage. For the ID you can use any unique name.

share|improve this answer
    
+1 But only for "the put new <ContentPlaceHolder> in master page & <Content> in Page layout" part –  Per Jakobsen Jun 6 '12 at 17:51
    
thanx. Why not for webpart as user control? –  Stefan Bauer Jun 6 '12 at 23:01
    
First of a web part is not a User control, but an ordinary control. Second it should only go into the page layout not the masterpage. Third the important part of your answer is the ContentPlaceHolder and should be highlighted –  Per Jakobsen Jun 7 '12 at 3:29
    
What if the page layout does not have teh contentplaceholderid? Will the master page blow up? –  JesseC Jun 7 '12 at 21:11
    
If the placeholder cannot be found on the article page then nothing happens. The space in the master page will be empty. –  Stefan Bauer Jun 8 '12 at 12:45
add comment

you can edit your page then add a specific webpart to that page. I think so.

share|improve this answer
    
He want to add a web part to the master page not the web part page. –  Stefan Bauer Jun 6 '12 at 5:23
    
While this post may answer the question, or perhaps not, we prefer more details such as "why". Please edit your post with additional details. See answer for general guidelines. –  SPDoctor Jun 6 '12 at 7:39
add comment

One master.

Typically, the PageLayout of the home page is unique, and the "inside" pages have a different layout. Put your specific web part on the home page PageLayout.

share|improve this answer
    
The homepage is nothing more than: a) A asp.net web part page or b) Article page in case of content publishing on both there is nothing unique. If publishing is enabled a welcome page could be set by the user to any asp.net page inside sharepoint. The other option is to set the homepage with SharePoint Designer. –  Stefan Bauer Jun 6 '12 at 5:24
add comment

Multiple master pages is the best option, but you can add the following inside your Masterpage: (From top of my head, might need some tweaking)

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    // Some way to check if its the homepage, this is, again, from the top of my head
    if(Page.Request.Url.ToString().Contains("Default.aspx"))
    {
         var wpManager = SPContext.Current.Site.RootWeb.GetLimitedWebPartManager(Page.Request.Url.ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

         // Zone index is the index of the zone
         // MyCustomWP is the webpart you want to add. 
         // You might need to add the dll reference for that web part if it is in another project
         var zoneIndex = ZONEINDEX;
         wpManager.AddWebPart(new MyCustomWP(), null, zoneIndex);

    }
}
</script>

You can also run this code on feature activated, which I would say would be better approach. Again, you might need to tweak this code.

Thanks!

share|improve this answer
add comment

You are correct in that you will need to create a custom master page, and base your home page off of that master page. Of course, you'll have to include the markup for the web part directly in the new master page.

A page layout won't work here because it only applies to the main content area in the master page.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.