Use of SPIisWebSite in SharePoint 2007

By Jatin Prajapati

How to use the SPIisWebSite object provided by SharePoint 2007.

Each SharePoint site runs on top of IIS, or an IIS web site is extended with extra functionaliy to support SharePoint features. An IIS web site is an entry point into IIS and is configured to listen incoming requrest on a specific port and specific IP address or with a unique host header. Configuration settings for each IIS web sites can be configured independently. For example, on site can be configured as Internet facing web site with anonymous access to every one, one site can be configured as Intranet site for internal work of organization with Windows authentication settings.

The SPIisWebSite object provided in SharePoint's Object Model, represents the instance of IIS web site.
Following are the properties exposed by this SPIisWebSite object.

Exists: This boolean property provides information for a specific IIS web site that it exists in the metabase of IIS or not.

InstanceId: This property of integer type, provides unique identification of IIS web site.

ServerBindings: This string arrary property of SPIisWebSite object specifies server binding information that is set for IIS web site.

ServerCommand: This property contains information about the display name of IIS web site represented by SPIisWebSite object.

ServerState: This read-only property says about the status of the IIS web site represented by SPIisWebSite object. This property is of SPIisServerState enumeration data type and its possible values are Continuing, Paused, Pausing, Started, Stopped and Stopping.

Following are the method exposed by SPIisWebSite object:

Provision: This method is used to provision (create) an IIS web site represented by SPIisWebSite object.

Unprovision: This method is used to remove / unprovision the IIS web site that is represented by SPIisWebSite object.

Update: Updates the changes and commits them against the registry.

The following code example, shows you how you can use the SPIisWebSite object.

<%@ Page Language="C#" Trace="False" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71E9BCE111E9429C" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    void Page_Load(object sender, EventArgs e)
           HttpContext context = HttpContext.Current;
            int instanceId = 1;
             int.TryParse(context.Request.ServerVariables["INSTANCE_ID"], out instanceId);
            SPIisWebSite site = new SPIisWebSite(instanceId);
             lbl.Text = "Exists: " + site.Exists.ToString() + "<br/>";
            lbl.Text += "InstanceId: " + site.InstanceId.ToString() + "<br/>";
            lbl.Text = "Server bindings served by this IIS web site<br/>";
            foreach (string serverBinding in site.ServerBindings)
                 lbl.Text += "    ServerBinding  " +
                    serverBinding + "<br/>";
            lbl.Text += "<br/>";
            lbl.Text += "Server comment: " + site.ServerComment + "<br/>";
            lbl.Text += "Server State: " + site.ServerState + "<br/>";
         catch (Exception ex)
             lbl.Text = ex.ToString();

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <h3>IIS Web site using SPIisWebSite object</h3>
    <h5 style="color:Red; text-transform:uppercase;">For this page to work you have to host this application in IIS.</h5>
    <asp:Label ID="lbl" runat="server"></asp:Label>

Use of SPIisWebSite in SharePoint 2007  (775 Views)