How to deploy a custom master page in SharePoint 2013 using Visual Studio



I have made this tutorial as many of my existing and coming tutorials start with deploying a custom master page to SharePoint 2013.

First you need to make sure you have a development envierment that is running SharePoint 2013 and have Visual Studio 2012/13 installed.

  1. Start by creating a “Webb Application” and a new Site Collection in Central Admin for this tutorial.
  2. Open Visual Studio and create a new project (SharePoint 2013 – Empty project) that you call “MyLabProject”
    Screen Shot 2013-12-05 at 22.24.21
  3. Choose Deploy as farm solution, or if you make this for Online then choose Sandbox.
    Screen Shot 2013-12-05 at 22.24.47
  4. When the project opens, click on the Project Name in the solution explorer and then set the property Site URL to the site collection you made.
  5. Now right click on the project and add a folder called FeatureElements
  6. Right click in the folder Feature Elements and add – > New Item, Under Visual C# -> SharePoint you create a new Module that you call MasterPages.
    Screen Shot 2013-12-05 at 21.35.19
  7. Remove the sample.txt
  8. Open your web browser and go to the site you created (http://mylabproject) and in there go to Settings -> Site Settings -> Master pages and page layouts. Find seattle.master and download a copy of that master page to you desktop.
  9. Rename the seattle.master to “MyCustom.master”. Now go back to VS and right click in the MasterPages module and take Open Folder in File Explorer. Copy your MyCustom.master to that folder.
  10. Go back to VS and In the Solution Explorer menu, click Show All Files. You can then see the MyCustom.master in the MasterPages Module. Right click and include in project.
  11. Open the Elements.xml that are in the MasterPages module and remove all code and replace with his code.

     
  12. Now go to the Features Folder and you will find Feature 1, Rename the feature to “Activate.Custom.Master”.
  13. Dubble klick on the feature to open it and Change the Title to “Activate Custom Master”.
  14. Set Scope to Site and make sure MasterPages are included, save and close.
    Screen Shot 2013-12-05 at 21.54.31
  15. Now right click on the feature and Add Event Receiver.
  16. The feature receiver will open (the Activate.Custom.EventReceiver.cs). If not Open it.
    The code under here will replace the default master page when you activate the feature and when you deactivate the feature it will restore to seattle.master.
  17. Delete every thing under “public class ActivateCustomEventReceiver : SPFeatureReceiver {” and paste in the following code under  the “{”
    (My code example will show the full Code, just so that you get the full picture. I have commented in the code what part you should copy. ) 
  18. Right click on the project and take Deploy. If every thing is working you should see your MyCustom.master if you go to your site and choose Settings -> Site Settings -> Master Page.
    Screen Shot 2013-12-05 at 22.19.42
  19. Your Project should look like this now. Good Luck!
    Screen Shot 2013-12-05 at 22.35.01

24 responses to “How to deploy a custom master page in SharePoint 2013 using Visual Studio”

  1. Karthik says:

    Nice Post

  2. Anna says:

    Hi,

    I am in the search for a best practice on how to deploy custom branding (such as master pages and css files) to SharePoint 2013 in the case where farm solutions are not an option.

    As I have understood it the sandboxed solutions which contain code are deprecated and should not be used. Do you have an alternative way of setting the master page to the site?

    Best regards,
    Anna

  3. ines says:

    S’il vous plait j’ai besoin d’une aide , lorsque j’accède à Paramètres -> Paramètres du site -> Page maître
    je trouve la page créée mais où je trouve les paramètres pour définir master page et master page système comme tu l’a mis dans le screen-shot ?!!

    Google Translation…
    Please I need help, when I go to Settings -> Site Settings -> Master Page
    I found the page created, but where I find the settings to set master page and system master page as you put in the screen-shot?!

  4. ines says:

    C’est bon ça marche j’arrive à résoudre mon problème 🙂

  5. Nick says:

    Thank you for the article, it has been very helpful. Just a note for those who may have this issue, though: If “Master Page” does not show up under “Look and Feel” you have to do two things. Turn on “SharePoint Server Publishing Infrastructure” in “Site collection features” and “SharePoint Server Publishing” under “Manage site features”. Hope this helps.

  6. Cinzia says:

    Thanks for this article it’s very clear and easy to follow and of course helpful ^-^.
    How can I show the custom master page in the Composed Looks of SharePoint Foundation 2013.
    I have tried by copying and renaming the seattle.preview but it doesn’t work.
    Thank You
    Cinzia

    • Hi Cinza,
      What i know about composed looks you cant use a custom master page. Not in the way i intended it in this tutorial. Composed looks are just Themes where you change colors/fonts more or less.
      If you use the solution WSP that this blog describes you will replace composed looks with this custom master and “branding”.

      Cheers,
      Frederik

  7. Atul Chavan says:

    Thanks 🙂
    It helped…

  8. Chase H says:

    Frederik,

    First, I want to thank you for this tutorial, it has helped greatly. I successfully used this to deploy a custom masterpage to my site collections. My question is, if I wanted to use this to publish to all site collections in my farm rather than one specific collection how would I go about doing this? We have roughly 15 site collections and I’d like to have this solution set up to where I can make a change in one location and have it published across all sites without having to reactive the feature in each site collection.

    Any help would be greatly appreciated.

  9. dany says:

    Hi,
    Great post .

    sorry for my english.

    Maybe i trunk but ,
    great to know why in you’r code , to not check , if publishing infrastructure feature was yet activated
    beacause if you deploy this feature in publishing site ,the publishing infrastructure feature , default was yet activated

    Thanks for you replay

  10. Daniel says:

    Hi Frederik,
    just wanted to drop you a line, that your article helped me a lot.
    Thanks man!
    Daniel

  11. Yeye says:

    Thanks! Helped me a lot! Keep it up. 😀

  12. Shariare says:

    Hy i am facing this error when deploying the project….. Please help me out of here.

    Error 32 Error occurred in deployment step ‘Activate Features’: Failed to instantiate file “MyCustom.master” from module “MasterPages”: Source path “Features\MyLabProject_Activate.Custom.Master\MasterPages\MyCustom.master” not found. 0 0 MyLabProject

  13. I am able to deploy the solution, but the feature does not appear in the list of site features.

  14. Further information….

    If I enum the features using the command below….

    Get-SPFeature | select ID, DisplayName, Scope, CompatibilityLevel | Export-Csv c:\Features.csv

    I one of the lines provided is (I removed the Display Name)….

    “135cbe36-3697-49bd-b243-e78f3da7a73a”,””,”Site”,”15″

    So, the feature is installed, but I can not see it in my site collection.

  15. I am an idiot (at least sometimes). I was looking in the wrong place. Many thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *