Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have designed a module that change master page dinamically based on user log-in privileges.

I followed this tutorial :

http://ranaictiu-technicalblog.blogspot.it/2009/10/sharepoint-dynamically-change-master.html?showComment=1354630820782#c5132604142640789136

Everything works well. But only works on sharepoint home page. If try to go to other page like

_layouts/user.aspx

or

/_layouts/closeConnection.aspx

then the page would crash!

public class DynamicMasterPageModule : IHttpModule
{
    public void Dispose()
    {


    }


    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }


    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        Page page = HttpContext.Current.CurrentHandler as Page;
        if (page != null)
        {
            page.PreInit += new EventHandler(page_PreInit);
        }
    }
void page_PreInit(object sender, EventArgs e)
    {
        Page page = sender as Page;
        string pageNo = ConfigurationManager.AppSettings["MasterPageNo"];




        if (page != null)
        {
            if (pageNo.Equals("1"))
            {
                page.MasterPageFile = "~masterurl/custom.master";
                if (SPContext.Current != null)
                {
                    SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/custom1.master";
                }
            }
            else if (pageNo.Equals("2"))
            {
                page.MasterPageFile = "~masterurl/custom.master";
                if (SPContext.Current != null)
                {
                    SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/custom2.master";
                }


            }
            else
            {
                page.MasterPageFile = "~masterurl/default.master";
                if (SPContext.Current != null)
                {
                    SPContext.Current.Web.MasterUrl = "/_catalogs/masterpage/default.master";
                }
            }


        }

ERROR :

The file '/_layouts/masterurl/custom.master' does not exist. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: The file '/_layouts/masterurl/custom.master' does not exist.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[HttpException (0x80004005): The file '/_layouts/masterurl/custom.master' does not exist.]
   System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath) +11108402
   System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) +163
   System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) +116
   System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +175
   System.Web.UI.Page.get_Master() +69
   System.Web.UI.Page.ApplyMasterPage() +18
   System.Web.UI.Page.PerformPreInit() +58
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1256
share|improve this question
    
Can you paste the code that changes the master page? I think it might be because you are getting the url of the master page relatively, you might need to change the path a little. (You can check for a file not found in the ULS) –  Hugh Wood Dec 4 '12 at 15:09
    
ok. that's the main part of the code. Do you see anything wrong? –  user1200566 Dec 4 '12 at 15:25

1 Answer 1

up vote 0 down vote accepted

In follow up to my comment, the code is indeed lookup like it is possibly not referencing the master page properly.

The code should look more like this:

string masterUrl = site.ServerRelativeUrl;

if (!masterUrl.EndsWith(@"/"))
{
    masterUrl = masterUrl + @"/";
}

SPFile file = web.GetFile(masterUrl + "_catalogs/masterpage/custom.master");

This will give the correct permission based relative url for the master page.

share|improve this answer
    
Hi . I tried but no way. Seems it's not the problem. Non problem for the home page but for other gives me error! –  user1200566 Dec 4 '12 at 15:56
1  
Check the error in the logs what does it say? Please post as an edit to your question in a code block –  Hugh Wood Dec 4 '12 at 16:06
    
oK. I got the error! I modified my code block as you told me! Thanks –  user1200566 Dec 4 '12 at 16:50
    
Because your master page will be deployed to the catalog not layouts. –  Hugh Wood Dec 4 '12 at 16:52
1  
The code isn't looking up the master page name correctly, use the code I provided to get the url. It has to be in place of the code you are using. the ~masterurl token isn't providing the correct values. –  Hugh Wood Dec 4 '12 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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