Friday, July 9, 2010

SharePoint 2010 custom masterpage with code behind file – Part 2

In part 2 I will tell how I eventually got a code-behind file working on the custom master-page that I deployed trough Visual Studio. To be clear I don’t use SharePoint Designer to customize my master pages so I cannot guarantee that those pages will work.
To completely understand this information I recommend you to read Part 1 first.
The story continues….

 

Add Reference…

The next thing to do is adding a Reference to the SharePoint 2010 project.
This can be done by right-click on References in the Solution Explorer and then –> Add Reference
12
A new Window will popup arise that look like this:
13
As far as I know there 2 References that there need to be added:
  • Microsoft.SharePoint.Publishing
    • First select the Browse tab
    • then go to following directory: C:\Program Files\Microsoft Office Servers\14.0\Bin
    • Select Microsoft.SharePoint.Publishing.dll
    • Press the OK button

  • System.Web
    • Repeat the Add reference step
    • Now select the .NET tab
    • Find the System.Web
    • Select it and press the OK button








      • 14













        Add object(s) for the code-behind file


        In this how-to I will add 2 objects that I will manipulate with the code-behind file.

        asp:LabelFirst I added an asp:Label to the masterpage. I put this above the ribbon section so it will be noticed when it works.








        div
        I put a simple div around the ribbon so that I can disable the complete ribbon.
        <div id="divRibbonContainer" runat="server">
        
        
        
        
        
        
        
        div>







        The id’s of these objects will be needed for the code-behind file.



        The code-behind file




        To create a code behind file right-click on the Module in the solution, –> Add –> New Item…
        15



        In the following window select on the left Code –> Class and I called the file _starter.master.cs, just like a normal code-behind file.
        16







        There are some things that need to be added



        • Add using statements of:


          • System.Web;
          • System.Web.UI;
          • System.Web.UI.WebControls;




      • Make the class public and let it inherit from Masterpage



      • Declare protected objects from the masterpage



      • Add a page_load method











        • If you’ve done all that your class should look something like this:
          using System;
          
          using System.Collections.Generic;
          
          using System.Linq;
          
          using System.Text;
          
          using System.Web;
          
          using System.Web.UI;
          
          using System.Web.UI.WebControls;
          
          
          
          namespace MasterPageWithCodeBehind.MasterPageModule
          
          {
          
          public class _starter : MasterPage
          
          {
          
          protected System.Web.UI.HtmlControls.HtmlGenericControl divRibbonContainer;
          
          protected Label Label1;
          
          
          
          protected void Page_Load(object sender, EventArgs e)
          
          {
          
          divRibbonContainer.Visible = false;
          
          Label1.Text = "Hello World!";
          
          }
          
          }
          
          }












          Retrieving Public Key Token/PublicKeyToken




          This is a little bit tricky, this token is needed in the next step when we need to apply the Inherits attribute.



          • First build the project
          • Right-click on the SharePoint 2010 project in the Solution Explorer


            • Click --> Open Folder in Windows Explorer
            • Next open: bin
            • Open: Debug
            • Copy the directory path and paste it into a text editor or something like that
            • Next copy the filename of the .dll file en past it behind the path you just pasted in the text editor
            • The result should be something like this:
              D:\administrator\documents\visual studio 2010\Projects\MasterPageWithCodeBehind\MasterPageWithCodeBehind\bin\Debug\MasterPageWithCodeBehind.dll




        • Next go to Start (Windows start menu)


          • Programs/All programs


            • Microsoft Visual Studio 2010


              • Visual Studio Tools


                • Visual Studio Command Prompt (2010)









          • You’ll get a dos prompt.
            Do the following things:



            • Enter the following command: cd\
            • Next copy your complete directory path including the *.dll from your texteditor
            • Type the following:
              - sn.exe –Tp “Don’t forget the quotes around the directory and filename
              It should look something like this:
              sn.exe -Tp "D:\administrator\documents\visual studio 2010\Projects\MasterPageWithCodeBehind\MasterPageWithCodeBehind\bin\Debug\MasterPageWithCodeBehind.dll"



            The output should be something like this:
            17



            Copy the Public key token.



            Inherits




            To combine the code-behind file with the masterpage there need to be an attribute added to the masterpage directive.
            The following data is needed:



            • Namespace of the class & Type/Class name (these need to be seperated by a dot) (MasterPageWithCodeBehind.MasterPageModule._starter)
            • Strongname/Assembly in my case was this the same as the projectname (MasterPageWithCodeBehind)
            • Version (Version=1.0.0.0)
            • Culture (Culture=neutral)
            • PublicKeyToken (PublicKeyToken=f8a88530fbc7b81b)In the masterpage navigate to the following:



            Add the following Inherits attribute:




            Now the 5 dots above need to be combined, in my situation it looks like this:
            MasterPageWithCodeBehind.MasterPageModule._starter, MasterPageWithCodeBehind, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f8a88530fbc7b81b



            Place this as value from the Inherits attribute.



            Result





            Deploy your SharePoint 2010 project and load the default site.
            Your ribbon should be gone and a text like Hello World! should be visible.



            The left top corner of my SharePoint 2010 looks like this now:
            18



            No comments:

            Post a Comment