Donnerstag, 14. April 2011

How to get the default page of a site

It is not always necessary to have the default page of a site called default.aspx.
You can use a page called mypage.aspx and use this one as default.

But when you iterate through a site, how do you find out, which page is the default one. Therefore use a code like this to find out.

First you need a SPWeb object of your site-url (e.g. your site-url is http://www.yourwebapp.com/yoursite/yoursubsite/)

string result = string.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate{
SPWeb web = SPContext.Current.Site.OpenWeb("yoursite/yoursubsite");
result = web.RootFolder.WelcomePage;
});

The result value will be "Pages/mypage.aspx".