private void DeployFarmFeature(object modelHost, FeatureDefinition featureModel)
        {
            throw new NotImplementedException("SPFarm feature activation has not implemented yet");

            //var farm = modelHost.WithAssertAndCast<SPFarm>("modelHost", value => value.RequireNotNull());
            //ProcessFeature(farm.FeatureDefinitions, featureModel);
        }
Beispiel #2
0
        private void DeployFeatureInternal(object modelHost, FeatureDefinition featureModel)
        {
            switch (featureModel.Scope)
            {
                case FeatureDefinitionScope.Farm:
                    {
                        TraceService.Error((int)LogEventId.ModelProvisionCoreCall, "Farm features are not supported with CSOM. Throwing SPMeta2NotSupportedException.");
                        throw new SPMeta2NotSupportedException("Farm features are not supported with CSOM.");
                    }

                case FeatureDefinitionScope.WebApplication:
                    {
                        TraceService.Error((int)LogEventId.ModelProvisionCoreCall, "Web application features are not supported with CSOM. Throwing SPMeta2NotSupportedException.");
                        throw new SPMeta2NotSupportedException("Web application features are not supported with CSOM.");
                    }

                case FeatureDefinitionScope.Site:
                    DeploySiteFeature(modelHost, featureModel);
                    break;

                case FeatureDefinitionScope.Web:
                    DeployWebFeature(modelHost, featureModel);
                    break;
            }
        }
        private void DeploySiteFeature(object modelHost, FeatureDefinition featureModel)
        {
            var siteModelHost = modelHost.WithAssertAndCast<SiteModelHost>("modelHost", value => value.RequireNotNull());
            var site = siteModelHost.HostSite;

            ProcessFeature(modelHost, site.Features, featureModel);
        }
        private void ValidateWebFeature(object modelHost, FeatureDefinition featureModel)
        {
            var host = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
            var features = LoadWebFeatures(host);

            var currentFeature = GetFeature(features, featureModel);

            Trace(featureModel, currentFeature);
        }
        private void DeploySiteFeature(object modelHost, FeatureDefinition featureModel)
        {
            var siteModelHost = modelHost.WithAssertAndCast<SiteModelHost>("modelHost", value => value.RequireNotNull());
            var site = siteModelHost.HostSite;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying site feature.");

            ProcessFeature(modelHost, site.Features, featureModel);
        }
        private void DeployWebApplicationFeature(object modelHost, FeatureDefinition featureModel)
        {
            var webApplicationModelHost = modelHost.WithAssertAndCast<WebApplicationModelHost>("modelHost", value => value.RequireNotNull());
            var webApplication = webApplicationModelHost.HostWebApplication;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying web application feature.");

            ProcessFeature(modelHost, webApplication.Features, featureModel);
        }
Beispiel #7
0
        private void DeploySiteFeature(object modelHost, FeatureDefinition featureModel)
        {
            var siteModelHost = modelHost.WithAssertAndCast<SiteModelHost>("modelHost", value => value.RequireNotNull());

            var site = siteModelHost.HostSite;
            var context = site.Context;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying site feature.");

            // a bit unclear why it should be Farm scope
            // seems to be a scope to find feature definition in so that for sandbox solutions it would be Site?
            ProcessFeature(modelHost, context, site.Features, featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope.Farm);
        }
Beispiel #8
0
        private void DeployWebFeature(object modelHost, FeatureDefinition featureModel)
        {
            var webModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());

            var web = webModelHost.HostWeb;
            var context = web.Context;

            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying web feature.");

            // a bit unclear why it should be Farm scope
            // seems to be a scope to find feature definition in so that for sandbox solutions it would be Site?
            // http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.featurecollection.add%28v=office.15%29.aspx
            ProcessFeature(modelHost, context, web.Features, featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope.Farm);
        }
        private void Trace(FeatureDefinition featureModel, Feature currentFeature)
        {
            var featureActivated = currentFeature != null;

            TraceUtils.WithScope(traceScope =>
            {
                traceScope.WriteLine(string.Format("Validating model:[{0}] feature:[{1}]", featureModel, currentFeature));

                traceScope.WriteLine(string.Format("Model prop [{0}] on obj prop [{1}]: model:[{2}] obj:[{3}]",
                 "Enable",
                 "Enable",
                 featureModel.Enable,
                 featureActivated));

                Assert.AreEqual(featureModel.Enable, featureActivated);
            });
        }
        private void DeployFeatureInternal(object modelHost, FeatureDefinition featureModel)
        {
            switch (featureModel.Scope)
            {
                case FeatureDefinitionScope.Farm:
                    throw new NotSupportedException("Farm features are not supported with CSOM.");

                case FeatureDefinitionScope.WebApplication:
                    throw new NotSupportedException("Web application features are not supported with CSOM.");

                case FeatureDefinitionScope.Site:
                    DeploySiteFeature(modelHost, featureModel);
                    break;

                case FeatureDefinitionScope.Web:
                    DeployWebFeature(modelHost, featureModel);
                    break;
            }
        }
        private void DeployFeatureInternal(object modelHost, FeatureDefinition featureModel)
        {
            switch (featureModel.Scope)
            {
                case FeatureDefinitionScope.Farm:
                    DeployFarmFeature(modelHost, featureModel);
                    break;

                case FeatureDefinitionScope.WebApplication:
                    DeployWebApplicationFeature(modelHost, featureModel);
                    break;

                case FeatureDefinitionScope.Site:
                    DeploySiteFeature(modelHost, featureModel);
                    break;

                case FeatureDefinitionScope.Web:
                    DeployWebFeature(modelHost, featureModel);
                    break;
            }
        }
        private void DeploySiteFeature(object modelHost, FeatureDefinition featureModel)
        {
            var siteModelHost = modelHost.WithAssertAndCast<SiteModelHost>("modelHost", value => value.RequireNotNull());

            var site = siteModelHost.HostSite;
            var context = site.Context;

            var features = LoadSiteFeatures(siteModelHost);

            // a bit unclear why it should be Farm scope
            // seems to be a scope to find feature definition in so that for sandbox solutions it would be Site?
            ProcessFeature(modelHost, context, features, featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope.Farm);
        }
Beispiel #13
0
        private static Feature SafelyActivateWebFeature(ClientRuntimeContext context, FeatureCollection features,
            FeatureDefinition featureModel, Microsoft.SharePoint.Client.FeatureDefinitionScope scope)
        {
            var result = features.Add(featureModel.Id, featureModel.ForceActivate, scope);

            try
            {
                context.ExecuteQueryWithTrace();
            }
            catch (Exception e)
            {
                // sandbox site/web features?
                // they need to ne activated with SPFeatureDefinitionScope.Site scope
                if ((featureModel.Scope == FeatureDefinitionScope.Site || featureModel.Scope == FeatureDefinitionScope.Web)
                    && e.Message.ToUpper().Contains(featureModel.Id.ToString("D").ToUpper()))
                {
                    result = features.Add(featureModel.Id, featureModel.ForceActivate, Microsoft.SharePoint.Client.FeatureDefinitionScope.Site);
                    context.ExecuteQueryWithTrace();
                }
                else
                {
                    throw e;
                }
            }

            return result;
        }
        private void ProcessFeature(
            object modelHost,
            SPFeatureCollection features, FeatureDefinition featureModel)
        {
            var currentFeature = GetFeature(features, featureModel);
            var featureActivated = IsFeatureActivated(currentFeature);

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

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

            if (!featureActivated)
            {
                if (featureModel.Enable)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Enabling feature");
                    var f = SafelyActivateFeature(features, featureModel);

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

                    var f = SafelyActivateFeature(features, featureModel);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = f,
                        ObjectType = typeof(SPFeature),
                        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(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
                else
                {
                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = currentFeature,
                        ObjectType = typeof(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
            }
        }
        private static SPFeature SafelyActivateFeature(SPFeatureCollection features, FeatureDefinition featureModel)
        {
            SPFeature result = null;

            try
            {
                result = features.Add(featureModel.Id, featureModel.ForceActivate);
            }
            catch (Exception e)
            {
                // sandbox site/web features?
                // they need to ne activated with SPFeatureDefinitionScope.Site scope
                if ((featureModel.Scope == FeatureDefinitionScope.Site || featureModel.Scope == FeatureDefinitionScope.Web)
                    && e.Message.ToUpper().Contains(featureModel.Id.ToString("D").ToUpper()))
                {
                    result = features.Add(featureModel.Id, featureModel.ForceActivate, SPFeatureDefinitionScope.Site);
                }
                else
                {
                    throw;
                }
            }

            return result;
        }
        private void ProcessFeature(
            object modelHost,
            SPFeatureCollection features, FeatureDefinition featureModel)
        {
            var currentFeature = features.FirstOrDefault(f => f.DefinitionId == featureModel.Id);
            var featureActivated = currentFeature != null;

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

            if (!featureActivated)
            {
                if (featureModel.Enable)
                {
                    var f = features.Add(featureModel.Id, featureModel.ForceActivate);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = f,
                        ObjectType = typeof(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
                else
                {
                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = currentFeature,
                        ObjectType = typeof(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
            }
            else
            {
                if (featureModel.Enable && featureModel.ForceActivate)
                {
                    var f = features.Add(featureModel.Id, featureModel.ForceActivate);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = f,
                        ObjectType = typeof(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
                else if (!featureModel.Enable)
                {
                    features.Remove(featureModel.Id, featureModel.ForceActivate);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = null,
                        ObjectType = typeof(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
                else
                {
                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = currentFeature,
                        ObjectType = typeof(SPFeature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
            }
        }
 protected SPFeature GetFeature(SPFeatureCollection features, FeatureDefinition featureModel)
 {
     return features.FirstOrDefault(f => f.DefinitionId == featureModel.Id);
 }
 private void DeployWebApplicationFeature(object modelHost, FeatureDefinition featureModel)
 {
     var webApplication = modelHost.WithAssertAndCast<SPWebApplication>("modelHost", value => value.RequireNotNull());
     ProcessFeature(modelHost, webApplication.Features, featureModel);
 }
        private void DeployWebFeature(object modelHost, FeatureDefinition featureModel)
        {
            var webModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
            var web = webModelHost.HostWeb;

            ProcessFeature(modelHost, web.Features, featureModel);
        }
 public static ModelNode AddWebFeature(this ModelNode model, FeatureDefinition definition)
 {
     return AddWebFeature(model, definition, null);
 }
 public static ModelNode AddFeature(this ModelNode model, FeatureDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
Beispiel #22
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
                    });
                }
            }


        }
        private void DeployFarmFeature(object modelHost, FeatureDefinition featureModel)
        {
            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deploying farm feature.");

            var farmModelHost = modelHost.WithAssertAndCast<FarmModelHost>("modelHost", value => value.RequireNotNull());
            var farm = farmModelHost.HostFarm;

            var adminService = SPWebService.AdministrationService;
            ProcessFeature(modelHost, adminService.Features, featureModel);
        }
 protected bool IsFeatureActivated(FeatureCollection features, FeatureDefinition featureModel)
 {
     return GetFeature(features, featureModel) != null;
 }
        private void ProcessFeature(
                    object modelHost,
                    ClientRuntimeContext context,
                    FeatureCollection features,
                    FeatureDefinition featureModel,
                    Microsoft.SharePoint.Client.FeatureDefinitionScope scope)
        {
            var currentFeature = GetFeature(features, featureModel);
            var featureActivated = IsFeatureActivated(features, featureModel);

            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)
                {
                    tmpFeature = features.Add(featureModel.Id, featureModel.ForceActivate, scope);
                    context.ExecuteQuery();
                }
                else
                {
                    tmpFeature = GetFeature(features, featureModel);
                }

                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)
                {
                    var f = features.Add(featureModel.Id, featureModel.ForceActivate, scope);

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

                    context.ExecuteQuery();
                }
                else if (!featureModel.Enable)
                {
                    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.ExecuteQuery();
                }
                else
                {
                    var tmpFeature = GetFeature(features, featureModel);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = tmpFeature,
                        ObjectType = typeof(Feature),
                        ObjectDefinition = featureModel,
                        ModelHost = modelHost
                    });
                }
            }
        }
 public static ModelNode AddWebFeature(this ModelNode model, FeatureDefinition definition, Action<ModelNode> action)
 {
     return AddFeature(model, definition, action);
 }
        public void Deploy_ActivateCustomFeature()
        {
            // Step 1, define web feature
            //
            // For custom features we need to define Id, Scope and Enable/ForceActivate properties
            // Title is not used, but it is nice to have readable code
            var mdsFeatrure = new FeatureDefinition
            {
                Title = "Minimal Download Strategy",
                Id = new Guid("{87294c72-f260-42f3-a41b-981a2ffce37a}"),
                Scope = SPMeta2.Definitions.FeatureDefinitionScope.Web,
                Enable = true
            };

            // Step 2, define web model and artifact relationships - add feature to the web 
            var model = SPMeta2Model
                             .NewWebModel(web =>
                             {
                                 web.AddFeature(mdsFeatrure);
                             });

            // Step 3, deploy model
            DeployWebModel(model);
        }
        public override ModelNode ReverseSingleHost(object reverseHost, ReverseOptions options)
        {
            var typedReverseHost = (reverseHost as FeatureReverseHost);
            var item = typedReverseHost.Feature;

            FeatureDefinition knownFeature = null;

            if (item != null)
            {
                knownFeature = typedReverseHost.AllKnownFeatures
                                               .FirstOrDefault(f => f.Id == item.DefinitionId);
            }
            else
            {
                knownFeature = typedReverseHost.AllKnownFeatures
                                               .FirstOrDefault(f => f.Id == typedReverseHost.FeatureId);
            }

            if (knownFeature == null)
            {
                // TODO, warning in logs 
                // better register a known feature
            }

            var def = new FeatureDefinition();

            if (typedReverseHost.IsSiteLevel)
                def.Scope = FeatureDefinitionScope.Site;

            if (typedReverseHost.IsWebLevel)
                def.Scope = FeatureDefinitionScope.Web;

            if (item != null)
            {
                def.Id = item.DefinitionId;
                def.Enable = true;
            }
            else
            {
                def.Id = typedReverseHost.FeatureId;
                def.Enable = false;
            }

            def.Title = knownFeature != null ? knownFeature.Title : def.Id.ToString();

            return new FeatureModelNode
            {
                Options = { RequireSelfProcessing = true },
                Value = def
            };
        }