Tuesday, August 25, 2009

Create SharePoint Web Application Programmatically

Generally web applications in SharePoint are created using the out-of-box functionality provided within Central Administration. But because of some client's needs there might be a requirement of creating these web applications within a custom utility solution. This functionality can be achieved programmatically using Microsoft SharePoint APIs. By this, one can easily create a web application & associate a content database to it, register the web application in the IIS server, create an associated application pool and define different credentials for the web aplication.

Example code using C#.Net

// namespace required
using System.IO;
using Microsoft.SharePoint.Administration;


// Create new object of SPWebApplication & SPWebApplicationBuilder class
SPWebApplication newApplication;
SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);

// Set necessary values to webAppBuilder object

webAppBuilder.Port = portNo;
webAppBuilder.RootDirectory = new DirectoryInfo(@"D:\projects\TestWebSite3030");
webAppBuilder.ApplicationPoolId = appPoolId;
webAppBuilder.ApplicationPoolUsername = appPoolUserName;
webAppBuilder.ApplicationPoolPassword = password;
webAppBuilder.CreateNewDatabase = true;
webAppBuilder.DatabaseServer = dbServerName; // DB server name
webAppBuilder.DatabaseName = dbName;// DB Name
if (isSQLAuth)
{
webAppBuilder.DatabaseUsername = dbUid; // dbUid is username of the DB sever
webAppBuilder.DatabasePassword = dbPwd; // dbpassword is password of the DB sever
}
webAppBuilder.UseNTLMExclusively = true; // authentication provider for NTLM
webAppBuilder.AllowAnonymousAccess = isAnonymous; // anonymous access permission

// Finally create web application
newApplication = webAppBuilder.Create();
newApplication.Provision();


I have written the same here also:Create SharePoint Web Application Programmatically

Samarendra Swain

No comments:

Post a Comment