Saturday, March 21, 2009

Copying configuration file to the root folder using Feature receiver

Scenario:
I usually use lot of config files but WSP doesn't support copying configuration file to the root folder.

Solution:
Here's the feature receiver.

Code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

using System.IO;

namespace AwesomeFeatureSet
{

public class ConfigFileMoverReceiver : SPFeatureReceiver
{
// Methods
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication application = (SPWebApplication) properties.get_Feature().get_Parent();
foreach (SPUrlZone zone in application.get_IisSettings().Keys)
{
SPIisSettings settings = application.get_IisSettings()[zone];
string sourceFileName = Path.Combine(SPUtility.GetGenericSetupPath("TEMPLATE"), @"FEATURES\ConfigFileMover\App.config");
string destFileName = Path.Combine(settings.get_Path().ToString(), @"App.config");
File.Copy(sourceFileName, destFileName, true);
}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication application = (SPWebApplication) properties.get_Feature().get_Parent();
foreach (SPUrlZone zone in application.get_IisSettings().Keys)
{
SPIisSettings settings = application.get_IisSettings()[zone];
string path = Path.Combine(settings.get_Path().ToString(), @"App.config");
try
{
File.Delete(path);
continue;
}
catch (Exception)
{
continue;
}
}
}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}

Article:

0 comments: