Client-side SharePoint PowerShell–Feature Activate/DeActivate

# Include SPPS Import-Module .\spps.psm1 # Setup SPPS Initialize-SPPS -siteURL "https://example.sharepoint.com/" -online $true -username "sitecollectionadmin@example.onmicrosoft.com" -password "password" # Activate Publishing Site Feature Activate-Feature -featureId "f6924d36-2fa8-4f0b-b16d-06b7250180fa" -force $false -featureDefinitionScope "Site" #Activate Publishing Web Feature Activate-Feature -featureId "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" -force $false -featureDefinitionScope "Web"

https://sharepointpowershell.codeplex.com/

An Easy Approach to test Windows Service

Recently I involved in a project where I had to write a windows service to interact with SharePoint portal for generating alerts and various processes. The service is called “AlertingEngineHostingService”. For testing the service I found that the following code snippet (reflection approach) quite handy as it allows you to rapidly perform fixes and tests without having to deploy as usual.

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Reflection;

namespace TestWindowsService
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceBase[] servicesToRun;

            servicesToRun = new ServiceBase[]  
                {  
                        new AlertingEngineHostingService()  
                };

            if (Environment.UserInteractive)
            {
                Type type = typeof(ServiceBase);
                BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
                MethodInfo method = type.GetMethod("OnStart", flags);

                foreach (ServiceBase service in servicesToRun)
                {
                    method.Invoke(service, new object[] { args });
                }

                Console.WriteLine("Press any key to exit");
                Console.Read();

                MethodInfo stopMethod = type.GetMethod("OnStop", flags);

                foreach (ServiceBase service in servicesToRun)
                {
                    stopMethod.Invoke(service, new object[] { args });
                }
            }
        }
    }
}

Jan 09, 2012 Update: ————————————-

Another alternative that could be used is, have the following precompile statement in the Program.cs file:

#if DEBUG
             AlertingEngineHostingService srv = null;
            try
            {
                srv = new AlertingEngineHostingService();
                srv.StartAsProcess();
            }
            catch (System.Exception)
            {
                if (srv != null)
                    srv.WriteLogEntry();

            }
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new AlertingEngineHostingService() 
            };
            ServiceBase.Run(ServicesToRun);
#endif

And have the following debug helper method in the service class (in my case it was – AlertingEngineHostingService.cs):

public void StartAsProcess()
{           // your code here
            // ....
            OnEventTrigger(null, null);
}