Welcome to EMC Consulting Blogs Sign in | Join | Help

Kalpesh Prajapati

Programmatically creating Web Application, Site Collection and Web in MOSS 2007

Classes and Object used…

·         SPWebApplicationBuilder: Creates an SPWebApplication object, providing default settings for all the required values and allowing the caller to change only those properties that need values different from the default

·         SPFarm: Represents a Windows SharePoint Services farm. To access the current server farm object, you can use members on SPFarm.Local.

·         SPWebApplication: Represents an Internet Information Services (IIS) load-balanced Web application that is installed on the server farm.

·         SPSiteCollection : Represents a collection of SPSite objects, or site collections.

·         SPSite : Represents a collection of sites on a virtual server, including a top-level site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections.

·         System.Security.SecureString: Represents text that should be kept confidential. The text is encrypted for privacy when being used, and deleted from computer memory when no longer needed. A SecureString object is similar to a String object in that it has a text value. However, the value of a SecureString object is automatically encrypted, can be modified until your application marks it as read-only, and can be deleted from computer memory by either your application or the .NET Framework garbage collector

 

Let’s start...

 

Creating web Application at Port no 2007 ( Assuming user spuser with password Password is present in system administrator group)

SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);

SPWebApplication newApplication;
int myPort = 2007;

webAppBuilder.Port = myPort;

webAppBuilder.ApplicationPoolId = "site-appol"; // application pool

webAppBuilder.ApplicationPoolUsername = webAppBuilder.DefaultZoneUri.Host + \\spuser;

System.Security.SecureString password = new System.Security.SecureString();               

string strName = "Password";               

char[] pass = strName.ToCharArray();               

foreach (char c in pass)               

    password.AppendChar(c);              

webAppBuilder.ApplicationPoolPassword = password;

webAppBuilder.CreateNewDatabase = true; // Create new database           

webAppBuilder.DatabaseName = "wss_site2007_content";    // database name           

webAppBuilder.DatabaseServer = webAppBuilder.DefaultZoneUri.Host;  //Host name/computer name          

webAppBuilder.UseNTLMExclusively = true;  // Use NTLM authentication

newApplication = webAppBuilder.Create(); // Create new web application
newApplication.Provision();           //Provision it into web farm    

 

Creating site Collection at root level with Blank site template

SPSite mySiteCollection = newApplication.Sites.Add("/",                       //Root

"Cgosite",              // site title

 "Conchango.com",             //description

1033,                         //language

"STS#1",                //Blank site template  

webAppBuilder.ApplicationPoolUsername, // Site owner

"Name",                 // Name

"name@name.com"); //Email

 

Creating web

mySiteCollection.AllWebs.Add("/webname");

 

Activating publishing feature at site collection
mySiteCollection.Features.Add(SPFarm.Local.FeatureDefinitions["PublishingSite"].Id);

SPFarm.Local.FeatureDefinitions["PublishingSite"].Provision()

 

Close Site Collection
mySiteCollection.Close();

Published 03 September 2007 10:15 by kalpesh.prajapati
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Antonio said:

Hi Kalpesh, great article!

I was searching for extending a web application programmatically...

Do you know how to do this?

Hello

October 11, 2007 17:02
 

3amzooo said:

Nice article mate...I'm trying to use a custom site template to create the new site...but its not working...how can i make it work...do you have any idea?

November 25, 2007 09:38
 

kondepati said:

kalpesh,

i am getting a problem at the site creation, it was showing the error:

object reference not set to instance of the object.

December 19, 2007 06:07
 

Kalpesh said:

kondepati ,

Could you please show full exception detail, from where it is being thrown?

Kalpesh

December 19, 2007 06:12
 

kondepati said:

SPSite mySiteCollection = newApplication.Sites.Add("/", "CustomSite","site Created Programatically",1033,"STS#0",moss,administrator,"moss@sample.com");

it is throwing an error at this line

even though I declared UseNTLMExclusively to true, it is showing false.

what could be the reason?

December 19, 2007 08:21
 

Kalpesh said:

Thanks for code line. This does not show us which object is null.

Did you try same code as in this blog? is that working with change in password and username?

December 19, 2007 08:30
 

kondepati said:

Null Reference Exception was unhandled was the exception at that line

December 19, 2007 08:34
 

Ricardo Flores said:

i try this code and it work perfect, but if you have a load-balanced server, is not creating the iis web site and the Inetpub folder on the 2nd server. any idea?

i tryed creating a site on central administration and it did created both servers iss the same.

January 18, 2008 21:01
 

DeepakB said:

is there any way to create two content database specific to the web application and then create two site collections beneath web application and make the setting in such a way that these two site collections will point to the seperate content databases?

Thanks,

Deepak

February 25, 2008 08:48
 

Kalpesh said:

Deepak, I think, You can have only one content database per web application

March 3, 2008 08:28
 

Peruri said:

WebApplication.Sites.Add has overload to create new database for each site collection if wanted.  

Check the overloads for the Sites.Add method.

March 13, 2008 02:03
 

deepakb said:

Nope,  We can have more than one content databases inside one web application. Even we can point out individual site collection to the content database. Peruri is right...i did the same thing.

March 25, 2008 11:35
 

sharepoint create site collection no web application said:

June 20, 2008 15:55
 

sanjay singh said:

Hi Kalpesh,

I followed all the procedure mentioned above.

