Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get site URL based on GUID? (SharePoint)

Tags:

c#

sharepoint

is there any code example out there that shows me how to get the url for a site if I know the guid?

Currently I have this code to get all sites within the site collection.

private void getSites()
{
    SPSite oSiteCollection = SPContext.Current.Site;
    SPWebCollection collWebsite = oSiteCollection.AllWebs;
    for (int i = 0; i < collWebsite.Count; i++)
    {
        ddlParentSite.Items.Add(new ListItem(collWebsite[i].Title, collWebsite[i].ID.ToString()));
    }
}

Thanks in advance.

like image 537
peter Avatar asked Feb 25 '23 08:02

peter


1 Answers

SPSite has a GUID constructor

using(SPSite site = new SPSite(guid)) {
    return site.Url;
}

And SPSite has a OpenWeb(GUID) method

using(SPSite site = new SPSite("http://somesite")) {
    using (SPWeb web = site.OpenWeb(guid)) {
        return web.Url;
    }
}
like image 185
djeeg Avatar answered Mar 07 '23 21:03

djeeg