Friday, June 19, 2009

Using SPWebApplicationBuilder class for creating Web Application programmatically

Hi All,

We all know that the Web Application is the main container for all site collections. Ultimately it is the whole and sole of all sites created later on.

So in this article, we will learn how to create Web Application programmatically with SPWebApplicationBuilder Class. When we instantiate the object of SPWebApplicationBuilder class, it automatically provides the default values for the required settings.

However there are many other properties that you can set. These are the same properties that we set from UI in central administration at the time of creating web application.

Here is a list of some properties. You can find all properties from of course MSDN.

ApplicationPoolId. – GUID of the application pool that will get created for web application.

ApplicationPoolUsername. – User name of the windows account under whom web application will run.

ApplicationPoolPassword- Password of the windows account under whom web application will run.

CreateNewDatabase. – Gets or sets the Boolean value indicating to create the database for web application or not.

DatabaseName – Name of the database

Port – Port number on which web application runs

Here is a way we will proceed to create web application. First instantiate the SPWebApplicationBuilder class object.

Then you can set different properties. Here I am setting only port number.

Then get the SPWebApplication class object by calling the create method on SPWebApplicationBuilder object.

Here is a short code.

SPWebApplicationBuilder objWebAppBuild= new SPWebApplicationBuilder(SPFarm.Local);
objWebAppBuild.Port = 8880;
SPWebApplication objWebApp= objWebAppBuild.Create();
objWebApp.Provision();


Just to check one important thing is under which account code is running.

That’s it. Your job is done.

1 comment:

Anonymous said...

how to set the url for this webapplication




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com