public void Execute(ClientContext ctx, Guid featureId, FeatureDefinitionScope scope, DesiredFeatureState desiredState)
        {
            Logger.Verbose($"Started executing {nameof(EnsureFeatureState)} for id '{featureId}' and scope '{scope}' on '{ctx.Url}'");

            FeatureCollection features = null;
            if (scope == FeatureDefinitionScope.Site)
            {
                features = ctx.Site.Features;
            }
            else if (scope == FeatureDefinitionScope.Web)
            {
                features = ctx.Web.Features;
            }
            else
            {
                throw new ArgumentException($"Unsupported scope: {scope}");
            }

            ctx.Load(features);
            ctx.ExecuteQuery();

            var activated = features.Any(f => f.DefinitionId == featureId);
            if (activated && desiredState == DesiredFeatureState.Activated)
            {
                Logger.Warning($"Feature with Id '{featureId}' already activated");
                return;
            }
            else if (activated && desiredState == DesiredFeatureState.Deactivated)
            {
                features.Remove(featureId, false);
                ctx.ExecuteQuery();
            }
            else if (!activated && desiredState == DesiredFeatureState.Activated)
            {
                features.Add(featureId, false, FeatureDefinitionScope.None);
                ctx.ExecuteQuery();
            }
            else if (!activated && desiredState == DesiredFeatureState.Deactivated)
            {
                Logger.Warning($"Feature with Id '{featureId}' already deactivated");
                return;
            }
        }
Exemplo n.º 2
0
        public void Execute(ClientContext ctx, Guid featureId, FeatureDefinitionScope scope, DesiredFeatureState desiredState)
        {
            Logger.Verbose($"Started executing {nameof(EnsureFeatureState)} for id '{featureId}' and scope '{scope}' on '{ctx.Url}'");

            var features =
                scope == FeatureDefinitionScope.Site
                ? ctx.Site.Features
                : scope == FeatureDefinitionScope.Web
                    ? ctx.Web.Features
                    : throw new ArgumentException($"Unsupported scope: {scope}");

            ctx.Load(features);
            ctx.ExecuteQuery();

            var activated = features.Any(f => f.DefinitionId == featureId);

            if (activated && desiredState == DesiredFeatureState.Activated)
            {
                Logger.Warning($"Feature with Id '{featureId}' already activated");
                return;
            }
            else if (activated && desiredState == DesiredFeatureState.Deactivated)
            {
                features.Remove(featureId, false);
                ctx.ExecuteQuery();
            }
            else if (!activated && desiredState == DesiredFeatureState.Activated)
            {
                features.Add(featureId, false, FeatureDefinitionScope.None);
                ctx.ExecuteQuery();
            }
            else if (!activated && desiredState == DesiredFeatureState.Deactivated)
            {
                Logger.Warning($"Feature with Id '{featureId}' already deactivated");
                return;
            }
        }