0

I'm building a custom MasterPage and almost done. I deployed it to the final project and start testing.

Everything seems perfect except some pages. Like if I click on my name the drop down menu will drop down and I can choose "my settings"

Now the layout of this page is still using the default.master and not the one I build. the page name is userdisp.aspx and if you open it in an editor you will find this code:

<%@ Page Language="C#" DynamicMasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.ApplicationPages.UserDisplayPage"%>

As far as I know is I can build my own page and add it to the _layout folder. but then it will overwrite the original page which I don't want to do that, because if the user try to use another master page it will be a problem.

Does anyone have an idea on how to replace this page when the new masterpage is been deployed?

Thanks in advance

2 Answers 2

0

Sorry for the delay!

  1. Start by right clicking "Features" and add, You want the scope to be site (which means site collection level). This is because masterpages sit at the sc level. Theoretically you can put them anywhere and just reference them properly, but then some pages get all wonky.

  2. Add the SharePoint Folders; Layouts and Images (right click on the project and add "SharePoint Images" Folder etc. This is just for good measure if you want to package images with your solution and for handling upgrades to your solution. What happens is sharepoint will copy the contents of the wsp (like a cab file) into the Hive's locations and create a folder for your solution/feature.

  3. Right Click on the project again and add empty element (3x); masterpage, style, and themes. Each of these folders has an elements.xml file. It tells SharePoint what it needs to do to provision it from the solution file. We are going to use it to "copy" the contents of these folders to the right locations on the site. Should look like this

solution folder structure

Creating the files to go into the elements; masterpages, Style, themes

Masterpage

I use a blank file, named my.master into the above folder.Paste your code from sharepoint designer (because when you export from SPD and then try to Import there is markup that MS injects (or secretes hahah). (You already have your master done, so you could just copy this file into the masterpages element.

**TIP: you need to have a css scriptlink reference to get to your css if its going to live in a location other than your masterpage, which is good practice.

<SharePoint:CssRegistration ID="CssRegistration1" Name="<% $SPUrl:~sitecollection/Style Library/yourstylename/yourcssfilename.css%>" After="corev4.css" runat="server"/>

This tag needs to be in your Head element of your masterpage

Style

add a css file (anything else you want in here, some people put images etc), you can copy paste into here if you want.

Themes

Copy your .thmx files into this location. Tip; you can use microsoft powerpoint to create themes and export them.

If you did this all correctly, your elements.xml files will be automatically updated and you should see that it's added references to each file you have created in these folders.

Defining Elements.xml (modules)

Masterpage elements.xml

You have to specify the module's correct list id and url (116, and _catalogs/masterpage ). The element(s) is the file you want to load into the module's URL; specifically the file path is where the file is in your project, and the URL is where it ends up. You'll note the elements url is just the filename, technically you could change filenames here, or have multiple copies of the file)

It should look like this

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="masterpages" List="116" Url="_catalogs/masterpage">
    <File Path="masterpages\yourfilename.master" Url="yourmasterfilename.master" Type="GhostableInLibrary"/>
  </Module>
</Elements>

Style Elements.xml

Pretty much the same thing, except you change the destination to be a folder in the site collections style library so the module definition is different.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Style" Url="Style Library/MyStyle">
<File Path="Style\your.css" Url="your.css" Type="GhostableInLibrary" />
</Module>
</Elements>

*you might have more elements that just the css. depends what you pasted into the folder.

Themes Elements.xml

Same steps repeated, however these go into a the special _catalogs folder like the masterpages. Note, you may not have themes in your solution, but just showing this for future reference.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Themes" Url="_catalogs/Theme">
<File Path="Themes\Theme1.thmx" Url="Theme1.thmx" Type="GhostableInLibrary" />
<File Path="Themes\Theme2.thmx" Url="Theme2.thmx" Type="GhostableInLibrary" />
Type="GhostableInLibrary" />
</Module>
</Elements>

Updating your Feature

Now edit your feature, and you should be able to include these modules as items in the feature.

Lastly, you need the code on the feature event receiver like I promised :). This will enable and disable the feature, and in doing so, set and reset the custom,default master URLs for the site collection.

  1. Right click on the feature and add event receiver, you may already have one by default on your feature? (I think it depends on which VS version you are running)

There are two methods you need to code, activation (enabling a feature) and deactivation.

 public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.


    // Uncomment the method below to handle the event raised after a feature has been activated.
        const string defaultmasterPage = "yourmasterfilename.master";
        const string custommasterPage = "yourmasterfilename.master";
        const string searchMasterPage = "yourmasterfilename.master";
        const string themeName = "Theme1";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
           SPSite site = properties.Feature.Parent as SPSite;

            if (site != null)
            {
                using (SPWeb topLevelSite = site.RootWeb)
                {

                    //Get the relative path
                    string relativePath = topLevelSite.ServerRelativeUrl;

                    if (!relativePath.EndsWith("/"))
                    {
                        relativePath += "/";
                    }
                    //Get Theme collection from site and the them we want
                    ReadOnlyCollection<ThmxTheme> themes = ThmxTheme.GetManagedThemes(site.RootWeb.Site);
                    ThmxTheme customTheme = null;
                    foreach (ThmxTheme theme in themes)
                    {
                        if (theme.Name == themeName)
                        {
                            customTheme = theme;
                            break;
                        }
                    }

                    //Apply branding to each web in the site collection

                        //Apply masterpage and logo
                        if (site.RootWeb.WebTemplate == "SRCHCENTERLITE" || site.RootWeb.WebTemplate == "SRCHCEN" || site.RootWeb.WebTemplate == "SRCHCENTERFAST")
                        {
                            site.RootWeb.CustomMasterUrl = relativePath + "_catalogs/masterpage/" + searchMasterPage;
                        }
                        else
                        {
                            site.RootWeb.MasterUrl = relativePath + "_catalogs/masterpage/" + defaultmasterPage;
                            site.RootWeb.CustomMasterUrl = relativePath + "_catalogs/masterpage/" + custommasterPage;
                        }
                        site.RootWeb.AlternateCssUrl = "";
                        site.RootWeb.SiteLogoUrl = "";
                        site.RootWeb.UIVersion = 4;
                        site.RootWeb.Update();

                        //Also apply the theme
                        if (customTheme != null)
                        {
                            customTheme.ApplyTo(site.RootWeb, true);
                            site.RootWeb.Update();
                        }

                }
            }    

    }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
            SPSite site = properties.Feature.Parent as SPSite;

            if (site != null)
                {
                using (SPWeb topLevelSite = site.RootWeb)
                {
                    //Get the relative path
                    string relativePath = topLevelSite.ServerRelativeUrl;
                    if (!relativePath.EndsWith("/"))
                        {
                        relativePath += "/";
                        }
                    //Apply branding to each web the wen the site collection

                        //Apply default masterpage and logo
                        if (site.RootWeb.WebTemplate == "SRCHCENTERLITE" || site.RootWeb.WebTemplate == "SRCHCEN" || site.RootWeb.WebTemplate == "SRCHCENTERFAST")
                        {
                            site.RootWeb.CustomMasterUrl = relativePath + "_catalogs/masterpage/minimal.master";
                            }
                        else
                        {
                            site.RootWeb.MasterUrl = relativePath + "_catalogs/masterpage/v4.master";
                            site.RootWeb.CustomMasterUrl = relativePath + "_catalogs/masterpage/v4.master";
                            }
                        site.RootWeb.AlternateCssUrl = "";
                        site.RootWeb.SiteLogoUrl = "";
                        site.RootWeb.Update();
                        //reset the theme back to default
                        ThmxTheme.SetThemeUrlForWeb(site.RootWeb, null, true);
                        site.RootWeb.Update();

                }
            }
        }

thats it!

1
  • Wow I just can't thank you enough for your help and time. I really appreciate this. Thank you so much Stefan.
    – R.Karar
    Jan 20, 2015 at 12:38
0

the ~masterurl/default.master means it will pull from whatever the site collection has set to the "default.master". You can check in SharePoint Designer to see which masterpages are set to both Custom Master and Default Master.

Some Pages like the settings.aspx pages will use a different master as well. The only way I was able to get my branded masters to look and behave the way I wanted was through CSS. (as you probably are already aware SharePoint has some contentplaceholders which MUST be present in the master).

For your master page did you start with an existing .master?

4
  • Hi Stefan, Thank you for your comment. Yes I'm using the v4 masterpage and I build mine on top. If I only can use my masterpage on all pages with out playing with the default.master like I said, if the user want to switch back or user another masterpage.
    – R.Karar
    Jan 8, 2015 at 13:07
  • Well, You can definitely use the V4, copy it, and then make your own modifications. However, it will reference the core.css so you have to make another css reference. You will have to set the default and custom master at the site collection level to affect those pages (or do what you already suggested by copying files into the hive _layout). Are you building a SharePoint Solution in VS? If so I can show you how to create a site collection feature that the user could just turn on and off which will set your masterpages and reset them. Jan 8, 2015 at 21:17
  • Yes I am using a SharePoint Solution in VS. Oh wow that would be awesome if you can show me how. I really can't thank you enough.
    – R.Karar
    Jan 9, 2015 at 9:29
  • Oh, and you will need to work with the css and masters to get it to play properly with different browsers. If you need help on that you should be able to find it. Or you can post another question and I'll answer it there. cheers! Jan 14, 2015 at 16:55

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.

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