locked
Start a SPService Programmatically RRS feed

  • Question

  • Hello,

    Is there any way in SP2010, to Start/Stop a SPService using Sharepoint object Model?? Or any way that i can see the Status of the SPService w/o the use of Central Administration??

    SPObjectStatus doesn't show actually whether the service is started or stopped.

    Any help???


    Shrikant Datke SharePoint Server 2010 MCITP| Developer| Administrator Blog: http://shrikantdatke.blogspot.com

    Wednesday, June 13, 2012 7:19 AM

Answers

  • Check out http://msdn.microsoft.com/en-us/library/ee537799.aspx and use the Provision() method of SPServiceInstance objects.

    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com


    Wednesday, June 13, 2012 7:40 AM
  • You're looking at the wrong level. It will show all SPService objects are online, which is correct, but you need to look at the SPServiceInstance level, like this:

    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;
                }
            }
        }
    }
    
    


    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

    Thursday, June 14, 2012 1:29 PM

All replies

  • Hello!

    Write your question on this forum thread.


    My contributions: SharePoint 2010 Solution Installer

    Wednesday, June 13, 2012 7:33 AM
  • Check out http://msdn.microsoft.com/en-us/library/ee537799.aspx and use the Provision() method of SPServiceInstance objects.

    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com


    Wednesday, June 13, 2012 7:40 AM
  • Extending this question:-

    "How can i get the Status whether my service is Started or Stopped?? " and not "Online/Offline" which i get  from SPObjectStatus

    Any help?


    Shrikant Datke SharePoint Server 2010 MCITP| Developer| Administrator Blog: http://shrikantdatke.blogspot.com

    Thursday, June 14, 2012 12:11 PM
  • According to my tests, SPObjectStatus.Online equates to Started, SPObjectStatus.Disabled to Stopped.

    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

    Thursday, June 14, 2012 12:34 PM
  • Strange...

    In my case it shows all Services Online :(


    Shrikant Datke SharePoint Server 2010 MCITP| Developer| Administrator Blog: http://shrikantdatke.blogspot.com

    Thursday, June 14, 2012 12:55 PM
  • You're looking at the wrong level. It will show all SPService objects are online, which is correct, but you need to look at the SPServiceInstance level, like this:

    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;
                }
            }
        }
    }
    
    


    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

    Thursday, June 14, 2012 1:29 PM
  • Great Amazing!!

    I was not aware of this ServiceInstance.Status fact i was hanging around SPService.Status. Very much thanks Margriet :)


    Shrikant Datke SharePoint Server 2010 MCITP| Developer| Administrator Blog: http://shrikantdatke.blogspot.com

    Friday, June 15, 2012 6:48 AM
  • One more question Margriet

    Can make this functionality available for none Admin user? Because i tried doing this but i received an error "Failed to connect to an IPC port:access denied"

    Also i tried doing it with System Account but same error...


    Shrikant Datke SharePoint Server 2010 MCITP| Developer| Administrator Blog: http://shrikantdatke.blogspot.com

    Tuesday, June 19, 2012 6:27 AM
  • Just run the code using the SPSecurity.RunWithElevatedPrivileges method, it executes the method with full control rights even if the user does not otherwise have Full Control. It worked for me, here's 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
    			{
    				SPSecurity.RunWithElevatedPrivileges(delegate()
    				{
    
    					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;
    			}
    		}
    	}
    }
    


    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

    Tuesday, June 19, 2012 7:02 AM