0

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/

2 Answers 2

2

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!

5
  • 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
    Commented Sep 6, 2013 at 9:53
  • can you post some of the code to see whats going on? the code should work!
    – Ali Jafer
    Commented Sep 6, 2013 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
    Commented Sep 6, 2013 at 11:46
  • Sorry, it works now I'm just slow. Thanks all the help you're a champion.
    – john_acton
    Commented Sep 6, 2013 at 12:22
  • thats no probs happy to help ;)
    – Ali Jafer
    Commented Sep 6, 2013 at 13:09
0

Try to add the code in EventReceiver on FeatureActivating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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