Tuesday, September 13, 2011

How to create SPWebApplication (SharePoint Web Application) programmatically?

SPWebApplication programmatically

For creating SharePoint Web Application programmatically you first need to enable Remote Administration using Sharepoint 2010 Management Shell. Open Sharepoint 2010 Management Shell and type following commands one at a time

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") > $null



$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$contentService.RemoteAdministratorAccessDenied = $false
$contentService.Update()



Below is the code to create SPWebApplication programmatically:

SPWebApplication newApplication;

SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);

webAppBuilder.Port = 80;
webAppBuilder.RootDirectory = new DirectoryInfo("C:\intepub\wwwroot\wss\80");

webAppBuilder.ApplicationPoolId = "My First site on port 80";
webAppBuilder.ApplicationPoolUsername = "domain\ServiceAccount";
webAppBuilder.ApplicationPoolPassword = StringToSecureString("password");


webAppBuilder.ServerComment = "My First Site";
webAppBuilder.CreateNewDatabase = true;
webAppBuilder.DatabaseServer = "sql01"; // DB server name
webAppBuilder.DatabaseName = "WSS_Content_MyFirstSite";// DB Name
webAppBuilder.HostHeader = "contoso.com"; //if any


webAppBuilder.UseNTLMExclusively = true;  // authentication provider for NTLM
webAppBuilder.AllowAnonymousAccess = true; // anonymous access permission
                                   
// Finally create web application
newApplication = webAppBuilder.Create();


//Enable Claims
newApplication.UseClaimsAuthentication = true;
newApplication.Update();
newApplication.Provision();
 

1 comment:

  1. Good post. But Management shell giving error while running commands.

    ReplyDelete

Popular Posts