I am trying to find if a subwebsite exists in sharepoint, and if it does, then add +1.
I think the loop can be improved.
// subsiteName will be like "FruitCrateOfCompanyX"
public static string FormatMyHomieURL(string subsiteName, string mainSite)
{
if (!mainSite.EndsWith("/"))
mainSite = mainSite + "/";
string temp = mainSite + subsiteName;
int i = 0;
while (SubwebExists(temp))
{
i++;
temp = mainSite + subsiteName + i;
}
return subsiteName + i; // I only need to return subSiteName which will be used later on by web services to create a unique subweb
}
public static bool SubwebExists(string url)
{
bool exists = false;
Uri u = new Uri(url);
using (SPSite site = new SPSite(url))
using (SPWeb web = site.OpenWeb(u.AbsolutePath.ToString(), true))
{
exists = web.Exists;
}
return exists;
}
return subsiteName + i
doesn't look right? what does it return if you run this code? it looks like you haven't actually written the code yet, and don't know if this code will run at all. you shouldn't be able to concatenate an integer to a string like that. \$\endgroup\$ – Malachi♦ Jul 7 '14 at 13:45