/// <summary>
        /// Checks if a feature is active in the given FeatureCollection.
        /// </summary>
        /// <param name="features">FeatureCollection to check in</param>
        /// <param name="featureID">ID of the feature to check</param>
        /// <param name="noRetry">Use regular ExecuteQuery</param>
        /// <returns>True if active, false otherwise</returns>
        private static async Task <bool> IsFeatureActiveInternal(FeatureCollection features, Guid featureID, bool noRetry = false)
        {
            var featureIsActive = false;

            features.ClearObjectData();

            features.Context.Load(features);
            if (noRetry)
            {
                string clientTag = $"{PnPCoreUtilities.PnPCoreVersionTag}:ProcessFeatureInternal";
                if (clientTag.Length > 32)
                {
                    clientTag = clientTag.Substring(0, 32);
                }
                features.Context.ClientTag = clientTag;

                // Don't update this to ExecuteQueryRetry
                await features.Context.ExecuteQueryAsync();
            }
            else
            {
                await features.Context.ExecuteQueryRetryAsync();
            }

            var iprFeature = features.GetById(featureID);

            iprFeature.EnsureProperties(f => f.DefinitionId);

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && !iprFeature.ServerObjectIsNull.Value && iprFeature.DefinitionId.Equals(featureID))
            {
                featureIsActive = true;
            }

            return(featureIsActive);
        }
Ejemplo n.º 2
0
        private static bool RemoveFeatureFromWeb(ClientContext userContext, Guid featureDefinitionId)
        {
            try
            {
                FeatureCollection features = userContext.Web.Features;
                ClearObjectData(features);

                userContext.Load(features);
                userContext.ExecuteQuery();

                //DumpFeatures(features, "web");

                Feature targetFeature = features.GetById(featureDefinitionId);
                targetFeature.EnsureProperties(f => f.DefinitionId);

                if (targetFeature == null || !targetFeature.IsPropertyAvailable("DefinitionId") || targetFeature.ServerObjectIsNull.Value)
                {
                    Logger.LogInfoMessage(String.Format("Could not delete Feature {0}; feature not found in web.Features", featureDefinitionId.ToString()), false);
                    return(false);
                }

                features.Remove(featureDefinitionId, true);

                // commit the changes
                userContext.Load(features);
                userContext.ExecuteQuery();
                return(true);
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage(String.Format("RemoveFeatureFromWeb() failed for Feature {0} on web {1}; Error={2}", featureDefinitionId.ToString(), userContext.Web.Url, ex.Message), false);
                return(false);
            }
        }
Ejemplo n.º 3
0
        private static bool IsFeatureOnWeb(Guid FeatureID, ClientContext clientContext)
        {
            bool isFeatureAvailable = false;

            try
            {
                FeatureCollection features = clientContext.Web.Features;
                clientContext.Load(features);
                clientContext.ExecuteQuery();

                Feature feature = features.GetById(FeatureID);
                if (feature != null)
                {
                    clientContext.Load(feature);
                    clientContext.ExecuteQuery();
                    if (feature.DefinitionId != null)
                    {
                        isFeatureAvailable = true;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.ForegroundColor = System.ConsoleColor.Red;
                Logger.LogErrorMessage("[IsFeatureOnWeb] Exception Message: " + ex.Message);
                System.Console.ResetColor();
                ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, Constants.NotApplicable, "AddWebPart", ex.Message, ex.ToString(), "IsFeatureOnWeb()", ex.GetType().ToString());
            }


            return(isFeatureAvailable);
        }
        private static bool RemoveFeatureFromSite(ClientContext userContext, Guid featureDefinitionId)
        {
            try
            {
                FeatureCollection features = userContext.Site.Features;
                ClearObjectData(features);

                userContext.Load(features);
                userContext.ExecuteQuery();

                //DumpFeatures(features, "site");

                Feature targetFeature = features.GetById(featureDefinitionId);
                targetFeature.EnsureProperties(f => f.DefinitionId);

                if (targetFeature == null || !targetFeature.IsPropertyAvailable("DefinitionId") || targetFeature.ServerObjectIsNull.Value)
                {
                    Logger.LogInfoMessage(String.Format("Could not delete Feature {0}; feature not found in site.Features", featureDefinitionId.ToString()), false);
                    return(false);
                }
                features.Remove(featureDefinitionId, true);

                // commit the changes
                userContext.Load(features);
                userContext.ExecuteQuery();
                return(true);
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage(String.Format("[DeleteMissingFeatures: RemoveFeatureFromSite] failed for Feature {0} on site {1}; Error={2}", featureDefinitionId.ToString(), userContext.Site.Url, ex.Message), true);
                ExceptionCsv.WriteException(Constants.NotApplicable, userContext.Site.Url, Constants.NotApplicable, "Feature", ex.Message, ex.ToString(), "RemoveFeatureFromSite",
                                            ex.GetType().ToString(), String.Format("RemoveFeatureFromSite() failed for Feature {0} on site {1}", featureDefinitionId.ToString(), userContext.Site.Url));
                return(false);
            }
        }
Ejemplo n.º 5
0
        public static bool IsFeatureActive(this ClientContext value, FeatureCollection features, Guid featureId)
        {
            var featureIsActive = false;

            value.Load(features);
            value.ExecuteQuery();

            var iprFeature = features.GetById(featureId);

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && !iprFeature.ServerObjectIsNull.Value && iprFeature.DefinitionId.Equals(featureId))
            {
                featureIsActive = true;
            }

            return(featureIsActive);
        }
        private static bool IsFeatureActiveInternal(FeatureCollection features, Guid featureID, bool noRetry = false)
#endif
        {
            var featureIsActive = false;

            features.ClearObjectData();

            features.Context.Load(features);
            if (noRetry)
            {
                string clientTag = $"{PnPCoreUtilities.PnPCoreVersionTag}:ProcessFeatureInternal";
                if (clientTag.Length > 32)
                {
                    clientTag = clientTag.Substring(0, 32);
                }
                features.Context.ClientTag = clientTag;

#if NETSTANDARD2_0
                (features.Context as ClientContext).FormDigestHandlingEnabled = false;
#endif

                // Don't update this to ExecuteQueryRetry
#if !ONPREMISES
                await features.Context.ExecuteQueryAsync();
#else
                features.Context.ExecuteQuery();
#endif
            }
            else
            {
#if !ONPREMISES
                await features.Context.ExecuteQueryRetryAsync();
#else
                features.Context.ExecuteQueryRetry();
#endif
            }

            var iprFeature = features.GetById(featureID);
            iprFeature.EnsureProperties(f => f.DefinitionId);

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && !iprFeature.ServerObjectIsNull.Value && iprFeature.DefinitionId.Equals(featureID))
            {
                featureIsActive = true;
            }

            return(featureIsActive);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Checks if a feature is active in the given FeatureCollection.
        /// </summary>
        /// <param name="features">FeatureCollection to check in</param>
        /// <param name="featureID">ID of the feature to check</param>
        /// <returns>True if active, false otherwise</returns>
        private static bool IsFeatureActiveInternal(FeatureCollection features, Guid featureID)
        {
            bool featureIsActive = false;

            features.Context.Load(features);
            features.Context.ExecuteQueryRetry();

            Feature iprFeature = features.GetById(featureID);

            features.Context.Load(iprFeature, f => f.DefinitionId);
            features.Context.ExecuteQueryRetry();

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && !iprFeature.ServerObjectIsNull.Value && iprFeature.DefinitionId.Equals(featureID))
            {
                featureIsActive = true;
            }

            return(featureIsActive);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Checks if a feature is active
        /// </summary>
        /// <param name="web">Web to operate against</param>
        /// <param name="featureID">ID of the feature to check</param>
        /// <returns>True if active, false otherwise</returns>
        public static bool IsFeatureActive(this Web web, Guid featureID)
        {
            bool featureIsActive = false;

            FeatureCollection clientSiteFeatures = web.Features;

            web.Context.Load(clientSiteFeatures);
            web.Context.ExecuteQuery();
            Feature iprFeature = clientSiteFeatures.GetById(featureID);

            web.Context.Load(iprFeature);
            web.Context.ExecuteQuery();

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && iprFeature.DefinitionId.Equals(featureID))
            {
                featureIsActive = true;
            }
            else
            {
                featureIsActive = false;
            }

            return(featureIsActive);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Checks if a feature is active in the given FeatureCollection.
        /// </summary>
        /// <param name="features">FeatureCollection to check in</param>
        /// <param name="featureID">ID of the feature to check</param>
        /// <returns>True if active, false otherwise</returns>
        private static bool IsFeatureActiveInternal(FeatureCollection features, Guid featureID)
        {
            bool featureIsActive = false;

            features.Context.Load(features);
            features.Context.ExecuteQueryRetry();

            Feature iprFeature = features.GetById(featureID);
            features.Context.Load(iprFeature, f => f.DefinitionId);
            features.Context.ExecuteQueryRetry();

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && !iprFeature.ServerObjectIsNull.Value && iprFeature.DefinitionId.Equals(featureID))
            {
                featureIsActive = true;
            }

            return featureIsActive;
        }
Ejemplo n.º 10
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var definition = model.WithAssertAndCast <FeatureDefinition>("model", value => value.RequireNotNull());

            if (!IsValidHost(modelHost))
            {
                throw new Exception("model host needs to be SPFarm, SPWebApplication, SPSit or SPWeb instance");
            }


            FeatureCollection features = null;
            Feature           spObject = null;

            var assert = ServiceFactory.AssertService
                         .NewAssert(definition, spObject);

            switch (definition.Scope)
            {
            case FeatureDefinitionScope.Farm:
                throw new SPMeta2NotImplementedException("Farm features are not supported in CSOM.");

            case FeatureDefinitionScope.WebApplication:

                throw new SPMeta2NotImplementedException("WebApplication features are not supported in CSOM.");

            case FeatureDefinitionScope.Site:

                assert.SkipProperty(m => m.Scope, "Correct site scope");

                var siteModelHost = modelHost.WithAssertAndCast <SiteModelHost>("modelHost", value => value.RequireNotNull());
                features = siteModelHost.HostSite.Features;

                var siteContext = siteModelHost.HostSite.Context;
                siteContext.Load(features);
                siteContext.ExecuteQuery();


                break;

            case FeatureDefinitionScope.Web:

                assert.SkipProperty(m => m.Scope, "Correct web scope");

                var webModelHost = modelHost.WithAssertAndCast <WebModelHost>("modelHost", value => value.RequireNotNull());
                features = webModelHost.HostWeb.Features;

                var webContext = webModelHost.HostWeb.Context;
                webContext.Load(features);
                webContext.ExecuteQuery();

                break;
            }

            var featureId = definition.Id;

            spObject = features.GetById(featureId);
            features.Context.Load(spObject, o => o.DefinitionId);
            features.Context.ExecuteQuery();

            assert.Dst = spObject;

            if (definition.Enable == false)
            {
                // check is null, skill all props

                assert.ShouldBeEqual((p, s, d) =>
                {
                    var srcProp = s.GetExpressionValue(m => m.Enable);

                    var isValid = d.ServerObjectIsNull == true;

                    return(new PropertyValidationResult
                    {
                        Tag = p.Tag,
                        Src = srcProp,
                        Dst = null,
                        IsValid = isValid
                    });
                });

                assert
                .SkipProperty(m => m.Id, "Enable = false. Skipping.")
                .SkipProperty(m => m.ForceActivate, "Enable = false. Skipping.")
                .SkipProperty(m => m.Enable, "Enable = false. Skipping.")
                .SkipProperty(m => m.Title, "Enable = false. Skipping.");
            }
            else
            {
                assert.ShouldBeEqual(m => m.Id, o => o.DefinitionId);

                if (definition.ForceActivate)
                {
                    assert
                    .SkipProperty(m => m.ForceActivate, "ForceActivate = true. Expect not null feature instance.")
                    .ShouldNotBeNull(spObject);
                }
                else
                {
                    assert
                    .SkipProperty(m => m.ForceActivate, "ForceActivate = false. Skipping.");
                }


                if (definition.Enable)
                {
                    assert
                    .SkipProperty(m => m.Enable, "Enable = true. Expect not null feature instance.")
                    .ShouldNotBeNull(spObject);
                }
                else
                {
                    assert
                    .SkipProperty(m => m.Enable, "Enable = false. Expect null feature instance.")
                    .ShouldBeNull(spObject);
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Checks if a feature is active in the given FeatureCollection.
        /// </summary>
        /// <param name="features">FeatureCollection to check in</param>
        /// <param name="featureID">ID of the feature to check</param>
        /// <param name="noRetry">Use regular ExecuteQuery</param>
        /// <returns>True if active, false otherwise</returns>
        private static bool IsFeatureActiveInternal(FeatureCollection features, Guid featureID, bool noRetry=false)
        {
            var featureIsActive = false;

            features.ClearObjectData();

            features.Context.Load(features);
            if (noRetry)
            {
                string clientTag = $"{PnPCoreUtilities.PnPCoreVersionTag}:ProcessFeatureInternal";
                if (clientTag.Length > 32)
                {
                    clientTag = clientTag.Substring(0, 32);
                }
                features.Context.ClientTag = clientTag;
                // Don't update this to ExecuteQueryRetry
                features.Context.ExecuteQuery();
            }
            else
            {
                features.Context.ExecuteQueryRetry();
            }

            var iprFeature = features.GetById(featureID);
            iprFeature.EnsureProperties(f => f.DefinitionId);

            if (iprFeature != null && iprFeature.IsPropertyAvailable("DefinitionId") && !iprFeature.ServerObjectIsNull.Value && iprFeature.DefinitionId.Equals(featureID))
            {
                featureIsActive = true;
            }

            return featureIsActive;
        }
Ejemplo n.º 12
0
        private void ProcessFeature(
            object modelHost,
            ClientRuntimeContext context,
            FeatureCollection features,
            FeatureDefinition featureModel,
            Microsoft.SharePoint.Client.FeatureDefinitionScope scope)
        {
            var featureId = featureModel.Id;

            var currentFeature = features.GetById(featureId);

            features.Context.ExecuteQueryWithTrace();

            var featureActivated = IsFeatureActivated(currentFeature);

            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Is feature activated: [{0}]", featureActivated);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = currentFeature,
                ObjectType       = typeof(Feature),
                ObjectDefinition = featureModel,
                ModelHost        = modelHost
            });

            if (!featureActivated)
            {
                Feature tmpFeature;

                if (featureModel.Enable)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Enabling feature");
                    tmpFeature = SafelyActivateWebFeature(context, features, featureModel, scope);
                }
                else
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching feature by ID");
                    tmpFeature = features.GetById(featureId);

                    features.Context.ExecuteQueryWithTrace();
                }

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = tmpFeature,
                    ObjectType       = typeof(Feature),
                    ObjectDefinition = featureModel,
                    ModelHost        = modelHost
                });
            }
            else
            {
                if (featureModel.Enable && featureModel.ForceActivate)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Feature enabled, but ForceActivate = true. Force activating.");

                    var f = SafelyActivateWebFeature(context, features, featureModel, scope);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioned,
                        Object           = f,
                        ObjectType       = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost        = modelHost
                    });
                }
                else if (!featureModel.Enable)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Removing feature.");
                    features.Remove(featureModel.Id, featureModel.ForceActivate);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioned,
                        Object           = null,
                        ObjectType       = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost        = modelHost
                    });

                    context.ExecuteQueryWithTrace();
                }
                else
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching feature by ID");
                    var tmpFeature = features.GetById(featureId);

                    features.Context.ExecuteQueryWithTrace();

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioned,
                        Object           = tmpFeature,
                        ObjectType       = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost        = modelHost
                    });
                }
            }
        }
Ejemplo n.º 13
0
        private void ProcessFeature(
                    object modelHost,
                    ClientRuntimeContext context,
                    FeatureCollection features,
                    FeatureDefinition featureModel,
                    Microsoft.SharePoint.Client.FeatureDefinitionScope scope)
        {
            var featureId = featureModel.Id;

            var currentFeature = features.GetById(featureId);
            features.Context.ExecuteQueryWithTrace();

            var featureActivated = IsFeatureActivated(currentFeature);

            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Is feature activated: [{0}]", featureActivated);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = currentFeature,
                ObjectType = typeof(Feature),
                ObjectDefinition = featureModel,
                ModelHost = modelHost
            });

            if (!featureActivated)
            {
                Feature tmpFeature;

                if (featureModel.Enable)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Enabling feature");
                    tmpFeature = SafelyActivateWebFeature(context, features, featureModel, scope);
                }
                else
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching feature by ID");
                    tmpFeature = features.GetById(featureId);

                    features.Context.ExecuteQueryWithTrace();
                }

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = tmpFeature,
                    ObjectType = typeof(Feature),
                    ObjectDefinition = featureModel,
                    ModelHost = modelHost
                });
            }
            else
            {
                if (featureModel.Enable && featureModel.ForceActivate)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Feature enabled, but ForceActivate = true. Force activating.");

                    var f = SafelyActivateWebFeature(context, features, featureModel, scope);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = f,
                        ObjectType = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
                else if (!featureModel.Enable)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Removing feature.");
                    features.Remove(featureModel.Id, featureModel.ForceActivate);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = null,
                        ObjectType = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });

                    context.ExecuteQueryWithTrace();
                }
                else
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Fetching feature by ID");
                    var tmpFeature = features.GetById(featureId);

                    features.Context.ExecuteQueryWithTrace();

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = tmpFeature,
                        ObjectType = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
            }


        }