How to identify the SharePoint exception when trying to create a new site

899 views Asked by At

Ok,

This seems stupid but I figured I would ask because there is a lot of expertise to tap into here and I will probably learn a good bit from the answers.

I have a service center panel for creating sites. If site creation fails, I would like to delete the site. EXCEPT if the exception is because another site already exists at that URL.

I currently get the following and could easily check for contained text but would like a more solid approach. (e.g. looking for an exception ID or something to this effect.)

Another site already exists at http://server:80/sites/xxxxxxxx. Delete this site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the path you originally specified.

2

There are 2 answers

2
TroyBramley On BEST ANSWER

Why don't you check to see if the site exists first before trying to create it?

0
Rikard Uppström On

As TrovB30 says, checking if it exists before trying to create is probably the best way of doing that.

I assume you have a reference to a SPSiteCollection object or a SPWebApplication object? In that case I would probably loop through it to see if there already exists one. This may seem tedious but will probably be more efficient than a try-catch procedure:

private bool SiteExists(SPWebApplication webApp, string siteUrl)
    {            
        var sites = webApp.Sites;
        //Add slash to enable comparison
        siteUrl = "/" + siteUrl;
        foreach (SPSite site in sites)
        {
            if (site.ServerRelativeUrl.Equals(siteUrl) == true)
            {
                return true;
            }
        }
        return false;
    }