none
How to activate a feature from Client OM RRS feed

  • Question

  • <script type="text/javascript"></script>

    Hi,

    I want to activate a feature from the client object model but I dont get it to work. I thougth that something like this should do the trick:

     

    function ActivateFeature(featureGuid) {
      var clientContext = new SP.ClientContext.get_current();
      var site = clientContext.get_site();
      var features = site.get_features();
      features.add(featureGuid, false, SP.FeatureDefinitionScope.site);
    }

    I dont get any errors, but the feature is not activated. Any ideas whats going wrong?

    I call the function with a string representation of the guid (i.e. XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX).

     

    Regards,

    Martin

     

    Monday, July 5, 2010 4:03 PM

Answers

All replies

  • According to the docs, the first parameter to the call to add should be an SP.Guid.

    SP.FeatureCollection.add(featureId, force, featdefScope) Method
    http://msdn.microsoft.com/en-us/library/ee658706.aspx

    SP.Guid(guidText) Constructor
    http://msdn.microsoft.com/en-us/library/ee659011.aspx

     


    Rob Windsor [MVP-VB], Toronto Canada
    Monday, July 5, 2010 5:39 PM
  • Also, call clientContext.ExecuteQuery after the features.Add method call.
    certdev.com
    Monday, July 5, 2010 11:21 PM
  • Hi again,
    and thanks for the replies. I almost got it to work now.

    the ExecuteQuery function isnt available in the javascript api (as far as I can see). But I can use the executeQueryAsync function instead. My code now looks like this:

    function ActivateFeature(featureGuid) {
    
      var clientContext = new SP.ClientContext.get_current();
      var site = clientContext.get_site();
      var guid = new SP.Guid('{'+featureGuid+'}');
    
      var featDef = site.get_features().add(guid, false, SP.FeatureDefinitionScope.site);
    
      clientContext.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFail));
    }
    
    function OnSuccess(sender, args) {
      alert('Success!');
    }
    
    function OnFail(sender, args) {
      alert('Fail: ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    My initial problem was solved by your answers, but the feature is still not activated. I get this error message: Feature with ID '<guid>' is not installed in this farm, and cannot be added to this scope. The feature is installed and it is scoped for a site.

    Any ideas?

    Regards,

    Martin

    Tuesday, July 6, 2010 8:31 AM
  • How are you figuring out what Guid to use?
    certdev.com
    Tuesday, July 6, 2010 1:44 PM
  • It's hardcoded (in a <Menu> element used in a ribbon customization).

    Tuesday, July 6, 2010 1:55 PM
  • So does the feature show up in the Site's features with the "Activate" button next to it?
    certdev.com
    Tuesday, July 6, 2010 2:15 PM
  • Yes, the feature is shown in the site collection feature list with the "Activate" button. I'm able to activate it from the UI, so theres nothing wrong with the feature.
    Tuesday, July 6, 2010 10:08 PM
  • I'm having the same issue, did you managed to make it work? 

    I would appreciate if you could share the solution.

    Best regards,

    Hugo

    Friday, June 10, 2011 6:36 AM
  • Sorry to get this discussion back to life after so much time guys. Just wanted to let you know there's no way to activate a Feature from a Farm Solution using Client Object Model.

    The FeatureDefinitionScope enumeration refers to where the feature solution is deployed: Farm - for farm solutions, Site - for sandboxed solutions. That is why there is not Web value, because it is not referencing the scope of the feature itself, but its definition.

    Taking this into account, a code like next one should work for a feature defined deployed as part of a non-sandboxed solution (i.e. farm solution):

    web.Features.Add([guid], false, FeatureDefinitionScope.Farm);

    So, what's the problem? The FeatureCollection.Add method requires the third parameter to be FeatureDefinitionScope.Site. You can see it by yourselves if you use .NET Reflector, dotPeek or JustDecompile, and decompile the SPFeatureCollection.Add_Client method:

    if (SPFeatureDefinitionScope.Site != featdefScope)
        SPUtility.ThrowArgumentExceptionWithTraceTag(1668232561U, ULSCat.msoulscat_WSS_FeaturesInfrastructure, "featdefScope", typeof (ArgumentException));

    Microsoft should clarify this in the documentation.


    Monday, August 27, 2012 4:47 AM
  • Thank you very much for your tip with feature deployed scope instead of feature activation scope. I managed to activate SharePoint Server Publishing feature on client side JavaScript using these lines:

       var guid = new SP.Guid('{94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb}');
       var feature = subWeb.get_features().add(guid, false, SP.FeatureDefinitionScope.farm);

    I tried web and site scopes unsuccessfully until I found this thread:).


    • Edited by shsv1 Thursday, May 30, 2013 4:35 PM
    Thursday, May 30, 2013 3:01 PM
  • To active feature in a site, you need

    var site = clientContext.get_web();

    instead of 

    var site = clientContext.get_site();

    Full code worked for is given below

    function activateFeature(siteUrl) {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {
            var clientContext = new SP.ClientContext(siteUrl);
            var featureGuid = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb";
            var site = clientContext.get_web();
            var guid = new SP.Guid('{' + featureGuid + '}');

            var featDef = site.get_features().add(guid, true, SP.FeatureDefinitionScope.none);

            clientContext.executeQueryAsync(OnSuccess, OnFail);

            function OnSuccess(sender, args) {}

            function OnFail(sender, args) {
                alert('Fail: ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        });
    }


    atish

    Saturday, January 9, 2016 6:45 PM