Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created a visual webpart that creates site collections programmatically. It works through SelfServiceCreateSite and does its job flawlessly but for one thing.

I need to check if the site collections that users are creating with the webpart already exist within the given webpart and I don't know how to do that. Is there anyone here that could give me some help?

I've read this blog, but I can't get it to work for me.

http://nikspatel.wordpress.com/2011/11/30/code-snippet-how-to-check-if-sharepoint-site-collection-or-sub-site-exists/

share|improve this question
add comment

2 Answers

use

spsite.exists();

http://msdn.microsoft.com/en-us/library/ms428778(v=office.14).aspx

//if site doesnt exist
if (!SPSite.Exists("http://google.com"))
{
   //make site
}

if it doesnt like the above you might need to have the string as an actual uri like so:

//if site doesnt exist
Uri url = new Uri("http://google.com");
if (!SPSite.Exists(url))
{
        //make site
}
else
{
        this.Controls.Add(new LiteralControl("<div style='color:Red'><p><strong>Sorry site already exists!</strong></p></div>"));
}

EDIT

just to clarify:

            Uri url = new Uri("http://google.com");
            if (!SPSite.Exists(url))
            {
                Console.Out.Write("site doesnt exists, create me");
                Console.Out.WriteLine();
                Console.Read();
            }
            else
            {
                Console.Out.Write("site exists sorry you cant!");
                Console.Out.WriteLine();
                Console.Read();
            }

thats what i used to test and works just fine in a console app! I need to see some code to see where your going wrong, how your calling this code and whats going in within the if function! how your displaying the message!

share|improve this answer
    
I will try this and get back to you, thanks alot. –  john_acton Sep 6 '13 at 8:42
    
I just keep getting the exception instead of getting the label that I put put out there to say "this site is already in use please pick another name". Microsoft.SharePoint.SPException: The specified name is already in use. Please try again with a new name. –  john_acton Sep 6 '13 at 9:53
    
can you post some of the code to see whats going on? the code should work! –  ali Sharepoint Sep 6 '13 at 10:12
    
I would post code if I could get the formatting to swallow my code. I tried putting the code in <blink> in <html> ... What am I missing? –  john_acton Sep 6 '13 at 11:46
    
Sorry, it works now I'm just slow. Thanks all the help you're a champion. –  john_acton Sep 6 '13 at 12:22
show 1 more comment

Try to add the code in EventReceiver on FeatureActivating.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.