1

I am working on "Retrieving activated features of specific scope".

I have written following code.

        ClientContext context = ContextManager.GetClientContext(cred, siteUrl);
        Site site = null;
        Web web = null;
        Guid featureID = Guid.Empty;
        FeatureCollection activatedFeatures;

        Office365Helper.GetSiteAndWeb(context, out site, out web);

            if (isSiteCollection)
            {
                activatedFeatures = site.Features;
                scope = "Site";
            }
            else
            {
                activatedFeatures = web.Features;
                scope = "Web";
            }

            context.Load(activatedFeatures);
            context.ExecuteQuery();

            try
            {
                Feature feature = null;
                string tempString = string.Empty;
                if (string.IsNullOrEmpty(featureId))
                {
                    context.Load(activatedFeatures, gotFeature => gotFeature.Include(myfeature => myfeature.DisplayName, myfeature => myfeature.DefinitionId));
                    context.ExecuteQuery();
                    featureID = activatedFeatures.FirstOrDefault(gotFeature => gotFeature.DisplayName.Equals(featureTitle, StringComparison.InvariantCultureIgnoreCase)).DefinitionId;
                    feature = activatedFeatures.GetById(featureID);
                }
                else
                {
                    featureID = new Guid(featureId);
                    feature = activatedFeatures.GetById(featureID);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error occured while trying to retrive the feature '{0}' from the site collection '{1}'. Error: {2}", featureTitle, siteUrl, ex.Message), ex);
            }

Now using site.Features or web.Features I am retrieving all the features. But I want to retrieve only activated features.

Is there any other method available to get only activated features?

2

The Features property only returns Features that have been activated.

Web.Features property

  • Thanks Rob. My problem is solved now. By the way do we have any way to get list of all features (whether enabled or disabled)? Using SharePoint Object Model we can get it by SPFarm.Local.FeatureDefinitions , so do we have any similar way for Client object model? – samay bhavsar Jul 23 '15 at 6:28
  • No, as far as I know you can't do that – Rob Windsor Jul 23 '15 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.