I’m trying to execute the following code snippet which creates a new site from a webpart depending on the user input:
SPWeb mySite = SPContext.Current.Web;
SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb;
SPWebCollection subSites = mySite.Webs;
string currentTemplate = mySite.WebTemplate;
string siteUrl = siteURL1;
string siteTitle = siteTitle1;
string siteDescription = siteDescription1;
subSites.Add(siteUrl, siteTitle, siteDescription, 1033,
currentTemplate, true, false);
How can I make an exception where the program will ignore the snippet if the site already exists? for example
if (siteURL exists)
code here;
else
do nothing
You can check
subSites.Names.Contains(siteUrl), which contains the names of all subsites. Check out the MSDN documentation for details.