1

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?

6 Answers 6

4

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.

6
  • +1 But only for "the put new <ContentPlaceHolder> in master page & <Content> in Page layout" part Jun 6, 2012 at 17:51
  • thanx. Why not for webpart as user control? Jun 6, 2012 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 Jun 7, 2012 at 3:29
  • What if the page layout does not have teh contentplaceholderid? Will the master page blow up?
    – user8344
    Jun 7, 2012 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. Jun 8, 2012 at 12:45
0

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.

0

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!

0

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.

1
  • 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. Jun 6, 2012 at 5:24
0
  • First write your logic and deploy a WebPart to a SharePoint site.
  • Add a New WebPart and add the custom WebPart you have developed.
  • Now open the page within a SharePoint Designer.
  • Now Open a Notepad.
  • Now you have to copy the two things.

    • Copy the Register tag within the Page. This will be in the format as shown below.

      <%@ Register TagPrefix="WpNs0" Namespace="GroupUsersGrid.GroupUsersGrid" Assembly="GroupUsersGrid, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b5c7b45489124c1b"%>
      
    • Now copy the WebPart Code Generated by SharePoint to your Notepad. This will be in the format as shown below.

      <
      
1
  • This doesn't seem to answer OP's question. Can you include more details? Feb 13, 2015 at 13:24
-2

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

2
  • He want to add a web part to the master page not the web part page. Jun 6, 2012 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, 2012 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.