private void DeployDefinition(object modelHost, SiteModelHost siteModelHost, CustomDocumentIdProviderDefinition definition)
        {
            var site = siteModelHost.HostSite;

            var targetType = Type.GetType(definition.DocumentProviderType);
            var targetInstance = Activator.CreateInstance(targetType) as DocumentIdProvider;

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

            DocumentId.SetProvider(site, targetInstance);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioned,
                Object = site,
                ObjectType = typeof(SPSite),
                ObjectDefinition = definition,
                ModelHost = modelHost
            });
        }
        private void DeployImageRenditionSettings(object modelHost, SiteModelHost siteModelHost,
            ImageRenditionDefinition imageRenditionModel)
        {
            var renditions = SiteImageRenditions.GetRenditions(siteModelHost.HostSite);
            var currentRendition = GetCurrentImageRendition(renditions, imageRenditionModel);

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

            if (currentRendition == null)
            {
                currentRendition = new ImageRendition
                {
                    Name = imageRenditionModel.Name,
                    Width = imageRenditionModel.Width,
                    Height = imageRenditionModel.Height
                };

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

                renditions.Add(currentRendition);
                renditions.Update();
            }
            else
            {
                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentRendition,
                    ObjectType = typeof(ImageRendition),
                    ObjectDefinition = imageRenditionModel,
                    ModelHost = modelHost
                });
            }
        }
 private void DeploySiteDependentLookup(object modelHost, SiteModelHost siteModelHost, DependentLookupFieldDefinition definition)
 {
     DeployDependentLookupField(modelHost, siteModelHost.HostSite.RootWeb.Fields, definition);
 }
        protected SPUserSolution FindExistingSolution(SiteModelHost siteModelHost,
            SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving sandbox solution by SolutionId: [{0}]", sandboxSolutionDefinition.SolutionId);

            return siteModelHost.HostSite.Solutions.OfType<SPUserSolution>()
                                                                      .FirstOrDefault(f => f.SolutionId == sandboxSolutionDefinition.SolutionId);
        }
        private void DeploySandboxSolution(object modelHost, SiteModelHost siteModelHost, SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            var existingSolutions = FindExistingSolutionFile(siteModelHost, sandboxSolutionDefinition);
            var sandboxSolution = FindExistingSolution(siteModelHost, sandboxSolutionDefinition);

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

            if (sandboxSolution != null)
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing sandbox solution");

            if (sandboxSolution != null && sandboxSolution.Status == SPUserSolutionStatus.Activated)
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Deleting existing sandbox solution");
                siteModelHost.HostSite.Solutions.Remove(sandboxSolution);
            }

            var solutionGallery = (SPDocumentLibrary)siteModelHost.HostSite.GetCatalog(SPListTemplateType.SolutionCatalog);
            var file = solutionGallery.RootFolder.Files.Add(sandboxSolutionDefinition.FileName, sandboxSolutionDefinition.Content, true);

            if (sandboxSolutionDefinition.Activate)
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Activating sandbox solution");

                var userSolution = siteModelHost.HostSite.Solutions.Add(file.Item.ID);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = userSolution,
                    ObjectType = typeof(SPUserSolution),
                    ObjectDefinition = sandboxSolutionDefinition,
                    ModelHost = modelHost
                });
            }
            else
            {
                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Activate = false. Continue provision");

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = sandboxSolution,
                    ObjectType = typeof(SPUserSolution),
                    ObjectDefinition = sandboxSolutionDefinition,
                    ModelHost = modelHost
                });
            }
        }
        protected SPFile FindExistingSolutionFile(SiteModelHost siteModelHost, SandboxSolutionDefinition sandboxSolutionDefinition)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving sandbox solution by FileName: [{0}]", sandboxSolutionDefinition.FileName);


            var site = siteModelHost.HostSite;
            var solutionGallery = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);

            return solutionGallery.RootFolder
                .Files
                .OfType<SPFile>()
                .FirstOrDefault(f => f.Name.ToUpper() == sandboxSolutionDefinition.FileName.ToUpper());
        }
 private void DeploySiteEventReceiver(object modelHost, SiteModelHost siteModelHost, EventReceiverDefinition definition)
 {
     DeployEventReceiver(modelHost, siteModelHost.HostSite.EventReceivers, definition);
 }
        private void DeploySiteGroup(object modelHost, SiteModelHost siteModelHost, DefinitionBase model)
        {
            var site = siteModelHost.HostSite;

            var securityGroupModel = model.WithAssertAndCast<SecurityGroupDefinition>("model",
                value => value.RequireNotNull());

            var web = site.RootWeb;

            //var site = web.Site;
            var currentGroup = (SPGroup)null;
            var hasInitialGroup = false;

            try
            {
                currentGroup = site.RootWeb.SiteGroups[securityGroupModel.Name];
                hasInitialGroup = true;

                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                    "Processing existing security group");
            }
            catch (SPException)
            {
                var defaultUser = EnsureDefaultUser(web, securityGroupModel);

                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                    "Processing new security group");

                // owner would be defaut site owner
                web.SiteGroups.Add(securityGroupModel.Name, web.Site.Owner, defaultUser,
                    securityGroupModel.Description ?? string.Empty);
                currentGroup = web.SiteGroups[securityGroupModel.Name];

                // updating the owner or leave as default
                // Enhance 'SecurityGroupDefinition' provision - add self-owner support #516
                // https://github.com/SubPointSolutions/spmeta2/issues/516
                var ownerUser = EnsureOwnerUser(web, securityGroupModel.Owner);

                currentGroup.Owner = ownerUser;
                currentGroup.Update();
            }

            if (hasInitialGroup)
            {
                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = currentGroup,
                    ObjectType = typeof(SPPrincipal),
                    ObjectDefinition = securityGroupModel,
                    ModelHost = modelHost
                });
            }
            else
            {
                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioning,
                    Object = null,
                    ObjectType = typeof(SPPrincipal),
                    ObjectDefinition = securityGroupModel,
                    ModelHost = modelHost
                });
            }

            currentGroup.OnlyAllowMembersViewMembership = securityGroupModel.OnlyAllowMembersViewMembership;

            if (!string.IsNullOrEmpty(securityGroupModel.Owner))
                currentGroup.Owner = EnsureOwnerUser(web, securityGroupModel.Owner);

            currentGroup.Description = securityGroupModel.Description ?? string.Empty;

            if (securityGroupModel.AllowMembersEditMembership.HasValue)
                currentGroup.AllowMembersEditMembership = securityGroupModel.AllowMembersEditMembership.Value;

            if (securityGroupModel.AllowRequestToJoinLeave.HasValue)
                currentGroup.AllowRequestToJoinLeave = securityGroupModel.AllowRequestToJoinLeave.Value;

            if (securityGroupModel.AutoAcceptRequestToJoinLeave.HasValue)
                currentGroup.AutoAcceptRequestToJoinLeave = securityGroupModel.AutoAcceptRequestToJoinLeave.Value;

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

            currentGroup.Update();
        }
 private void DeploySiteManagedProperty(object modelHost, SiteModelHost siteModelHost, ManagedPropertyDefinition managedProperty)
 {
     throw new NotImplementedException();
 }
 protected TermStore FindTermStore(SiteModelHost siteModelHost, TaxonomyTermStoreDefinition termStoreModel)
 {
     return FindTermStore(siteModelHost.HostSite,
         termStoreModel.Name, termStoreModel.Id, termStoreModel.UseDefaultSiteCollectionTermStore);
 }
 private void DeploySiteManagedProperty(object modelHost, SiteModelHost siteModelHost, ManagedPropertyDefinition managedProperty)
 {
     throw new SPMeta2NotImplementedException("Managed property deployment under site is not implemented yet");
 }