Sunday, July 5, 2009

Features and Solutions with WSS Object model

Hi All,

Today I am going to discuss about the features and solutions. I know you will ask me what is new in this. Well, this time we are going to talk about features and solutions in terms of object model.

We are going to discuss following things.

(1) Feature install and uninstall
(2) Feature activation and deactivation
(3) Solution install and uninstall
(4) Feature properties

We will discuss all in terms of object model. So let us start one by one. First we will take feature install and uninstall.

Now when we create feature and place it in to 12 hive folder and when we run the -o installfeature command, we actually installs the feature at farm level and when we run –o uninstallfeature command, we actually uninstalls the feature at farm level. Now how this can be done programmatically. So here is a way.

But before starting on this, we also need to learn one more object which is SPFarm class. To initiate SPFarm class, we need to know the configuration database connection path, just like ordinary connection path to connect to the database we use in ADO.NET.

For example, this can be a connection string for my configuration database.

string strConnectionString = @”server=localhost\myServer;initial catalog=SharePoint_Config_75980120-a9bf-4p71-3401-qn26385ca059;IntegratedSecurity=SSPI;”;

SPFarm objFarm = SPFarm.Open(strConnectionString);
SPFeatureDefinitionCollection objinstalledFeatures = objFarm.FeatureDefinitions;
objinstalledFeatures.Add(“{new feature}”, new Guid(“guid of feature “));


This will install the new feature at farm level. Now let’s see how to uninstall the feature from farm level.

objinstalledFeatures.Remove(new Guid(“guid of feature “));

Ok, this was about installing the feature at farm level; now let’s talk about activating feature at site level.

Considering objWeb refers to a site, then

objWeb.Features.Add(new Guid({guid of feature}));


And same to deactivate the feature from site level,

objWeb.Features.Remove(new Guid({guid of feature}));


Now let us understand what we mean by feature properties. Each feature has set of properties that you can set along with it and also built in properties that you can enumerate and check. We will insert properties in the feature.

This is how you enumerate to the properties of the feature.

foreach (SPFeatureProperty objProperty in objFeature.Properties)
{
String strPropName = objProperty.Name;
String strPropValue = objProperty.Value;
}


Now let us see how we can add and remove the properties from feature.

SPFeatureProperty objProp = new SPFeatureProperty(“{your prop name}”, “{prop value}”);
objFeature.Properties.Add(objProp);


SPFeatureProperty objProp = objFeature.Properties[0];
objFeature.Properties.Remove(objProp);


Interesting thing to note is that you do not need to call any update method on any of the objects to achieve this behavior.

Now let us take solution in to picture. By solution we mean collectively we can install features at farm level (on every server in the farm) and deploy features to those servers.

We know that every operation that I have mentioned in this article can be achieved through STSADM command, so this operation can also be performed by –o addsolution and –o deletesolution command. But here we are talking about object model, so I will show you the way to do it programmatically.

Consider the objFarm object just like mentioned above and then if you access the Solutions property, it will give you all solutions deployed at farm level.

It can be done simple as add and remove method.

objFarm.Solutions.Add({customapp.cab});
objFarm.Solutions.Remove(new Guid({guid of solution})); // Pass Guid of solution


You can enumerate through all solutions in Farm, like mentioned below.

foreach (SPSolution objSolution in objFarm.Solutions)
{
//your custom code
}


Each objSolution has different properties like Deployed, DeployedServers, Id, Name etc.

Again each DeplyoedServer represents SPServer class and has properties like Name. So in a nutshell, you can make use of these classes and play with it.

At the end I would like to summarize with what we discussed in the article with classes.

SPFeature – Represents individual feature
SPFeatureCollection – Represents collection of features at site level
SPFeatureDefinition – Represents Feature definition.
SPFeatureDefinitionCollection – Represents all feature definitions of farm
SPFeatureProperty – Individual property of feature
SPFeaturePropertyCollection – All properties of single feature
SPFarm – represents Farm
SPSolution – Represents solution file.
SPServer – Represents each server on which solution is deployed in Farm.

Thank you.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com