2
            Guid featureId = new Guid("0af5989a-3aea-4519-8ab0-85d91abe39ff");

            ClientContext clientContext = new ClientContext("http://mysite:786/");

            Site clientSite = clientContext.Site;
            clientContext.Load(clientSite);

            FeatureCollection clientSiteFeatures = clientSite.Features;
            clientContext.Load(clientSiteFeatures);
            clientContext.ExecuteQuery();

            // Activate the feature
            clientSite.Features.Add(featureId, true, FeatureDefinitionScope.Site);
            //clientSiteFeatures.Remove(featureId, false); 
            clientContext.ExecuteQuery();
            MessageBox.Show("Success");

When I am running this code, I am getting the exception: Feature with id "0af5989a-3aea-4519-8ab0-85d91abe39ff" isn't installed in farm and can't be added to scope.

I got this feature id from the link http://social.technet.microsoft.com/wiki/contents/articles/7695.list-of-sharepoint-2010-features-id-displayname-and-scopes.aspx

Please guide.

Regards, Vikrant Raj Behal

Community
  • 1
  • 1
vikbehal
  • 1,486
  • 3
  • 21
  • 48

4 Answers4

4
FeatureDefinitionScope.None

This activated a web-scoped feature for me.

Aaron Prince
  • 170
  • 7
2

According to MSDN the FeatureCollection.Add method has the following signature

public Feature Add(
    Guid featureId,
    bool force,
    FeatureDefinitionScope featdefScope
) 

which is intended for adding the feature to the collection of activated features and returns the added feature

Parameter FeatureDefinitionScope is used for specifying the feature scope for a feature definition. At the same time the documentation says:

It must have the value of FeatureDefinitionScope.Site or FeatureDefinitionScope.Farm

It basically means that the method FeatureCollection.Add does not accept FeatureDefinitionScope.Web value for featdefScope and therefore feature activation with Web scope is not supported.

In your case it seems you are trying to activate feature with scope that is not supported (ex. Web) via CSOM


How to verify feature scope

$feature = get-spfeature featureId
if ($feature -eq $null -or $feature -eq "") {
    echo "no feature found with id"
} else {
  echo ("feature found. Scope is  " + $feature.Scope)
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
0

In order to activate a feature using the Client Object Model, It has to be deployed using a sandbox solution. Features which are deployed via Farm Solutions cannot be activated through the Client Object Model

0
FeatureDefinitionScope.None

also works for activating Site Collection features.

The way a feature is added (Sandbox, Farm Solution) does not matter.