This documentation is archived and is not being maintained.

SPServiceInstance Class

Windows SharePoint Services 3

Represents a single instance of a service that runs on a server.

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

[GuidAttribute("983C4B09-FBC0-4f4b-92BC-42FABF556ED5")]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPServiceInstance : SPPersistedUpgradableObject

The SPServiceInstance class associates services with servers. Use the Service property to get the SPService object that contains the farm-wide settings that apply to the service that this instance implements. Use the Server property to get the SPServer object on which this instance is installed.

To return an SPServiceInstanceCollection object that represents the service instances that currently run on a server, use either the ServiceInstances property of the SPServer class, or the SPServiceInstanceCollection constructor. To return an SPServiceInstanceDependencyCollection object that represents the service instances that are dependent upon a service, use the Instances property of the SPService class.

Use an indexer to return a single service instance from the collection. For example, if the collection is assigned to a variable named myServiceInstances, use myServiceInstances[index] in C#, or myServiceInstances(index) in Visual Basic, where index is either the name or the GUID that identifies the service instance.

The following example displays the server name and version numbers for all search service instances with a status of Online. The example requires using directives (Imports in Visual Basic) for both the Microsoft.SharePoint.Administration and Microsoft.SharePoint.Search.Administration namespaces.

SPSearchService searchService = SPFarm.Local.Services.GetValue<SPSearchService>("SPSearch");

if (searchService != null)
{
   SPServiceInstanceDependencyCollection instances = searchService.Instances;

   foreach (SPServiceInstance serviceInstance in instances)
   {
      if (serviceInstance.Status != SPObjectStatus.Online)
      {
         continue;
      }
      else
      {
         string serverName = 
            SPEncode.HtmlEncode(serviceInstance.Server.DisplayName);
         Response.Write(serverName + " == " + 
            serviceInstance.Version.ToString() + "<BR>");
      }
   }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: