Feature Installation & Activation
Problem
We have discussed automated deployment in another post, now let’s discuss how we can install a feature. Doing an automated feature installation contains some problems if we use SDK. For these problems, please keep on reading.
Solution
The following code snippet should install a feature on the form.
SPWebApplication app = SPWebApplication.Lookup(new Uri(DefaultZone));
SPFeatureDefinitionCollection col = farm.FeatureDefinitions;
SPFeatureDefinition def = col.Add(featurefile, id, true);
app.Features.Add(id, true);
The above code installs the feature but it do not activate the feature even if we try to activate it. The activation step needs to be performed manually.
If we wanted to somehow perform this step, we can do it using the following code (this does not use the SDK):
ProcessStartInfo pStartInfo = new ProcessStartInfo();
//specify gacutil.exe whith which to start the process
pStartInfo.FileName = @”.\stsadm.exe”;
pStartInfo.Arguments = string.Format(“-o installfeature -filename {0} -force”, featureName);
pStartInfo.UseShellExecute = false;
pStartInfo.CreateNoWindow = true;
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//start the process
Process process = Process.Start(pStartInfo);
//wait till the process completes
process.WaitForExit();
The above code will install the feature on the farm where this code is being executed. If we wanted to activate it as well, we can write down the same statements with activatefeature switch.
Recent Discussions