SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this code courtesy of Alex Angas in Programmatically instantiate a web part page in Sharepoint. I already posted this question on stackoverflow but didn't get any answer so I decided to transfer my question here.

In my _catalogs/masterpage folder, I have 2 masterpages namely:

  1. default.master
  2. mobilepage.master

I wanted to modify the code below in such a way that I could set the masterpage instead of default.master, I will make it to mobilepage.master which is created specifically for Mobile.aspx

This code below creates a mobile.aspx file inside the document library. My only problem is how to specify the masterpage of the mobile.aspx?

string newFilename = "Mobile.aspx";
string templateFilename = "spstd1.aspx";
string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS\\");
FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);
using (SPSite site = new SPSite("http://sharepoint")){
using (SPWeb web = site.OpenWeb())
    SPFolder libraryFolder = web.GetFolder("Document Library");
    SPFileCollection files = libraryFolder.Files;
    SPFile newFile = files.Add(newFilename, stream);

Please help me modify the code above. I would gladly appreciate your help. Thanks!

share|improve this question
Are you trying to set the master page for Mobile.aspx to be mobilepage.master? – Chakkaradeep Chandran Sep 19 '11 at 9:39
yes :) hope you can help me – Carls Jr. Sep 19 '11 at 9:50

I hope your Mobile.aspx is an Application Page. In application pages, you can set the master page either in the .aspx with the MasterPageFile attribute:

<%@ Page Language="C#" AutoEventWireup="true"
MasterPageFile="_catalogs/mobilemaster.master" %>

Or in the application page code behind, where your code may come to use:

this.Page.MasterPageFile = "custom master url";
share|improve this answer
Mobile.aspx is a webpart page, and it is created on the fly based on the web part page template. I don't think I can do that :( do you have any other option? – Carls Jr. Sep 19 '11 at 10:14

Your Answer


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.