I want to check a sub site exist in site collection. I site exist a new one will be created.This is what I have tried:
int siteLanguage = 1033;
//Create a new webCreateInformation object to specify the properties of the new site being created.
Web oWebsite = clientContext.Web;
var newUrl = contextUrl + title;
clientContext.Load(oWebsite);
clientContext.ExecuteQuery();
//var subWeb = (from w in oWebsite.Webs where w.Url == webUrl select w).SingleOrDefault();
var subWeb = (from w in oWebsite.Webs where w.Url == newUrl select w).SingleOrDefault();
if(subWeb==null)
{
WebCreationInformation webCreateInfo = new WebCreationInformation();
webCreateInfo.Description = description;
webCreateInfo.Language = siteLanguage;
webCreateInfo.Title = title;
webCreateInfo.Url = title;
webCreateInfo.UseSamePermissionsAsParentSite = true;
webCreateInfo.WebTemplate = templateId;
//Adding a new site under the root web
Web oNewWebsite = oWebsite.Webs.Add(webCreateInfo);
// Retreiving the server properties to the client context. Here we are retreiving the web server url and the time it was created.
clientContext.Load(oNewWebsite,
Website => Website.ServerRelativeUrl,
Website => Website.Created);
clientContext.ExecuteQuery();
ViewBag.Message = "Site created.";
}
else
{
ViewBag.Message = "already exist";
}
but it is not working, got an error like collection not initialized
.
Can anyone please help?