public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var definition = model.WithAssertAndCast<FieldDefinition>("model", value => value.RequireNotNull());
            var spObject = GetField(modelHost, definition);

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

            ValidateField(assert, spObject, definition);

            var textField = spObject as SPFieldMultiLineText;
            var textDefinition = model.WithAssertAndCast<NoteFieldDefinition>("model", value => value.RequireNotNull());

            var textFieldAssert = ServiceFactory.AssertService.NewAssert(model, textDefinition, textField);

            textFieldAssert.ShouldBeEqual(m => m.NumberOfLines, o => o.NumberOfLines);
            textFieldAssert.ShouldBeEqual(m => m.RichText, o => o.RichText);
            textFieldAssert.ShouldBeEqual(m => m.AppendOnly, o => o.AppendOnly);

            textFieldAssert.ShouldBeEqual(m => m.RichText, o => o.RichText);
            textFieldAssert.ShouldBeEqual(m => m.UnlimitedLengthInDocumentLibrary, o => o.UnlimitedLengthInDocumentLibrary);

            if (!string.IsNullOrEmpty(textDefinition.RichTextMode))
                textFieldAssert.ShouldBeEqual(m => m.RichTextMode, o => o.GetRichTextMode());
            else
                textFieldAssert.SkipProperty(m => m.RichTextMode);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var list = modelHost.WithAssertAndCast<SPList>("modelHost", value => value.RequireNotNull());
            var listItemModel = model.WithAssertAndCast<ListItemDefinition>("model", value => value.RequireNotNull());

            DeployInternall(list, listItemModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var typedModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
            var typedDefinition = model.WithAssertAndCast<DeleteTopNavigationNodesDefinition>("model", value => value.RequireNotNull());

            DeployDefinition(modelHost, typedModelHost, typedDefinition);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var webAppModelHost = modelHost.WithAssertAndCast<WebApplicationModelHost>("modelHost", value => value.RequireNotNull());
            var definition = model.WithAssertAndCast<PeoplePickerSettingsDefinition>("model", value => value.RequireNotNull());

            DeployPeoplePickerSettings(modelHost, webAppModelHost.HostWebApplication, definition);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            // base validation
            base.DeployModel(modelHost, model);

            // web specific validation
            var host = modelHost.WithAssertAndCast<WebpartPageModelHost>("modelHost", value => value.RequireNotNull());
            var definition = model.WithAssertAndCast<PageViewerWebPartDefinition>("model", value => value.RequireNotNull());

            var item = host.PageListItem;

            WebPartExtensions.WithExistingWebPart(item, definition, (spWebPartManager, spObject) =>
            {
                var web = spWebPartManager.Web;
                var typedObject = spObject as PageViewerWebPart;

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

                if (!string.IsNullOrEmpty(definition.ContentLink))
                    assert.ShouldBeEqual(m => m.ContentLink, o => o.ContentLink);
                else
                    assert.SkipProperty(m => m.ContentLink);

                if (!string.IsNullOrEmpty(definition.SourceType))
                    assert.ShouldBeEqual(m => m.SourceType, o => o.GetSourceType());
                else
                    assert.SkipProperty(m => m.SourceType);
            });
        }
        public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            var quickLaunchNode = model as QuickLaunchNavigationNodeDefinition;

            if (modelHost is WebModelHost)
            {
                var webModelHost = modelHost as WebModelHost;
                var currentNode = EnsureRootQuickLaunchNavigationNode(webModelHost, quickLaunchNode);

                var nodeHost = ModelHostBase.Inherit<NavigationNodeModelHost>(webModelHost, host =>
                {
                    host.HostNavigationNode = currentNode;
                });

                action(nodeHost);
            }
            else if (modelHost is NavigationNodeModelHost)
            {
                var nodeModelHost = modelHost as NavigationNodeModelHost;
                var currentNode = EnsureQuickLaunchNavigationNode(nodeModelHost, quickLaunchNode);

                var nodeHost = ModelHostBase.Inherit<NavigationNodeModelHost>(nodeModelHost, host =>
                {
                    host.HostNavigationNode = currentNode;
                });

                action(nodeHost);
            }
            else
            {
                action(modelHost);
            }
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var modelHostContext = modelHost.WithAssertAndCast<ModelHostContext>("modelHost", value => value.RequireNotNull());
            var fieldlinkModel = model.WithAssertAndCast<ContentTypeFieldLinkDefinition>("model", value => value.RequireNotNull());

            var site = modelHostContext.Site;
            var contentType = modelHostContext.ContentType;

            var context = site.Context;

            context.Load(contentType, ct => ct.FieldLinks);
            context.ExecuteQuery();

            var spFieldLink = FindFieldLinkById(contentType.FieldLinks, fieldlinkModel.FieldId);

            TraceUtils.WithScope(traceScope =>
            {
                Trace.WriteLine(string.Format("Validate model: {0} ContentType:{1}", fieldlinkModel, contentType));

                // assert base properties
                traceScope.WithTraceIndent(trace =>
                {
                    trace.WriteLine(string.Format("Validate FieldId: model:[{0}] ct field link:[{1}]", fieldlinkModel.FieldId, spFieldLink.Id));
                    Assert.AreEqual(fieldlinkModel.FieldId, spFieldLink.Id);
                });
            });
        }
        public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            var web = ExtractWeb(modelHost);

            if (web != null)
            {
                string securityGroupName;

                if (model is SecurityGroupLinkDefinition)
                    securityGroupName = (model as SecurityGroupLinkDefinition).SecurityGroupName;
                else if (model is SecurityGroupDefinition)
                    securityGroupName = (model as SecurityGroupDefinition).Name;
                else
                {
                    throw new ArgumentException("model has to be SecurityGroupDefinition or SecurityGroupLinkDefinition");
                }

                var securityGroup = web.SiteGroups[securityGroupName];

                var newModelHost = new SecurityGroupModelHost
                {
                    SecurityGroup = securityGroup,
                    SecurableObject = modelHost as SPSecurableObject
                };

                action(newModelHost);
            }
            else
            {
                action(modelHost);
            }
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var farmModelHost = modelHost.WithAssertAndCast<FarmModelHost>("modelHost", value => value.RequireNotNull());
            var managedAccountDefinition = model.WithAssertAndCast<ManagedAccountDefinition>("model", value => value.RequireNotNull());

            DeployManagedAccount(modelHost, farmModelHost.HostFarm, managedAccountDefinition);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var webModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
            var navigationModel = model.WithAssertAndCast<WebNavigationSettingsDefinition>("model", value => value.RequireNotNull());

            DeployNavigationSettings(modelHost, webModelHost, navigationModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var list = ExtractListFromHost(modelHost);
            var hideContentTypeLinksDefinition = model.WithAssertAndCast<RemoveContentTypeLinksDefinition>("model", value => value.RequireNotNull());

            DeployHideContentTypeLinks(modelHost, list, hideContentTypeLinksDefinition);
        }
        public override void Validate(DefinitionBase definition, List<ValidationResult> result)
        {
            Validate<ListDefinition>(definition, model =>
            {
                model
                    .NotNullString(m => m.Title, result)
                    .NotEmptyString(m => m.Title, result)
                    .NoSpacesBeforeOrAfter(m => m.Title, result)

                    .NotNullString(m => m.Description, result)
                    .NotEmptyString(m => m.Description, result)
                    .NoSpacesBeforeOrAfter(m => m.Description, result)

                    .NotNullString(m => m.Url, result)
                    .NotEmptyString(m => m.Url, result)
                    .NoSpacesBeforeOrAfter(m => m.Url, result);

                if (model.TemplateType == 0)
                {
                    model
                        .NotNullString(m => m.TemplateName, result)
                        .NotEmptyString(m => m.TemplateName, result)
                        .NoSpacesBeforeOrAfter(m => m.TemplateName, result);
                }

                if (string.IsNullOrEmpty(model.TemplateName))
                {
                    model
                        .NotEqual(m => m.TemplateType, 0, result);
                }
            });
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var definition = model.WithAssertAndCast<EventReceiverDefinition>("model", value => value.RequireNotNull());
            Microsoft.SharePoint.Client.EventReceiverDefinition spObject = null;

            if (modelHost is ListModelHost)
                spObject = FindEventReceiverDefinition((modelHost as ListModelHost).HostList.EventReceivers, definition);
            else if (modelHost is WebModelHost)
                spObject = FindEventReceiverDefinition((modelHost as WebModelHost).HostWeb.EventReceivers, definition);
            else if (modelHost is SiteModelHost)
                spObject = FindEventReceiverDefinition((modelHost as SiteModelHost).HostSite.EventReceivers, definition);
            else
            {
                throw new SPMeta2UnsupportedModelHostException("model host should be ListModelHost or WebModelHost");
            }

            var assert = ServiceFactory.AssertService
                               .NewAssert(definition, spObject)
                                     .ShouldNotBeNull(spObject)
                                     .ShouldBeEqual(m => m.Name, o => o.ReceiverName)
                                     .ShouldBeEqual(m => m.Class, o => o.ReceiverClass)
                                     .ShouldBeEqual(m => m.Assembly, o => o.ReceiverAssembly)
                                     .SkipProperty(m => m.Data, "Data property is not supported by CSOM. SKipping.")
                                     .ShouldBeEqual(m => m.SequenceNumber, o => o.SequenceNumber)
                                     .ShouldBeEqual(m => m.Synchronization, o => o.GetSynchronization())
                                     .ShouldBeEqual(m => m.Type, o => o.GetEventReceiverType());
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var webModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
            var definition = model.WithAssertAndCast<MasterPageSettingsDefinition>("model", value => value.RequireNotNull());

            var spObject = webModelHost.HostWeb;

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


            if (!string.IsNullOrEmpty(definition.SiteMasterPageUrl))
            {
                assert.ShouldBeEndOf(m => m.SiteMasterPageUrl, o => o.CustomMasterUrl);
            }
            else
            {
                assert.SkipProperty(m => m.SiteMasterPageUrl, "SiteMasterPageUrl is NULL or empty");
            }

            if (!string.IsNullOrEmpty(definition.SystemMasterPageUrl))
            {
                assert.ShouldBeEndOf(m => m.SystemMasterPageUrl, o => o.MasterUrl);
            }
            else
            {
                assert.SkipProperty(m => m.SystemMasterPageUrl, "SystemMasterPageUrl is NULL or empty");
            }

        }
        protected override void DeployModelInternal(object modelHost, DefinitionBase model)
        {
            var siteModelHost = modelHost.WithAssertAndCast<SiteModelHost>("modelHost", value => value.RequireNotNull());
            var definitionModel = model.WithAssertAndCast<ContentTypeDefinition>("model", value => value.RequireNotNull());

            var site = siteModelHost.HostSite;
            var rootWeb = site.RootWeb;

            var contentTypes = rootWeb.AvailableContentTypes;

            var spModel = contentTypes[definitionModel.Name];

            TraceUtils.WithScope(traceScope =>
            {
                Trace.WriteLine(string.Format("Validate model: {0} ContentType:{1}", definitionModel, spModel));

                // assert base properties
                traceScope.WithTraceIndent(trace =>
                {
                    trace.WriteLine(string.Format("Validate Name: model:[{0}] ct:[{1}]", definitionModel.Name, spModel.Name));
                    Assert.AreEqual(definitionModel.Name, spModel.Name);

                    trace.WriteLine(string.Format("Validate Description: model:[{0}] ct:[{1}]", definitionModel.Description, spModel.Description));
                    Assert.AreEqual(definitionModel.Description, spModel.Description);

                    trace.WriteLine(string.Format("Validate Id: model:[{0}] ct:[{1}]", definitionModel.GetContentTypeId(), spModel.Id));
                    Assert.AreEqual(new SPContentTypeId(definitionModel.GetContentTypeId()), spModel.Id);

                    trace.WriteLine(string.Format("Validate Group: model:[{0}] ct:[{1}]", definitionModel.Group, spModel.Group));
                    Assert.AreEqual(definitionModel.Group, spModel.Group);
                });
            });
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var webAppModelHost = modelHost.WithAssertAndCast<WebApplicationModelHost>("modelHost", value => value.RequireNotNull());
            var siteModel = model.WithAssertAndCast<SiteDefinition>("model", value => value.RequireNotNull());

            DeploySite(webAppModelHost, webAppModelHost.HostWebApplication, siteModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var securableObject = ExtractSecurableObject(modelHost);
            var breakRoleInheritanceModel = model.WithAssertAndCast<BreakRoleInheritanceDefinition>("model", value => value.RequireNotNull());

            ProcessRoleInheritance(modelHost, securableObject, breakRoleInheritanceModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var securableObject = modelHost.WithAssertAndCast<SecurableObject>("modelHost", value => value.RequireNotNull());
            var securityGroupLinkModel = model.WithAssertAndCast<SecurityGroupLinkDefinition>("model", value => value.RequireNotNull());

            var web = GetWebFromSPSecurableObject(securableObject);

            var context = web.Context;

            context.Load(web, w => w.SiteGroups);
            context.Load(securableObject, s => s.RoleAssignments.Include(r => r.Member));

            context.ExecuteQuery();

            var securityGroup = WebExtensions.FindGroupByName(web.SiteGroups, securityGroupLinkModel.SecurityGroupName);

            TraceUtils.WithScope(traceScope =>
            {
                traceScope.WriteLine(string.Format("Validate model:[{0}] securableObject:[{1}]", securityGroupLinkModel, securityGroup));

                traceScope.WithTraceIndent(trace =>
                {
                    // asserting it exists
                    trace.WriteLine(string.Format("Validating existance..."));

                    var existingRoleAssignments = FindClientRoleRoleAssignment(securableObject.RoleAssignments, securityGroup);

                    Assert.IsNotNull(existingRoleAssignments);

                    trace.WriteLine(string.Format("RoleAssignments for security group link [{0}] exists.", securityGroupLinkModel.SecurityGroupName));
                });
            });
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var siteModelHost = modelHost.WithAssertAndCast<AdminSiteModelHost>("modelHost", value => value.RequireNotNull());
            var o365SiteModel = model.WithAssertAndCast<O365SiteDefinition>("model", value => value.RequireNotNull());

            DeployO365SiteCollection(modelHost, siteModelHost.HostClientContext, o365SiteModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            if (modelHost is WebModelHost)
            {
                var workflowWebSubscriptionModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
                var definition = model.WithAssertAndCast<SP2013WorkflowSubscriptionDefinition>("model", value => value.RequireNotNull());

                var web = workflowWebSubscriptionModelHost.HostWeb;

                var spObject = GetCurrentWebWorkflowSubscriptioBySourceId(workflowWebSubscriptionModelHost,
                       web,
                       web.ID,
                       definition);

                ValidateWorkflowSubscription(modelHost, workflowWebSubscriptionModelHost.HostWeb, spObject, definition);
            }

            if (modelHost is ListModelHost)
            {
                var workflowSubscriptionModelHost = modelHost.WithAssertAndCast<ListModelHost>("modelHost", value => value.RequireNotNull());
                var definition = model.WithAssertAndCast<SP2013WorkflowSubscriptionDefinition>("model", value => value.RequireNotNull());

                var list = workflowSubscriptionModelHost.HostList;
                var web = list.ParentWeb;

                var spObject = GetCurrentWebWorkflowSubscriptioBySourceId(workflowSubscriptionModelHost,
                      web,
                      list.ID,
                      definition);

                ValidateWorkflowSubscription(modelHost, web, spObject, definition);
            }
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            base.DeployModel(modelHost, model);

            var listItemModelHost = modelHost.WithAssertAndCast<ListItemModelHost>("modelHost", value => value.RequireNotNull());
            var definition = model.WithAssertAndCast<ClientWebPartDefinition>("model", value => value.RequireNotNull());

            //var pageItem = listItemModelHost.HostListItem;

            WithExistingWebPart(listItemModelHost.HostFile, definition, spObject =>
            {
                var assert = ServiceFactory.AssertService
                                           .NewAssert(model, definition, spObject)
                                                 .ShouldNotBeNull(spObject);

                // some of the properties can actually be validated
                // http://stackoverflow.com/questions/11814829/how-to-read-webpart-content-using-sharepoint-client-om
                // asmx calls are required to get additional information about the current web parts

                assert
                    .SkipProperty(m => m.ZoneIndex, "Property is not available in CSOM. Skipping.")

                    .SkipProperty(m => m.Id, "Property is not available in CSOM. Skipping.")
                    .SkipProperty(m => m.ZoneId, "Property is not available in CSOM. Skipping.")

                    .SkipProperty(m => m.WebpartFileName, "Property is not available in CSOM. Skipping.")
                    .SkipProperty(m => m.WebpartType, "Property is not available in CSOM. Skipping.")
                    .SkipProperty(m => m.WebpartXmlTemplate, "Property is not available in CSOM. Skipping.")

                    .ShouldBeEqual(m => m.Title, o => o.Title);
            });
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var folder = ExtractFolderFromModelHost(modelHost);
            var welcomePgaeModel = model.WithAssertAndCast<WelcomePageDefinition>("model", value => value.RequireNotNull());

            DeployWelcomePage(modelHost, model, folder, welcomePgaeModel);
        }
        public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            var folderModelHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull());
            var folderModel = model.WithAssertAndCast<FolderDefinition>("model", value => value.RequireNotNull());

            if (folderModelHost.CurrentLibrary != null)
            {
                var currentFolder = EnsureLibraryFolder(folderModelHost, folderModel);

                var newContext = new FolderModelHost
                {
                    CurrentLibrary = folderModelHost.CurrentLibrary,
                    CurrentLibraryFolder = currentFolder
                };

                action(newContext);
            }
            else if (folderModelHost.CurrentList != null)
            {
                var currentListItem = EnsureListFolder(folderModelHost, folderModel);

                var newContext = new FolderModelHost
                {
                    CurrentList = folderModelHost.CurrentList,
                    CurrentListItem = currentListItem
                };

                action(newContext);
            }
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var farmModelHost = modelHost.WithAssertAndCast<FarmModelHost>("modelHost", value => value.RequireNotNull());
            var solutionModel = model.WithAssertAndCast<FarmSolutionDefinition>("model", value => value.RequireNotNull());

            DeploySolution(farmModelHost, solutionModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var webModelHost = modelHost.WithAssertAndCast<WebModelHost>("modelHost", value => value.RequireNotNull());
            var definition = model.WithAssertAndCast<RegionalSettingsDefinition>("model", value => value.RequireNotNull());

            DeployRegionalSettings(modelHost, webModelHost.HostWeb, definition);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var workflowAssociationModel = model.WithAssertAndCast<WorkflowAssociationDefinition>("model", value => value.RequireNotNull());

            if (modelHost is ListModelHost)
            {
                var listModelHost = (modelHost as ListModelHost);
                var list = listModelHost.HostList;

                DeployListWorkflowAssociationDefinition(listModelHost, list, workflowAssociationModel);
            }
            else if (modelHost is WebModelHost)
            {
                var webModelHost = (modelHost as WebModelHost);
                var web = webModelHost.HostWeb;

                DeployWebWorkflowAssociationDefinition(webModelHost, web, workflowAssociationModel);
            }
            else if (modelHost is ModelHostContext)
            {
                var contentType = (modelHost as ModelHostContext).ContentType;

                DeployContentTypeWorkflowAssociationDefinition(modelHost, contentType, workflowAssociationModel);
            }
            else
            {
                throw new SPMeta2NotSupportedException("model host should be of type ListModelHost or WebModelHost");
            }
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var siteModelHost = modelHost.WithAssertAndCast<TermStoreModelHost>("modelHost", value => value.RequireNotNull());
            var groupModel = model.WithAssertAndCast<TaxonomyTermGroupDefinition>("model", value => value.RequireNotNull());

            DeployTaxonomyGroup(modelHost, siteModelHost, groupModel);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var securableObject = ExtractSecurableObject(modelHost);
            var definition = model.WithAssertAndCast<ResetRoleInheritanceDefinition>("model", value => value.RequireNotNull());

            var context = securableObject.Context;

            if (!securableObject.IsObjectPropertyInstantiated("HasUniqueRoleAssignments"))
            {
                context.Load(securableObject, s => s.HasUniqueRoleAssignments);
                context.ExecuteQueryWithTrace();
            }

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

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

                return new PropertyValidationResult
                {
                    Tag = p.Tag,
                    Src = null,
                    Dst = dstProp,
                    IsValid = d.HasUniqueRoleAssignments == false
                };
            });
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var folder = modelHost.WithAssertAndCast<SPFolder>("modelHost", value => value.RequireNotNull());
            var moduleFile = model.WithAssertAndCast<ModuleFileDefinition>("model", value => value.RequireNotNull());

            ProcessFile(modelHost, folder, moduleFile);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var properties = ExtractProperties(modelHost);
            var property = model.WithAssertAndCast<PropertyDefinition>("model", value => value.RequireNotNull());

            DeployProperty(modelHost, properties, property);
        }