Set application page as welcome page programmatically

02/08/2019 10:25

By design, setting application page as welcome page is not possible, you can only choose page inside the web. Code below does following steps

 

1. Creates standard page (web part page) in Site Pages list on web

2. Adds Script Editor web part to the page

3. Adds javascript to Script Editor (from Media web parts)  web part (to redirect to application page)

4. Sets this Site page as welcome page

 

You can do these steps also manually

In our case, ProjectHome.aspx is application page and ProjectHomeRedirect.aspx is site page. 

Here it is:

private static void CreateWelcomePage(SPWeb web)
{
var list = ListRepository.GetListByUrl("SitePages", web);
var pageRelativeUrl = list.RootFolder.ServerRelativeUrl + "/ProjectHomeRedirect.aspx";
list.RootFolder.Files.Add(pageRelativeUrl, SPTemplateFileType.StandardPage);
}
 
 
private static void SetWelcomePage(SPWeb web)
{
var rootFolder = web.RootFolder;
rootFolder.WelcomePage = "SitePages/ProjectHomeRedirect.aspx";
rootFolder.Update();
}
 
private static void AddRedirectToWelcomePage(SPWeb web)
{
var pageAbsoluteUrl = web.Url + "/SitePages/ProjectHomeRedirect.aspx";
 
var wpManager = web.GetLimitedWebPartManager(pageAbsoluteUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
 
var scriptWebPart = new ScriptEditorWebPart() { Title = "Script Editor Redirect Web Part" };
scriptWebPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
scriptWebPart.Content = "";
 
wpManager.AddWebPart(scriptWebPart, "Main", 0);
wpManager.SaveChanges(scriptWebPart);
 
web.Update();
}
private static void CreateWelcomePage(SPWeb web)
{
var list = GetListByUrl("SitePages", web);
var pageRelativeUrl = list.RootFolder.ServerRelativeUrl + "/ProjectHomeRedirect.aspx";
list.RootFolder.Files.Add(pageRelativeUrl, SPTemplateFileType.StandardPage);
}
 
 
private static void SetWelcomePage(SPWeb web)
{
var rootFolder = web.RootFolder;
rootFolder.WelcomePage = "SitePages/ProjectHomeRedirect.aspx";
rootFolder.Update();
}
 
private static void AddRedirectToWelcomePage(SPWeb web)
{
var pageAbsoluteUrl = web.Url + "/SitePages/ProjectHomeRedirect.aspx";
 
var wpManager = web.GetLimitedWebPartManager(pageAbsoluteUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
 
var scriptWebPart = new ScriptEditorWebPart() { Title = "Script Editor Redirect Web Part" };
scriptWebPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
scriptWebPart.Content = "<script>windowlocation.href =_spPageContextInfo.webAbsoluteUrl + '/_layouts/15/GM/ProjectHome.aspx'</script>"; #fix dot in javascript redirect
 
wpManager.AddWebPart(scriptWebPart, "Main", 0);
wpManager.SaveChanges(scriptWebPart);
 
web.Update();
}
 
 internal static SPList GetListByUrl(string webRelativeUrl, SPWeb web)
{
string listRelativeUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, webRelativeUrl);
try
{
SPList list = web.GetList(listRelativeUrl);
return list;
}
catch (FileNotFoundException ex)
{
LogService.LogError(ex);
return null;
}
}

 

Methods are then called in the same order

 

CreateWelcomePage(projectWeb);
SetWelcomePage(projectWeb);
AddRedirectToWelcomePage(projectWeb);