How to brand SharePoint 2013 My Site using Visual Studio (Updated)



I will walk though, step by step how you can brand the My Site in SharePoint 2013 using Visual Studio.

(You can also read my other article on “How to brand SharePoint 2013 Team sites“)

Before you start you need to make sure you have the following.

  • SharePoint 2013 development environment
    • Visual Studio 2012 installed in the server.
  • You need a configured/working the My Site host

OK, so lets start.

  1. Open Visual Studio and create a new Project. (You can do this within an existing Solution or as a New Solution, in this case i extend my Solution with My Site Branding)
  2. Under Templates – > Visual C# -> Office/SharePoint choose a SharePoint 2013 – Empty Project and give it a name and click OK
    Screen Shot 2013-08-07 at 21.06.33
  3. Type in the URL of you My Site host and choose Deploy as Farm solution, go Finish
    Screen Shot 2013-08-07 at 21.09.50
  4. Right click on the project and create a new folder that you call FeatureElements
  5. Right click on the project and create add SharePoint “Layouts” mapped folder
    1. Under the mapped folder you create the following folders: Images, Scripts and Styles. You can later in use these to add your styling resources like CSS and JavaScript.
  6. You project should look something like this now.
    Screen Shot 2013-08-07 at 21.20.19
  7. Now you need to add your custom MySite Master page to the project, start with adding a Module under FeatureElements (Right kick the folder and choose add -> new item). Call the Module “Masterpages”.
  8. Delete the sample.txt and then open Explorer and go to  C:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15TEMPLATEFEATURESMySiteMaster.
  9. Create a copy of mysite15.master on your desktop and rename it to something like CustomerNameMySite.master. Then move the master  to your new Module under Feature Elements. (if you put it in the project folder you have to right click on it in VS and take include in project) (Disregard the GS_ApplyCustomMasterPageStapler in the image, that coming steps…)
    Screen Shot 2013-08-07 at 21.38.03
  10. At this point i normally swap the CSS to my custom and add a colored background, it makes it easier to see that it works later on.
  11. Let continue with editing the Modules Elements.xml, replace all code with the following…(change the name of you .master)
  12. Add a feature under the folder Features, make it a “Site” feature and name it “ApplyCustomMasterPage or simular” (you have to rename the feature in VS too)
  13. Right click on the feature and add Event Receiver, then edit the receiver and you will find a GUID, save it to note pad for now.
  14. Replace all code in the receiver with this code and then replace my GUID with yours. Alos rename the master page in the code to reflect your master page name.
  15. Click on the feature and include Masterpages (the module you created earlier.)
  16. Now we need to create the stapling feature that staples the custom master page to a My Site when created. Under Features you create a new Feature that is a “Farm” feature. Call it something like “CompanyName_MySiteStapler”.
  17. Go to FeatureElements folder and Add -> Empty element and name it something like  “ApplyCustomMasterPageStapler”.
  18. Add the following code to it and replace my GUID with the GUID from your Feature called ApplyCustomMasterPage. (open the feature and click on the tab Manifest, you will find the GUID id there.)
  19. Go back to the CompanyName_MySiteStapler Feature that you created in step 16 and Add the new ApplyCustomMasterPageStapler Element to it.
  20. Thats is, if every thing is working you should now have a custom master page in your MySite that you can brand however you like. Don’t forget that when you deploy this to the production you will need to Activate the feature the MySite Site collection under site collection features.
    This is how the project looks now.
    Screen Shot 2013-08-07 at 22.09.26

Update, Add the branding to other MySite pages like blog etc.

    1. In your project create a new folder that you call Receivers, right click the folder and Add new Event Receiver. Call it something like “ElementsWebProvisioned”. Click Next.
    2. On the settings page choose “Web Events” and “A site was provisioned”, click finish.
    3. Add the following code and don’t forget to change the namespace to mach your project.

  1. Add the receiver to the ApplyCustomMasterPage Feature you created earlier.
  2. Deploy the Solution and you are down. (You may have delete your MySite and recreate it to get the branding applied on blogs etc.)

25 responses to “How to brand SharePoint 2013 My Site using Visual Studio (Updated)”

  1. Martin Reichelt says:

    Very clear walkthrough guide. Thank you & keep posting!

  2. vaibhav shinde says:

    Its helpful in my case,Thanks

  3. Craig Tarr says:

    Hi Frederik, Thanks for the post. It seems to work fine for me for the Mysites host site, i.e. when I go to newsfeed the branding is applied, however when I go to Skydrive pro or sites it isn’t. ANy clues what I could be doing wrong?

  4. P Zhang says:

    Really appreciate your post! Thank you for the time and effort to share the knowledge. It helps me a lot as I am faced with branding my sites.

  5. Kirill says:

    Thanks a lot Frederik!
    I am newest at sharepoint 2013 and your guide is the best I ever seen now for sharepoint. It works correct in my solution after first deploy 🙂

    • Thanks Kirill! Im glad it helped you out. I try to make my guides as detailed as possible, I know how frustrating it is to find a guide where the author only explain half of what is needed to complete something.

      /F

  6. Ash says:

    Hi Frederik, nice post !!!

    I tried it, and the feature(scoped to site collection) will not activate on new personel sites. If you activate the feature manually on the newly created personel site, it gets applied. Any idea on how to fix this?

    • Hi,

      This should work. Make sure you have done the stapler correct, the feature “CompanyName_MySiteStapler” This feature and its module/element is activating the master page feature when the site is created.

      /Fredd

  7. Allan says:

    I love this article, it’s nice a clear which makes a change from some of the stuff out there, my question is can these steps and this approach be used on a publishing site? or would that be handled differently?

    Thanks

    • Hi Allan, thanks for the feedback! 🙂
      Have a look at this article, it describes how you deploy a custom master page on a publishing portal. “How to deploy a custom master page in SharePoint 2013 using Visual Studio

      Cheers,
      Fredd

      • Allan says:

        Thanks for the link Frederik that’s great. I had a look at that previously but my problem is this, I want to include not only a master page but also images and css files as well. When I deploy them I want the entire branding to be a feature you can turn on and off. The article you have linked only covers picking a masterpage from a list upon deployment. I need something on the same track as these articles where my solution is deployed and then activated from the features menu but on a publishing site instead 🙂 Perhaps some key steps in your articles could bridge that gap?

        • Hi, You are right, the bridge in that manner. let me get back to you when im back from my house in the archipelago on Monday, jus have my mobile with me 🙂

        • HI,
          To add images and Style do the following.
          1. Right click on your project and add SharePoint “images” mapped folder.
          2. Right click on your project and add SharePoint “layouts” mapped folder.
          This will add the mapping to the images and style folders in your 15 HIVE (C:\Program Files\Common files\Microsoft Shared\Web Server Extensions\15\…)
          In etch folder you will see a sub folder with your projects name. For your images, just add them to the sub-folder. For the Styel, add one folder under the project name folder called Styles (for css files) and if you like a Scripts folder.

          The mapped folders will be included in the Feature you have to Activate the master. If not just open the Feature and add them to the included.
          To reference a style, script or image in your master page use:

          Let me know how it works out for you! 🙂
          /Fredd

  8. sarath says:

    Great post Fred!! , Keep posting.. 🙂

  9. James says:

    Great post! One newbie question: you mention in your update to add a “new element receiver”. Did you mean an Event Receiver? And Empty Element? I do not see “element receiver” anywhere. Also, the “Receivers” folder is added to “FeatureElements”?

    Thanks for your time.

  10. Victor Camargo says:

    Hi, Thanks for the very detailed post.

    Unfortunately it still fails …

    After installing the SPSolution, SharePoint is unable to create the site “personal”.

    When looking at the log, I identified the following points of attention:

    The configuration database was queried for a non-existent object with the id “5476deaf-3149-4d85-b463-18470d26fe1c” Most commonly, this is caused by removing an SPFeatureDefinition without writing upgrade code to remove references to the feature from each site
    A solution with the same name “meusitetemplate.wsp” or id “5476deaf-3149-4d85-b463-18470d26fe1c” already exists in the solution store.

    Exception in EnsureTemplateAssociatedSiteFeaturesActivated: System.InvalidOperationException: Failed to compare two elements in the array. —> System.FormatException: Expected hex 0x in ‘{0}’. at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument) at System.Guid.TryParseGuidWithHexPrefix(String guidString, GuidResult& result) at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult& result) at System.Guid..ctor(String g) at Microsoft.SharePoint.SPTemplateAssociationElement.ActivationDependencyComparer.System.Collections.Generic.IComparer.Compare(SPTemplateAssociationElement tplassoc1, SPTemplateAssociationElement tplassoc2) at System.Co… fa16d49c-b2d9-20f1-0908-0bc42572545c
    …llections.Generic.ArraySortHelper1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer1 comparer, Int32 depthLimit) at System.Collections.Generic.ArraySortHelper1.Sort(T[] keys, Int32 index, Int32 length, IComparer1 comparer) — End of inner exception stack trace — at System.Collections.Generic.ArraySortHelper1.Sort(T[] keys, Int32 index, Int32 length, IComparer1 comparer) at System.Collections.Generic.List1.Sort(Int32 index, Int32 count, IComparer1 comparer) at Microsoft.SharePoint.SPElementProvider.QueryForSortedElementsJoinOR[TElementType](List1 dictAttrPatterns, List1 lstfeatdefFeaturesOfInterest, List1 listofOptionalElementsToQuery, CultureInfo ciElements, IComparer1 icmp, Int32 webUIVersion) at Microsoft.SharePoint.SPTemplateAssoc… fa16d49c-b2d9-20f1-0908-0bc42572545c
    …iationElement.DetermineSiteScopedTemplateAssociations(SPSite site, String sTemplateName) at Microsoft.SharePoint.SPTemplateAssociationElement.EnsureTemplateAssociatedSiteFeaturesActivated(SPSite site, String sTemplateName) at Microsoft.SharePoint.SPFeatureManager.c__DisplayClass16.b__15() fa16d49c-b2d9-20f1-0908-0bc42572545c

    Failed to activate template-associated site-collection-scoped features for template ‘SPSPERS#2’ in site ‘http://dtcvspoint-01/pessoal/tjsqlsvc2013’. fa16d49c-b2d9-20f1-0908-0bc42572545c
    Failed to activate site features when provisioning site at url “http://dtcvspoint-01/pessoal/tjsqlsvc2013” with site definition “SPSPERS#2”. fa16d49c-b2d9-20f1-0908-0bc42572545c
    Failed to apply template “SPSPERS#2” to web at URL “http://dtcvspoint-01/pessoal/tjsqlsvc2013”, error Failed to compare two elements in the array. 0x8ac6c860 fa16d49c-b2d9-20f1-0908-0bc42572545c
    Failed to create personal site (tjsp\tjsqlsvc2013, http://dtcvspoint-01/pessoal/tjsqlsvc2013): Microsoft.SharePoint.SPException: Failed to compare two elements in the array. —> System.Runtime.InteropServices.COMException: 0x8107058aFailed to compare two elements in the array. at Microsoft.SharePoint.Library.SPRequestInternalClass.ApplyWebTemplate(String bstrUrl, String bstrWebTemplateContent, Int32 fWebTemplateContentFromSubweb, Int32 fDeleteGlobalListsWithWebTemplateContent, Int32 fIgnoreMissingFeatures, String& bstrWebTemplate, Int32& plWebTemplateId) at Microsoft.SharePoint.Library.SPRequest.ApplyWebTemplate(String bstrUrl, String bstrWebTemplateContent, Int32 fWebTemplateContentFromSubweb, Int32 fDeleteGlobalListsWithWebTemplate… fa16d49c-b2d9-20f1-0908-0bc42572545c
    My Site creation failure for user ‘tjsp\tjsqlsvc2013’ for site url ‘http://dtcvspoint-01/pessoal/tjsqlsvc2013’. The exception was: Microsoft.Office.Server.UserProfiles.PersonalSiteCreateException: A failure was encountered while attempting to create the site.
    Deleting the site at http://dtcvspoint-01/pessoal/tjsqlsvc2013 and not deleting AD accounts.

    I’m lost on the problem, can guide me?

    tks

  11. Mahesh says:

    What a wonderful post! saved my life and headaches man..!! great work..!!
    Was wondering if you could put powershell scripts to update existing mysites to new master page.??
    Thanks

    • Hi and Thanks! 🙂
      To update the existing sites you should be able to do something like this.

      Good Luck!

  12. Thomas says:

    When I try to build the solution (VS2013) I am getting hit with all sorts of Errors. Here are most of them:

    ‘Namespace’ statement must end with a matching ‘End Namespace’
    Syntax error (this is for the curly brackets)
    Declaration expected.
    Statement cannot appear outside of a method body/multiline lambda.

    Is there a way to force the build or is that not a good idea. I’m pretty new to VS. Any help would be appreciated.

  13. Tyo says:

    Hi Frederik thanks you very much for detailed guide.
    i’m newbie using sharepoint and this post helped me.
    Great post!!
    thank you again

Leave a Reply

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