SharePoint Dragons

Nikander & Margriet on SharePoint

Starting and Stopping a service instance

If you want to start and stop services programmatically, you can use the Provision() method of an SPServiceInstance object to start it, and its Unprovision() method to stop it. You can find details about this at http://msdn.microsoft.com/en-us/library/ee537799.aspx

The following C# code demonstrates, using the server object model run from a console app, how to start and stop the Access service status. You can check the Services on Server page (in our case, http://astro:40000/_admin/Server.aspx ) to see what happens when you run the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SPServiceCollection services = SPFarm.Local.Services;
                foreach (SPService service in services)
                {
                    Console.WriteLine(“SPService {0} status: {1}”, service.Name, service.Status);

                    foreach (SPServiceApplication application in service.Applications)
                    {
                        Console.WriteLine(“SPServiceApplication {0} Status: {1}”, application.Name, application.Status);
                        if (application.Name.ToLower().Contains(“access”))
                        {                           
                            Console.WriteLine(“Toggling Access service instances”);
                            foreach (SPServiceInstance serviceInstance in application.ServiceInstances)
                            {
                                Console.WriteLine(“SPServiceInstance {0} Status: {1}”, serviceInstance.Name, serviceInstance.Status);
                                if (serviceInstance.Status == SPObjectStatus.Online)
                                {
                                    Console.WriteLine(“Stopping service instance”);
                                    serviceInstance.Unprovision();
                                }
                                else if (serviceInstance.Status == SPObjectStatus.Disabled)
                                {
                                    Console.WriteLine(“Starting service instance”);
                                    serviceInstance.Provision();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                var errMsg = err.Message;
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: