简体   繁体   中英

How Do I Get The Default/Welcome Page Of A SharePoint 2010 Site?

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!

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;

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;
    }

也许主题中的指导会有所帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM