The easiest way I found is
// Checks whether a website exists by looking up its url in the current site collection bool WebExistsByUrl(string url) { return SPContext.Current.Site.AllWebs.Any(w => w.Url.Equals(url, System.StringComparison.InvariantCultureIgnoreCase)); } // Checks whether a website exists by looking up its name in the current site collection public static bool WebsiteExistsByName(string name) { return Array.Exists(SPContext.Current.Site.AllWebs.Names, n => n == name); }
or even better you can put them in an extension class to be more accessible:
public static class SiteExtensions { // Checks whether a website exists by looking up its name public static bool WebExistsByName(this SPSite site, string name) { return Array.Exists(site.AllWebs.Names, n => n == name); } // Checks whether a website exists by looking up its URL public static bool WebExistsByUrl(this SPSite site, string url) { return site.AllWebs.Any(w => w.Url.Equals(url, StringComparison.InvariantCultureIgnoreCase)); } }
Leave a Reply