10

Is there a way to load a different master page if I can identify the user to be annonymous?

  • 1
    In my experience the "master page switching" approach leads to a great deal of confusion and complexity. I would try to avoid it if you can find another way of achieving what you are looking for. – SPDoctor Nov 11 '11 at 0:12
13

If you want to do it at page level, you can assign a different master page on "OnPreInit" event. Below is the example to do it on layout page if you have publishing site :

public class MyPublishingLayoutPage:PublishingLayoutPage
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            if (Context.Request.LogonUserIdentity.IsAnonymous)
            {                
              SPWeb web = SPContext.Current.Web;
              this.MasterPageFile = "~/_catalogs/masterpage/anonymous.master";                   

            }
        }

    }

If you have many pages, I would recommend to use HttpModule to hook PreInit handler into the page. Below is the example :

public class AnonymousMasterModule: IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute+= new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
                string requestUrl = HttpContext.Current.Request.Url.ToString();
                if (!requestUrl.Contains(".aspx"))
                    return;               

                 Page page = HttpContext.Current.Handler as Page;
                 if (page != null)
                    page.PreInit += new EventHandler(page_PreInit);                

        }

        void page_PreInit(object sender, EventArgs e)
        {            
            Page page = sender as Page;
            if (page != null)
            {
                if (Context.Request.LogonUserIdentity.IsAnonymous)
                {
                    SPWeb web = SPContext.Current.Web;
                    page.MasterPageFile = "~/_catalogs/masterpage/anonymous.master";
                }
            }

        }
        public void Dispose()
        {

        }
    }
6

If your goal is to have a different look and feel for different users, you can use a single Master Page, and include steps that only target specific profiles (anonymous users in your case).

Search the internet for the following terms: SPSecurityTrimmedControl and AnonymousTemplate.

It is what I use on this draft page:

http://usermanagedsolutions.com

For authenticated users, the site is just an out of the box SharePoint 2010 page. I add a css layer for anonymous users to modify the page width and hide the top bar.

5

This post describes how can you change masterpage dynamiccally.

  • This is sharepoint 2007, so I don't have access to code behind like that. Is there a way to do it 2007? – mdrussell0779 Nov 10 '11 at 18:37
  • sorry, I don't know another solution. – Alexander Nov 10 '11 at 18:47
  • If you working with SharePoint 2007, you can create HttpModule, and change the master page as described in the post. I don't see a reason related to SP2007 why you can't do that if you have SP on dedicated server. We used this approach to customize design of application pages in SP2007. Steps: – Yuri Leontyev Nov 10 '11 at 19:58
  • Steps: 1. create HttpModule that performs changing master page for anonymous users. 2. deploy it in GAC 3. register Module in web.config - Thats all. – Yuri Leontyev Nov 10 '11 at 20:03
  • @mdrussell0779 : You can create a code behind and implement what Alexander has suggested. – Amit Kumawat Nov 11 '11 at 10:44
4

You can use two options only for targeting content to users (not change of master page):

  1. Audience for Server version - http://www.codeguru.com/csharp/.net/net_data/sortinganditerating/article.php/c13255__2/ and for 2010 but the concept seems to be the same in 2010 - http://blog.mastykarz.nl/content-targeting-anonymous-users-sharepoint-server-2010-part2-2/
  2. SPSecurityTrimmedControl - which allows showing content based on permissions - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spsecuritytrimmedcontrol%28v=office.12%29.aspx
3

You can use an Http Module to accomplish this.

1

You can include within the Master Page steps that only target specific profiles (anonymous users in your case).

Search the internet for the following terms: SPSecurityTrimmedControl and AnonymousTemplate.

It is what I use on this draft page:

http://usermanagedsolutions.com

For authenticated users, the site is just an out of the box SharePoint 2010 page. I add a css layer for anonymous users to modify the page width and hide the top bar.

1

This is the same what you asked, See the answer: How can we use a different masterpage for annoymous and authenticated users?

You can do with httpmodule or a code-behind in a page

0

I have tried your example in my publishing site for web part pages. But its not work.

i debug the code but event handler is not call the below method.

void page_PreInit(object sender, EventArgs e)

Please advice...

Thanks in advance

  • Did you register the HttpModule in web.config? Google up "HttpModule" and how to use it. – Sleeper Smith Oct 19 '12 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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