patterncsharpMinor
Checking if website exists and adding +1
Viewed 0 times
websitecheckingaddingexistsand
Problem
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.
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;
}Solution
Eliminate potential logic error in return value
I'm assuming the purpose of this code is to let you provide the name you want for your subsite, and the code will give you an available name based on whether the provided name already exists. It does this by appending/incrementing a suffix to the provided name whenever there is a naming collision. If this is the case, your
To walk through an example briefly, let's say you want to name a subsite
Reduce the number of instantiated SPSite objects
You are only ever dealing with a single SPSite object in this code (the parent site that you are checking for subsites). Therefore, you don't really need to create and dispose of a new SPSite object in every iteration of your loop just to grab a subsite from it.
I recommend refactoring your code to instantiate the SPSite object outside of
I'm assuming the purpose of this code is to let you provide the name you want for your subsite, and the code will give you an available name based on whether the provided name already exists. It does this by appending/incrementing a suffix to the provided name whenever there is a naming collision. If this is the case, your
FormatMyHomieURL() function should return temp sans the mainSite prefix, not subsiteName + i. To walk through an example briefly, let's say you want to name a subsite
AwesomeWeb. The code checks and AwesomeWeb doesn't exist, so it returns subsiteName + i which resolves to AwesomeWeb0. You then go ahead and create a subsite named AwesomeWeb0 and everyone is happy. One month later, you come along and decide to make a subsite named AwesomeWeb. The code checks and sure enough, AwesomeWeb doesn't exist (since you created AwesomeWeb0 last time), so the code returns subsiteName + i which again resolves to AwesomeWeb0. Now you're going to run into a naming collision.Reduce the number of instantiated SPSite objects
You are only ever dealing with a single SPSite object in this code (the parent site that you are checking for subsites). Therefore, you don't really need to create and dispose of a new SPSite object in every iteration of your loop just to grab a subsite from it.
I recommend refactoring your code to instantiate the SPSite object outside of
SubwebExists() and pass it in to SubwebExists() as a parameter, along with the name of the target subweb temp.Context
StackExchange Code Review Q#56019, answer score: 4
Revisions (0)
No revisions yet.