Install Features Programmatically in SharePoint

Generally, we use STSADM command line to install features by “STSADM -o installfeature -name FeatureName” or Install-SPFeature PowerShell Cmdlet. At times, we may have to execute stsadm programmatically to install a Feature. Say for e.g. from console applications or from another feature activation code.  Here is how it can be achieved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;

namespace InstallFeature
    class Program
        static void Main(string[] args)
            string site;
            if (args.Length == 0)
                Console.WriteLine("Enter the Web Application URL:");
                site = Console.ReadLine();
                site = args[0];
            SPSite tmpRoot = new SPSite(site);

           //Get the particular Feature, if installed already
            SPFarm farm = tmpRoot.WebApplication.Farm;
            SPFeatureDefinitionCollection features = farm.FeatureDefinitions;
            SPFeatureDefinition feature = features[new Guid("05BA4E54-C1E9-4244-8276-06451EBBB260")];

            if (feature == null)    //Feature is not installed!
                // install the feature
                Process oProcess = new Process();
                ProcessStartInfo oProcInfo = new ProcessStartInfo();

                // Get the STSADM command's path
                string commonFilesPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonProgramFiles);
                oProcInfo.FileName = commonFilesPath + @"\Microsoft Shared\web server extensions\12\BIN\" + "stsadm.exe ";

                oProcInfo.Arguments = " -o installfeature -name Crescent.Support.Link -force";
                oProcInfo.WindowStyle = ProcessWindowStyle.Hidden; 
                oProcInfo.UseShellExecute = true;

                oProcess.StartInfo = oProcInfo;
                //execute the stsadm command.


Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *