Wednesday, October 28, 2009

Using Feature Properties and its benefits

Hi all,

While working for one of my project, I have to use feature properties. Let me introduce why we need "Feature Properties"

Q. What is exactly is a "Feature property" and why its useful?
Ans. Well, we know that feaure.xml file has certain elements as follows:

i) ActivationDependencies
ii) ElementManifests

Each element has different use. For e.g.

i) ActivationDependencies is to indicate prevents the current Feature from being activated unless the Feature with the specified ID is activated.

For instance,
        <activationdependency featureid="11111111-1111-1111-1111-111111111111">

ii) ElementManifests contains references to element manifests and element files that contain definitions for the Feature elements.

iii) Properties contains a list of the default values for Feature properties.


Benefits of feature properties:

1) Use of default values for storing the data.
Feature framework uses feature properties for storing the data values and its similar functionality of ASP.NET where web.config stores all data connections, global settings and default values.

In SharePoint, we can have exactly same functionality. For e.g.
We can develop a timer job to configuration settings

<Feature xmlns=""...>
<Properties xmlns="">
<!-- The name of the timer job (will appear in Central Admin >> ). -->
<Property Key="JobTitle" Value="Aroh's Timer Job"/>
<!-- Connection String that the TimerJob will use -->
<Property Key="ConnString" Value="ConnectionString..."/>
<!-- The schedule to run the job in (24 hour format) -->
<Property Key="Schedule" Value="daily at 02:00:00"/>

We have write a method to tap FeatureActivated method:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
// Get all the properties from the feature.xml file
string jobTitle = properties.Feature.Properties["JobTitle"].Value;
string connString = properties.Feature.Properties["ConnString"].Value;
string dailySchedule = properties.Feature.Properties["Schedule"].Value;

// Create the job.
CustomTimerJob customTimerJob = new CustomTimerJob((SPWebApplication)properties.Feature.Parent, jobTitle);

// Set the properties for the job to run properly
customTimerJob.Properties.Add("ConnString", connString);
customTimerJob.Properties.Add("Schedule", dailySchedule);

// Set the schedule
SPSchedule mainSchedule = SPSchedule.FromString(dailySchedule);
customTimerJob.Schedule = mainSchedule;

// Activate the schedule

Later on, we use timer's job "Execute" method to use feature properties

public override void Execute(Guid targetInstanceId)
// Set up configuration values
string connString = this.Properties["ConnString"] as string;

Another example is where we fixed URLs have to be used.
For e.g. We have a URL:

Now, we can use it in a feature properties as follows:

<Feature Id="40fefb9d-c00a-48fc-b5c8-98ed0263eb0f"
Title="[IT] Alert Notification Job"
Description="Description for AlertNotificationInstaller"
ReceiverAssembly="Company.SystemChecklists, Version=, Culture=neutral, PublicKeyToken=fbfd703f61794815"
<Property Key ="ApacSite" Value="apac"/>
<Property Key ="ToolsSite" Value="Tools"/>
<ElementManifest Location="elements.xml"/>

And in the execute method (of timer job) we can use feature properties:

public override void Execute(Guid contentDbId)
siteName = this.Properties["ApacSite"].ToString();
webName = this.Properties["ToolsSite"].ToString();

// get a reference to the current web application
SPWebApplication webApplication = this.Parent as SPWebApplication;

// get a reference to the Site Collection in the content database
using (SPSite spSite = contentDb.Sites[siteName])
// get a reference to the Web
using (SPWeb spWebRoot = spSite.OpenWeb(this.Properties["ToolsSite"].ToString()))

2) Another use of feature properties is that XML files is not complied.
Therefore, if can to alter the URL from http://testserver/apac/tools to http://testserver/apac/IT/tools, it can be very easily changed in the feature properties itself and just upgrade the solution.

There is NO need to alter the DLL again.

Some important points to be noted:

>> If you want the timer job on Web scope instead on Web Application scope, first DEACTIVATE the feature
>> Alter feature.xml
>> ACTIVATE the feature.

If you don't deactivate the feature and directly change the feature.xml, then your timer job and your SharePoint instance will be screwed up. Solution is there in my post as well.


No comments:

How to display Sharegate administrative dynamic reports via Power BI on SharePoint Online

After working in the SharePoint domain for a couple of years as SharePoint consultant , I am fortunate enough to help customers from man...