But my site collection is not getting created

It simply says The web page cannot be found

Could you please help me.

Thanks & Regards

Sanjay Singh

June 24, 2008 11:11
 

Create web applications and site collection throws coding « Ilaiyaraja’s Blog said:

December 18, 2008 08:04
 

Seema said:

Hi Kalpesh,

                I followed all the procedure mentioned above.

                But i am not able to create web applictaion its throwing

                a null reference  exception  at-

                newApplication = webAppBuilder.Create();

               Could you please help me.

             Thanks & Regards'

              Seema Singh

December 29, 2008 10:55
 

sidhartha said:

even i am gettin the same error as seema is getting.can any pls help me out.

December 30, 2008 09:18
 

Seema said:

Hi Sidhartha,

                       try creating your web application in-

                    SPSecurity.RunWithElevatedPrivileges(delegate()

       {

\\ web application code  here  

       });

i  hope it will work.Because  your null  reference error is because  of some security settings.

January 6, 2009 04:34
 

pooja mehta said:

its really nice article..

can u tell me how the workflow create from the developing era??

January 23, 2009 18:52
 

Karthikeyan said:

Really a good one, please keep posting articles on sharepoint 2007.

Thanks,

Karthikeyan.

March 12, 2009 07:43
 

AAfroze said:

Hi guys,

I understand the object model, but I dont know where to type this code I am new to sharepoint development I have Visual Studio 2005, SharePoint Server 2007, WSS 3.0, VSeWSS extension installed on the machine could you please let me where or which template I should use to write and execute the code so that I can create new site and sitecollections programmatically plz

a_afroze@yahoo.com

Thanks,

April 28, 2009 15:15
 

Sid said:

Hi,

Is there a way to access the site collections under Wildcard inclusion programmatically?

I have a Site Collection http://servername/<wildcardDefinedManagedPath>/<SiteCollectionURL>. How can I access this through code?

Ultimate goal is to access mySites without using the UserProfileManager, as it is not working well for me.

Thanks In Advance,

Sid

July 8, 2009 15:02
 

Murlidhar said:

Hi Kalpesh,

i too have written same code to create site collection, but the strage thing is we don't find Member, owners and visitors site groups created in new site collection created using this code.

help me out, this need to be fixed ASAP.

October 8, 2009 08:42
 

Murlidhar said:

please mail me

Murlidhar.chawhan@ubs.com, if you find any solution for this.

i had even tried with elevated priviledges and even with SP admin webservices, sitll new site collection dont create above mentioned site groups.

October 8, 2009 08:44
 

Gautam said:

Hello Kalpesh,

I created a Windows Application and followed this article:

I debugged the code and the new web application,site collection and web got created properly and I am able to see the newly created web application in the Central Admin Site along with the newly created Content Database.

But the web application,site collection and web cannot be browsed in IE...

Also, the new IIS Website is NOT listed in the IIS Manager(INETMGR) and is also there is NO folder created in: "C:\Inetpub\wwwroot\wss\VirtualDirectories\34567"

(34567 is the port number).

I tried doing IISRESET many times.

Can you tell me why this is happening?

Thanks.

June 28, 2010 11:26
 

shariff said:

i used this code to create webapplication and site collection but i'm unable to browse the site. am i missing something...

September 23, 2010 11:34
 

shariff said:

and i get a page which says"Cannot connect to the configuration database."

September 23, 2010 11:35
 

BLOCKED SCRIPT {if%20(typeof(Page_ClientValidate)%20!=%20'function'%20||%20%20Page_ClientValidate())%20__doPostBack('_ctl0$_$_ctl0$_$_ctl0$_ctl0$bcr$_ctl0$_$form$_$btnSubmit','')} said:

BLOCKED SCRIPT {if%20(typeof(Page_ClientValidate)%20!=%20'function'%20||%20%20Page_ClientValidate())%20__doPostBack('_ctl0$_$_ctl0$_$_ctl0$_ctl0$bcr$_ctl0$_$form$_$btnSubmit','')}

June 6, 2011 11:39
 

BLOCKED SCRIPT {if%20(typeof(Page_ClientValidate)%20!=%20'function'%20||%20%20Page_ClientValidate())%20__doPostBack('_ctl0$_$_ctl0$_$_ctl0$_ctl0$bcr$_ctl0$_$form$_$btnSubmit','')} said:

Thanks for sharing your feedback! If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish - or longer if the blogger is moderating comments.

Leave a Comment

Name (required)

Your URL (optional)

Comments (requ

June 6, 2011 11:40
 

Gabe said:

Hi Kalpesh - Are there any specific configuration changes that need to be made within SharePoint to bind to the Admin.asmx from Visual Studio? I have been unable to do so.

Your help is appreciated.

Gabe

June 6, 2011 20:31
 

Ashley said:

You also may have to set the app pool account as well. Otherwise depending upon how your farm config is, you may get error while trying to browse to the site. It won't be able to connect to the config db. (Never tried on MOSS 2007, but  only in 2010)

           newApplication.ApplicationPool.CurrentIdentityType = IdentityType.SpecificUser;

           newApplication.ApplicationPool.Username = "mydomain\myuser";

           newApplication.ApplicationPool.SetPassword(webAppPassword);

           newApplication.ApplicationPool.Update();

           newApplication.ApplicationPool.Provision();

July 8, 2011 23:54

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems