Ever run into a situation where you allow a user to configure a custom component with site URLs but wish you can verify if those URLs are valid in SharePoint?
This was the case for a couple of custom components I recently completed. The user simply types the Site URLs on the WebPart Tool Pane configuration Textbox delimited by semicolon.
One thing I wanted to do is to validate if the Web Application existed before actually iterating through it and searching for a given SPSite Object based on the URL. (line 6)
Here is a snippet on that (other code omitted for brevity)
//check if the Web App exists _spWebApplication = SPWebApplication.Lookup(new Uri(url)); if (_spWebApplication != null) { //We have a Web App, find Site by URL if (_spWebApplication.Sites.Where(s => s.Url.Equals(url)).Count() > 0) { SPSite _site = new SPSite(url); _sites.Add(_site); } }
NOTE: Code not optimized etc, but this should give you an idea.
Hope this helps!
Oscar