How to Change the IIS Path for a SharePoint Web Application

When creating a new web application in SharePoint, you choose the physical location of where the IIS website will be created. Once the application is born, however, there is no longer an option to change this path in Central Administration. You can very easily change it in IIS (by going to your websites Properties -> Home Directory -> Local Path), but once this is done, you will start running into trouble, as solution deployments will point to the web.config file at the old location.

Three quick steps need taken to successfully move the web application:

  1. Copy the current directory’s contents into your new directory
  2. Make the change to the website’s local path in IIS (as described above)
  3. Update the SharePoint web application to point to the new path

Note: If you have multiple servers in your farm, steps 1 and 2 will need made on each web server, but step 3 will only need done once.

The third step can be done without having to extend the site. Here are a couple options.

using System;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
 
namespace IisPath {

  class Program {
  
    static void Main(string args) {
    
      try {
      
        //Open the site your web application hosts
        using(SPSite site = new SPSite("http://yoursite.com")) {
        
 
          //Grab a reference to the site's web application's IIS Settings
          //Change SPUrlZone if application is not in default zone
          SPIisSettings iisSettings = site.WebApplication.IisSettings[SPUrlZone.Default];
 
          //Point to your new path
          iisSettings.Path = new System.IO.DirectoryInfo(@"D:\wss\VirtualDirectories\yoursite.com");
 
          //Call the Update method of the web application
          site.WebApplication.Update();
 
        
      }
      catch(Exception ex) {
      
        Console.WriteLine(ex.Message);
        Console.ReadLine();
      }
    }

      
}
System.Reflection.Assembly::LoadWithPartialName("Microsoft.SharePoint")
 
#Point to the site your web application hosts to grab a reference to the web application
$SPSite = new-object Microsoft.SharePoint.SPSite("http://yoursite.com")
$WebApp = $SPSite.WebApplication
 
#Change SPUrlZone if not in default zone
$IISSettings = $WebApp.IisSettings[Microsoft.SharePoint.Administration.SPUrlZone::Default]
 
#Point to your new directory
$IISSettings.Path = "D:\wss\VirtualDirectories\yoursite.com"
 
#Update Web Application
$WebApp.Update()
$SPSite.Dispose()

I hope this has been helpful, and as always, thank you for reading.

Cheers,
Matt

Matt Jimison

Microsoft 365 Geek - Husband, father, lover of basketball, football, smoking / grilling, music, movies, video games, and craft beer!

Leave a Reply

Your email address will not be published. Required fields are marked *