Thursday, August 16, 2012

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.


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...