Kristian Hebert

Hello all,

I am trying to change the masterpage runtime, based on the currently logged in user. I understand this needs to be done at the preInit event on each sharepoint page. Since I don't want to change every page in the site, I have made my own implementation of IHttpModule, added it to the GAC and registered it in web.config.
It works great whith ordinary WSS 3.0 pages, but for some reason I never catch the preInit event for publishing pages.

Here's my code:
Code Snippet

public class Hook : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

}

void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page;
IHttpHandler handler = HttpContext.Current.Handler;
page = 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)
{
string strUrl = "/Docs/_catalogs/masterpage/";
SPWeb web = SPControl.GetContextSite(HttpContext.Current).OpenWeb();

SPUser user = web.CurrentUser;

if (user == null)
strUrl = "";
else
strUrl += "BlueBand.master";
if (strUrl.Length > 0)
page.MasterPageFile = strUrl;
}
}

public void Dispose()
{
}
}


When a publishing page is loaded the page_PreInit method gets registered, but it is never called. In these cases the page object is always of type TemplateRedirectionPage, which may have something to do with my problems, I just don't know what, or how I am going to solve it.
Any comments are very much appreciated.



Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Dave Moyle

Did you ever resolve this I am experiencing the same thing Sad






Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Kristian Hebert

No I didn't, but I use a workaround, where I make a class that inherits from SPPublishingPage (I think that was the name). And in that class I set the masterpage in the PreInit event. Then I need to change all the LayoutPages to inherit from the new page, instead of SPPublishingPage.




Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Dave Moyle

Thanks for that Kristian.

Not quite the scalable solution I'm looking for though Sad

Cheers

Dave






Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Kristian Hebert

It is scalable. Notice that you have to change the PageLayout pages, not every new page. Unless you plan on adding more pagelayouts everything would run without further intervention once you change the existing 5-10 pagelayouts. It's not ideal or pretty, but it works, and no one has apparantly come up with a better idea.





Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

zrizzi

Do you have an example of what you did to fix this....I am experiencing the same problem...





Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

zrizzi

Here is how i got around it. I created a httpmodule and in the PreRequestHandlerExecute event I set the CustomMasterUrl property of the current site, only if the page is a PublishingLayoutPage or a TemplateRedirectionPage.

if (page is PublishingLayoutPage || page is TemplateRedirectionPage)

{

SPContext.Current.Web.CustomMasterUrl = myMasterPage;

}

I don't save the site, so it only changes it for this instance. If there are any ideas or thoughts on this please let me know.





Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Kristian Hebert

That's great, I haven't tried it yet, but if it works, it is the answer I was looking for.

I don't see why you would want to save the site with the given masterpage, isn't the whole idea to be able to set it dynamically





Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Faizur Rahman

I am having the same problem. Can anyone tell me how to make the PreInit event to fire I am also getting the TemplateRedirectionPage as the Page Object, and any subscribed page events are not working.





Re: SharePoint - Development and Programming Unable to catch preInit event on Publishing Pages

Kristian Hebert

Great job zrizzi it now works perfectly for me.