0

I wonder how can I create SPWebApplication (SharePoint 2010 and 2013 ) programmatically? Do I need a Remote Administration using Sharepoint 2010 Management Shell or there is c# code.

best regards

3

Example code using C#.Net

// 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(); 

Source

|improve this answer|||||
2

Easiest way is to use PowerShell

New-SPWebApplication

Taken from Technet

Creates a new Web application specified by the Name parameter. The user specified by the DatabaseCredentials parameter must be a member of the dbcreator fixed server role on the database server.

This example creates a new Web application by using an internal host header of sharepoint.contoso.com and a public URL of https://www.contoso.com

$ap = New-SPAuthenticationProvider
New-SPWebApplication -Name "Contoso Internet Site" -Port 443 -HostHeader sharepoint.contoso.com -URL "https://www.contoso.com" -ApplicationPool "ContosoAppPool" -ApplicationPoolAccount (Get-SPManagedAccount "DOMAIN\jdoe") -AuthenticationProvider $ap -SecureSocketsLayer
|improve this answer|||||
  • seems like we were typing at the same time :-) – Nadeem Yousuf Mar 26 '14 at 12:03
  • Indeed we do ;-) – Remko Mar 26 '14 at 12:03
2

You can create it in both C# and PowerShell. Example of PS command:

New-SPWebApplication -Name "Contoso Internet Site" -Port 443 -HostHeader sharepoint.contoso.com -URL "https://www.contoso.com" -ApplicationPool "ContosoAppPool" -ApplicationPoolAccount (Get-SPManagedAccount "DOMAIN\jdoe") -AuthenticationProvider $ap -SecureSocketsLayer

See more at http://technet.microsoft.com/en-us/library/ff607931(v=office.15).aspx

The C# code is already provided by @Karthik Jaganathan

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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