0
votes

I'm new to SharePoint 2010 coding web parts. I'm trying to figure out how to get the landing page of a SharePoint site given the URL.

Ex. I give the function http://www.yahoo.com and I get http://www.yahoo.com/pages/default.aspx .

Here's the function so far:

private string GetSPSiteUrl(string u) {
    var siteurl = string.Empty;

    using (SPSite site = new SPSite(u)) {
        using (SPWeb web = site.OpenWeb()) {
            siteurl = web.Url;
        }
    }

    return siteurl;
}

The function just returns what I give it now which is no use.

Any help would be great. Thanks!

3

3 Answers

-4
votes

Perhaps the guidance in this thread would be of assistance.

8
votes

A simpler approach without having to click through.

This gives you the direct "WelcomePage" url:

web.RootFolder.WelcomePage

If you need the actual item:

SPListItem welcomePage = web.GetFile(web.RootFolder.WelcomePage).Item;
4
votes

To ensure that anonymous users can get it use:

public static string GetWelcomePageUrl(SPWeb web)
    {
        if (web.DoesUserHavePermissions(SPBasePermissions.BrowseDirectories))
        {
            return web.RootFolder.WelcomePage;
        }
        string welcomePage = string.Empty;
        SPSecurity.RunWithElevatedPrivileges(delegate
        {
            using (SPSite sPSite = new SPSite(web.Site.ID))
            using (SPWeb sPWeb = sPSite.OpenWeb(web.ID))
            {
                welcomePage = sPWeb.RootFolder.WelcomePage;
            }
        });
        return welcomePage;
    }