private void MapProperties(ListItem currentItem, FolderDefinition definition) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { currentItem[BuiltInInternalFieldNames.ContentTypeId] = definition.ContentTypeId; } else if (!string.IsNullOrEmpty(definition.ContentTypeName)) { currentItem[BuiltInInternalFieldNames.ContentTypeId] = ContentTypeLookupService .LookupContentTypeByName(currentItem.ParentList, definition.ContentTypeName) .Id.ToString(); } }
protected string ResolveContentTypeId(FolderModelHost folderHost, ModuleFileDefinition moduleFile) { var context = folderHost.CurrentListFolder.Context; var list = folderHost.CurrentList; var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(moduleFile.ContentTypeId)) { stringCustomContentType = moduleFile.ContentTypeId; } else if (!string.IsNullOrEmpty(moduleFile.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService.LookupContentTypeByName(list, moduleFile.ContentTypeName).Id.ToString(); } return(stringCustomContentType); }
protected virtual void MapFolderProperties(SPListItem item, FolderDefinition definition) { if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { var list = item.ParentList; if (!string.IsNullOrEmpty(definition.ContentTypeId)) { item["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { item["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } }
protected virtual void MapListItemProperties(ListItem currentItem, ListItemDefinition definition) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { currentItem[BuiltInInternalFieldNames.ContentTypeId] = definition.ContentTypeId; } else if (!string.IsNullOrEmpty(definition.ContentTypeName)) { currentItem[BuiltInInternalFieldNames.ContentTypeId] = ContentTypeLookupService .LookupContentTypeByName(currentItem.ParentList, definition.ContentTypeName) .Id.ToString(); } FieldLookupService.EnsureDefaultValues(currentItem, definition.DefaultValues); currentItem[BuiltInInternalFieldNames.Title] = definition.Title; FieldLookupService.EnsureValues(currentItem, definition.Values, true); }
protected virtual void MapListItemProperties(SPListItem item, ListItemDefinition definition) { FieldLookupService.EnsureDefaultValues(item, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { var list = item.ParentList; if (!string.IsNullOrEmpty(definition.ContentTypeId)) { item["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { item["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } item[BuiltInInternalFieldNames.Title] = definition.Title; FieldLookupService.EnsureValues(item, definition.Values, true); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var list = folderModelHost.CurrentList; var definition = model.WithAssertAndCast <PublishingPageDefinition>("model", value => value.RequireNotNull()); var contentTypeId = string.Empty; // pre load content type if (!string.IsNullOrEmpty(definition.ContentTypeId)) { contentTypeId = definition.ContentTypeId; } else if (!string.IsNullOrEmpty(definition.ContentTypeName)) { contentTypeId = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Id.ToString(); } var context = folder.Context; var pageName = GetSafePageFileName(definition); var currentPageFile = GetCurrentPage(list, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f => { // create if only file does not exist // overriting spoils the fields's content // Investigate publishing page provision, Content property #744 // https://github.com/SubPointSolutions/spmeta2/issues/744 if (f == null || !f.Exists) { var file = new FileCreationInformation(); var pageContent = PublishingPageTemplates.RedirectionPageMarkup; file.Url = pageName; file.Content = Encoding.UTF8.GetBytes(pageContent); file.Overwrite = definition.NeedOverride; return(folder.Files.Add(file)); } return(f); }, newFile => { var newFileItem = newFile.ListItemAllFields; context.Load(newFileItem); context.ExecuteQueryWithTrace(); var site = folderModelHost.HostSite; var currentPageLayoutItem = FindPageLayoutItem(site, definition.PageLayoutFileName); var currentPageLayoutItemContext = currentPageLayoutItem.Context; var publishingFile = currentPageLayoutItem.File; currentPageLayoutItemContext.Load(currentPageLayoutItem); currentPageLayoutItemContext.Load(currentPageLayoutItem, i => i.DisplayName); currentPageLayoutItemContext.Load(publishingFile); currentPageLayoutItemContext.ExecuteQueryWithTrace(); // settig up dfault values if there is PublishingPageLayout setup FieldLookupService.EnsureDefaultValues(newFileItem, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.Title)) { newFileItem[BuiltInInternalFieldNames.Title] = definition.Title; } if (!string.IsNullOrEmpty(definition.Description)) { newFileItem[BuiltInInternalFieldNames.Comments] = definition.Description; } if (!string.IsNullOrEmpty(definition.Content)) { newFileItem[BuiltInInternalPublishingFieldNames.PublishingPageContent] = definition.Content; } newFileItem[BuiltInInternalFieldNames.PublishingPageLayout] = publishingFile.ServerRelativeUrl + ", " + currentPageLayoutItem.DisplayName; var associatedContentTypeStringValue = ConvertUtils.ToString(currentPageLayoutItem[BuiltInInternalFieldNames.PublishingAssociatedContentType]); if (!string.IsNullOrEmpty(associatedContentTypeStringValue)) { var contentTypeValues = associatedContentTypeStringValue.Split(new string[] { ";#" }, StringSplitOptions.None); var associatedContentTypeName = contentTypeValues[1]; var associatedContentTypeId = contentTypeValues[2]; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = associatedContentTypeId; } if (!string.IsNullOrEmpty(contentTypeId)) { newFileItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } FieldLookupService.EnsureValues(newFileItem, definition.Values, true); newFileItem.Update(); context.ExecuteQueryWithTrace(); }); currentPageFile = GetCurrentPage(folderModelHost.CurrentList, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var definition = model.WithAssertAndCast <HtmlMasterPageDefinition>("model", value => value.RequireNotNull()); var spObject = FindPage(folderModelHost.CurrentList, folder, definition); var spFile = FindPageFile(folderModelHost.CurrentList, folder, definition); var context = spObject.Context; context.Load(spObject); context.Load(spObject, o => o.DisplayName); context.Load(spFile); context.Load(spFile, o => o.ServerRelativeUrl); context.ExecuteQueryWithTrace(); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.FileName, o => o.GetFileName()) .ShouldBeEqual(m => m.DefaultCSSFile, o => o.GetDefaultCSSFile()) .ShouldBeEqual(m => m.Description, o => o.GetMasterPageDescription()) .ShouldBeEqual(m => m.Title, o => o.GetTitle()); // check that there is a .master page with the same name in the library var associatedMasterPageName = definition.FileName.ToLower().EndsWith(".html") ? definition.FileName.ToLower().Replace(".html", ".master") : definition.FileName + ".master"; var associatedMasterPage = FindPageByName(folderModelHost.CurrentList, folder, associatedMasterPageName); assert.ShouldNotBeNull(associatedMasterPage); if (definition.UIVersion.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.UIVersion); var dstPropValue = d.GetUIVersion(); var isValid = true; foreach (var v in s.UIVersion) { if (!dstPropValue.Contains(v)) { isValid = false; break; } } return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(d => d.UIVersion, "UIVersion.Count is 0. Skipping"); } assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Content); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); var isContentValid = false; byte[] dstContent = null; folderModelHost.HostClientContext.ExecuteQueryWithTrace(); using (var stream = File.OpenBinaryDirect(folderModelHost.HostClientContext, spFile.ServerRelativeUrl).Stream) dstContent = ModuleFileUtils.ReadFully(stream); var srcStringContent = Encoding.UTF8.GetString(s.Content); var dstStringContent = Encoding.UTF8.GetString(dstContent); // same or greater, length // too tricky to compare the content //isContentValid = dstStringContent.Contains(srcStringContent); isContentValid = dstStringContent.Length >= srcStringContent.Length; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, // Dst = dstProp, IsValid = isContentValid }); }); if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual(m => m.ContentTypeName, o => o.GetContentTypeName()); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is NULL. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WebPartPageDefinition>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var context = folder.Context; var pageName = GetSafeWebPartPageFileName(definition); var pageFile = GetCurrentWebPartPageFile(folderModelHost.CurrentList, folder, pageName); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var spObject = pageFile.ListItemAllFields; context.Load(spObject); context.Load(spObject, s => s.ContentType); context.Load(spObject, s => s.File); context.ExecuteQueryWithTrace(); var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.FileName, o => o.GetFileLeafRef()); assert.SkipProperty(m => m.Title, "Web part pages don't have title. Skipping."); if (!string.IsNullOrEmpty(definition.CustomPageLayout)) { var custmPageContent = Encoding.UTF8.GetBytes(definition.CustomPageLayout); var pageTemplateContent = definition.GetWebPartPageTemplateContent(); byte[] dstContent = null; using (var stream = File.OpenBinaryDirect(folderModelHost.HostClientContext, spObject.File.ServerRelativeUrl).Stream) dstContent = ModuleFileUtils.ReadFully(stream); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.PageLayoutTemplate); var isValidPageLayoutTemplate = custmPageContent.SequenceEqual(dstContent); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, //Dst = dstProp, IsValid = isValidPageLayoutTemplate }); }); assert.SkipProperty(m => m.PageLayoutTemplate, "PageLayoutTemplate validated with GetWebPartPageTemplateContent() call before."); assert.SkipProperty(m => m.CustomPageLayout, "CustomPageLayout validated with GetCustomnPageContent() call before."); } else { assert.SkipProperty(m => m.CustomPageLayout, "CustomPageLayout is null or empty. Skipping."); } if (definition.PageLayoutTemplate > 0) { var pageTemplateContent = definition.GetWebPartPageTemplateContent(); byte[] dstContent = null; using (var stream = File.OpenBinaryDirect(folderModelHost.HostClientContext, spObject.File.ServerRelativeUrl).Stream) dstContent = ModuleFileUtils.ReadFully(stream); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.PageLayoutTemplate); var isValidPageLayoutTemplate = pageTemplateContent.SequenceEqual(dstContent); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, //Dst = dstProp, IsValid = isValidPageLayoutTemplate }); }); assert.SkipProperty(m => m.PageLayoutTemplate, "PageLayoutTemplate validated with GetWebPartPageTemplateContent() call before."); } else { assert.SkipProperty(m => m.CustomPageLayout, "PageLayoutTemplate is o or less. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <FolderDefinition>("model", value => value.RequireNotNull()); SPFolder spObject = null; if (folderModelHost.CurrentLibrary != null) { spObject = GetLibraryFolder(folderModelHost, definition); } else if (folderModelHost.CurrentList != null) { spObject = GetListFolder(folderModelHost, definition); } var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldBeEqual(m => m.Name, o => o.Name); var item = spObject.Item; var stringCustomContentTypeId = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { var ct = ContentTypeLookupService .LookupContentTypeByName(item.ParentList, definition.ContentTypeName); stringCustomContentTypeId = ct.ToString(); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { var ct = ContentTypeLookupService .LookupListContentTypeById(item.ParentList, definition.ContentTypeId); stringCustomContentTypeId = ct.ToString(); } } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeId); var currentContentTypeId = ConvertUtils.ToString(item["ContentTypeId"]); var isValis = currentContentTypeId.StartsWith(s.ContentTypeId); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); // still validating agains content type ID. // setting up by Name, the item must have correct ID var currentContentTypeId = ConvertUtils.ToString(item["ContentTypeId"]); var isValis = stringCustomContentTypeId.StartsWith(currentContentTypeId); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WikiPageDefinition>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentLibraryFolder; var list = folderModelHost.CurrentLibrary; //if (!string.IsNullOrEmpty(wikiPageModel.FolderUrl)) // throw new Exception("FolderUrl property is not supported yet!"); var pageItem = FindWikiPageItem(folder, definition); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = pageItem == null ? null : pageItem.File, ObjectType = typeof(SPFile), ObjectDefinition = model, ModelHost = modelHost }); if (pageItem == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new wiki page"); var newWikiPageUrl = GetSafeWikiPageUrl(folder, definition); var newpage = folder.Files.Add(newWikiPageUrl, SPTemplateFileType.WikiPage); FieldLookupService.EnsureDefaultValues(newpage.ListItemAllFields, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { newpage.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { newpage.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } newpage.ListItemAllFields[SPBuiltInFieldId.WikiField] = definition.Content ?? string.Empty; FieldLookupService.EnsureValues(newpage.ListItemAllFields, definition.Values, true); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newpage, ObjectType = typeof(SPFile), ObjectDefinition = model, ModelHost = modelHost }); newpage.ListItemAllFields.Update(); newpage.Update(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing wiki page"); if (definition.NeedOverride) { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "NeedOverride = true. Updating wiki page content."); FieldLookupService.EnsureDefaultValues(pageItem, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { pageItem["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { pageItem["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } pageItem[SPBuiltInFieldId.WikiField] = definition.Content ?? string.Empty; FieldLookupService.EnsureValues(pageItem, definition.Values, true); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "NeedOverride = false. Skipping Updating wiki page content."); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = pageItem.File, ObjectType = typeof(SPFile), ObjectDefinition = model, ModelHost = modelHost }); pageItem.Update(); pageItem.File.Update(); } }
private void DeployPublishingPage(object modelHost, SPList list, SPFolder folder, PublishingPageLayoutDefinition definition) { var web = list.ParentWeb; var targetPage = GetCurrentObject(folder, definition); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = targetPage == null ? null : targetPage.File, ObjectType = typeof(SPFile), ObjectDefinition = definition, ModelHost = modelHost }); if (targetPage == null) { targetPage = CreateObject(modelHost, folder, definition); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = targetPage.File, ObjectType = typeof(SPFile), ObjectDefinition = definition, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, folder, targetPage.Url, GetSafePageFileName(definition), Encoding.UTF8.GetBytes(definition.Content), definition.NeedOverride, null, afterFile => { //var pageItem = afterFile.Properties; var pageItem = afterFile.ListItemAllFields; FieldLookupService.EnsureDefaultValues(pageItem, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.Title)) { //pageItem["vti_title"] = definition.Title; pageItem["Title"] = definition.Title; } // ootb ? pageItem[BuiltInInternalFieldNames.ContentTypeId] = BuiltInPublishingContentTypeId.PageLayout; // custom? if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { pageItem["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { pageItem["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } if (!string.IsNullOrEmpty(definition.Description)) { // did not work pageItem["MasterPageDescription"] = definition.Description; //pageItem.Properties["MasterPageDescription"] = definition.Description; } if (!string.IsNullOrEmpty(definition.PreviewImageUrl)) { var urlValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext { Value = definition.PreviewImageUrl, Context = web }).Value; var urlFieldValue = new SPFieldUrlValue { Url = urlValue }; if (!string.IsNullOrEmpty(definition.PreviewImageDescription)) { urlFieldValue.Description = definition.PreviewImageDescription; } pageItem["PublishingPreviewImage"] = urlFieldValue.ToString(); } if (!string.IsNullOrEmpty(definition.AssociatedContentTypeId)) { var siteContentType = web.AvailableContentTypes[new SPContentTypeId(definition.AssociatedContentTypeId)]; pageItem["PublishingAssociatedContentType"] = String.Format(";#{0};#{1};#", siteContentType.Name, siteContentType.Id.ToString()); } FieldLookupService.EnsureValues(pageItem, definition.Values, true); pageItem.Update(); }); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var definition = model.WithAssertAndCast <TemplateDefinitionBase>("model", value => value.RequireNotNull()); var spFile = GetItemFile(folderModelHost.CurrentList, folder, definition.FileName); var spObject = spFile.ListItemAllFields; var context = spObject.Context; context.Load(spObject); context.Load(spFile, f => f.ServerRelativeUrl); context.ExecuteQueryWithTrace(); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.Title, o => o.GetTitle()) .ShouldBeEqual(m => m.FileName, o => o.GetFileName()) //.ShouldBeEqual(m => m.CrawlerXSLFile, o => o.GetCrawlerXSLFile()) .ShouldBeEqual(m => m.HiddenTemplate, o => o.GetHiddenTemplate()) //.ShouldBeEqual(m => m.Description, o => o.GetMasterPageDescription()) ; if (!string.IsNullOrEmpty(definition.Description)) { assert.ShouldBeEqual(m => m.Description, o => o.GetMasterPageDescription()); } else { assert.SkipProperty(m => m.Description, "Description is null. Skiping."); } assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Content); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); var isContentValid = false; byte[] dstContent = null; using (var stream = File.OpenBinaryDirect(folderModelHost.HostClientContext, spFile.ServerRelativeUrl).Stream) dstContent = ModuleFileUtils.ReadFully(stream); var srcStringContent = Encoding.UTF8.GetString(s.Content); var dstStringContent = Encoding.UTF8.GetString(dstContent); isContentValid = dstStringContent.Contains(srcStringContent); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, // Dst = dstProp, IsValid = isContentValid }); }); if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual(m => m.ContentTypeName, o => o.GetContentTypeName()); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is NULL. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <WikiPageDefinition>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var context = folder.Context; var pageName = GetSafeWikiPageFileName(definition); var file = GetWikiPageFile(folderModelHost.CurrentList.ParentWeb, folder, definition); var spObject = file.ListItemAllFields; context.Load(spObject); context.Load(spObject, o => o.ContentType); context.ExecuteQueryWithTrace(); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var assert = ServiceFactory.AssertService.NewAssert(definition, spObject); assert .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.FileName, o => o.GetFileLeafRef()) .SkipProperty(m => m.Title, "Title field is not available for wiki pages."); if (!string.IsNullOrEmpty(definition.Content)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Content); var srcContent = Regex.Replace(definition.Content, @"<[^>]*>", string.Empty); var dstContent = Regex.Replace(spObject.GetWikiField(), @"<[^>]*>", string.Empty); // crazy lazy var isValid = dstContent.Trim().Replace((char)8203, char.Parse(" ")).Replace(" ", "") == srcContent.Trim().Replace((char)8203, char.Parse(" ")).Replace(" ", ""); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, //Dst = dstProp, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Content, "Content is NULL. Skiping."); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var definition = model.WithAssertAndCast <PublishingPageLayoutDefinition>("model", value => value.RequireNotNull()); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var spObject = FindPublishingPage(folderModelHost.CurrentList, folder, definition); var spFile = spObject.File; var context = spObject.Context; context.Load(spObject); context.Load(spObject, o => o.DisplayName); context.Load(spObject, o => o.ContentType); context.Load(spFile, f => f.ServerRelativeUrl); context.ExecuteQueryWithTrace(); var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.FileName, o => o.GetFileName()); if (!string.IsNullOrEmpty(definition.Title)) { assert.ShouldBeEqual(m => m.Title, o => o.GetTitle()); } else { assert.SkipProperty(m => m.Title); } if (!string.IsNullOrEmpty(definition.Description)) { assert.ShouldBeEqual(m => m.Description, o => o.GetPublishingPageLayoutDescription()); } else { assert.SkipProperty(m => m.Description); } if (!string.IsNullOrEmpty(definition.AssociatedContentTypeId)) { assert.ShouldBeEndOf(m => m.AssociatedContentTypeId, o => o.GetPublishingPageLayoutAssociatedContentTypeId()); } else { assert.SkipProperty(m => m.AssociatedContentTypeId, "AssociatedContentTypeId is null or empty."); } if (!string.IsNullOrEmpty(definition.PreviewImageUrl)) { var urlValue = spObject.FieldValues["PublishingPreviewImage"] as FieldUrlValue; assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.PreviewImageUrl); var isValid = (urlValue != null) && (urlValue.Url == s.PreviewImageUrl); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.PreviewImageDescription); var isValid = (urlValue != null) && (urlValue.Description == s.PreviewImageDescription); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.PreviewImageUrl, "MasterPageUrl is NULL"); assert.SkipProperty(m => m.PreviewImageDescription, "MasterPageDescription is NULL"); } assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Content); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); var isContentValid = false; byte[] dstContent = null; using (var stream = File.OpenBinaryDirect(folderModelHost.HostClientContext, spFile.ServerRelativeUrl).Stream) dstContent = ModuleFileUtils.ReadFully(stream); var srcStringContent = s.Content; var dstStringContent = Encoding.UTF8.GetString(dstContent); srcStringContent = srcStringContent .Replace("meta:webpartpageexpansion=\"full\" ", string.Empty); isContentValid = dstStringContent.Contains(srcStringContent); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, // Dst = dstProp, IsValid = isContentValid }); }); if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost as FolderModelHost; var definition = model as WebPartPageDefinition; var contentTypeId = string.Empty; // pre load content type if (!string.IsNullOrEmpty(definition.ContentTypeId)) { contentTypeId = definition.ContentTypeId; } else if (!string.IsNullOrEmpty(definition.ContentTypeName)) { contentTypeId = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Id.ToString(); } Folder folder = folderModelHost.CurrentListFolder; //if (!string.IsNullOrEmpty(webPartPageModel.FolderUrl)) // throw new NotImplementedException("FolderUrl for the web part page model is not supported yet"); var context = folder.Context; // #SPBug // it turns out that there is no support for the web part page creating via CMOM // we we need to get a byte array to 'hack' this pages out.. // http://stackoverflow.com/questions/6199990/creating-a-sharepoint-2010-page-via-the-client-object-model // http://social.technet.microsoft.com/forums/en-US/sharepointgeneralprevious/thread/6565bac1-daf0-4215-96b2-c3b64270ec08 var currentPage = GetCurrentWebPartPageFile(folderModelHost.CurrentList, folder, GetSafeWebPartPageFileName(definition)); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentPage, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); if ((currentPage == null) || (currentPage != null && definition.NeedOverride)) { if (definition.NeedOverride) { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part page"); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "NeedOverride = true. Replacing web part page."); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new web part page"); } var file = new FileCreationInformation(); var pageContent = string.Empty; if (!string.IsNullOrEmpty(definition.CustomPageLayout)) { pageContent = definition.CustomPageLayout; } else { pageContent = GetWebPartTemplateContent(definition); } var fileName = GetSafeWebPartPageFileName(definition); file.Url = fileName; file.Content = Encoding.UTF8.GetBytes(pageContent); file.Overwrite = definition.NeedOverride; var newFile = folder.Files.Add(file); FieldLookupService.EnsureDefaultValues(newFile.ListItemAllFields, definition.DefaultValues); if (!string.IsNullOrEmpty(contentTypeId)) { newFile.ListItemAllFields[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } FieldLookupService.EnsureValues(newFile.ListItemAllFields, definition.Values, true); if (definition.Values.Any() || definition.DefaultValues.Any() || !string.IsNullOrEmpty(contentTypeId)) { newFile.ListItemAllFields.Update(); context.ExecuteQueryWithTrace(); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); context.Load(newFile); context.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part page"); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "NeedOverride = false. Skipping replacing web part page."); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentPage, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); } }
private SPFile GetOrCreateNewWebPartFile(object modelHost, SPFolder folder, WebPartPageDefinition definition) { var list = folder.DocumentLibrary; var targetFile = FindWebPartPage(folder, definition); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = targetFile, ObjectType = typeof(SPFile), ObjectDefinition = definition, ModelHost = modelHost }); if (targetFile == null || definition.NeedOverride) { if (definition.NeedOverride) { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part page"); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "NeedOverride = true. Replacing web part page."); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new web part page"); } var webPartPageName = GetSafeWebPartPageFileName(definition); byte[] fileContent = null; if (!string.IsNullOrEmpty(definition.CustomPageLayout)) { fileContent = Encoding.UTF8.GetBytes(definition.CustomPageLayout); } else { fileContent = Encoding.UTF8.GetBytes(GetWebPartPageTemplateContent(definition)); } ModuleFileModelHandler.DeployModuleFile(folder, SPUrlUtility.CombineUrl(folder.ServerRelativeUrl, webPartPageName), webPartPageName, fileContent, true, file => { }, after => { FieldLookupService.EnsureDefaultValues(after.ListItemAllFields, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { after.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { after.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } FieldLookupService.EnsureValues(after.ListItemAllFields, definition.Values, true); if (definition.DefaultValues.Any() || definition.Values.Any() || !string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { after.ListItemAllFields.Update(); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = after, ObjectType = typeof(SPFile), ObjectDefinition = definition, ModelHost = modelHost }); }); targetFile = FindWebPartPage(folder, definition); } else { FieldLookupService.EnsureDefaultValues(targetFile.ListItemAllFields, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.ContentTypeId) || !string.IsNullOrEmpty(definition.ContentTypeName)) { if (!string.IsNullOrEmpty(definition.ContentTypeId)) { targetFile.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { targetFile.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName); } } TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part page"); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "NeedOverride = false. Skipping replacing web part page."); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = targetFile, ObjectType = typeof(SPFile), ObjectDefinition = definition, ModelHost = modelHost }); targetFile.Update(); } return(targetFile); }
protected virtual void ValidateProperties(ListItem item, ListItemDefinition definition) { var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(item.ParentList, definition.ContentTypeName) .Name; } } var assert = ServiceFactory.AssertService .NewAssert(definition, item) .ShouldNotBeNull(item); assert .ShouldBeEqual(m => m.Title, o => o.DisplayName); if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
private void DeployWikiPage(Web web, List list, Folder folder, WikiPageDefinition definition) { var context = folder.Context; var newWikiPageUrl = string.Empty; var contentTypeId = string.Empty; // pre load content type if (!string.IsNullOrEmpty(definition.ContentTypeId)) { contentTypeId = definition.ContentTypeId; } else if (!string.IsNullOrEmpty(definition.ContentTypeName)) { contentTypeId = ContentTypeLookupService .LookupContentTypeByName(list, definition.ContentTypeName) .Id.ToString(); } var file = GetWikiPageFile(web, folder, definition, out newWikiPageUrl); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = file, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = folder }); if (file == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new wiki page"); var newPageFile = folder.Files.AddTemplateFile(newWikiPageUrl, TemplateFileType.WikiPage); context.Load(newPageFile); var currentListItem = newPageFile.ListItemAllFields; context.Load(currentListItem); context.ExecuteQueryWithTrace(); FieldLookupService.EnsureDefaultValues(currentListItem, definition.DefaultValues); if (!string.IsNullOrEmpty(contentTypeId)) { currentListItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } currentListItem[BuiltInInternalFieldNames.WikiField] = definition.Content ?? String.Empty; FieldLookupService.EnsureValues(currentListItem, definition.Values, true); currentListItem.Update(); context.ExecuteQueryWithTrace(); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newPageFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = folder }); context.ExecuteQueryWithTrace(); } else { // TODO,override if force TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing wiki page"); if (definition.NeedOverride) { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "NeedOverride = true. Updating wiki page content."); var currentListItem = file.ListItemAllFields; context.Load(currentListItem); context.ExecuteQueryWithTrace(); FieldLookupService.EnsureDefaultValues(currentListItem, definition.DefaultValues); if (!string.IsNullOrEmpty(contentTypeId)) { currentListItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } currentListItem[BuiltInInternalFieldNames.WikiField] = definition.Content ?? String.Empty; currentListItem.Update(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "NeedOverride = false. Skipping Updating wiki page content."); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = file, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = folder }); context.ExecuteQueryWithTrace(); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var csomModelHost = modelHost.WithAssertAndCast <CSOMModelHostBase>("modelHost", value => value.RequireNotNull()); var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <PublishingPageLayoutDefinition>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var list = folderModelHost.CurrentList; ContentType siteContentType = null; var contentTypeId = string.Empty; // pre load content type if (!string.IsNullOrEmpty(definition.ContentTypeId)) { contentTypeId = definition.ContentTypeId; } else if (!string.IsNullOrEmpty(definition.ContentTypeName)) { contentTypeId = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Id.ToString(); } if (!string.IsNullOrEmpty(definition.AssociatedContentTypeId)) { siteContentType = folderModelHost.HostSite.RootWeb.AvailableContentTypes.GetById(definition.AssociatedContentTypeId); folderModelHost.HostSite.Context.Load(siteContentType); folderModelHost.HostSite.Context.ExecuteQueryWithTrace(); } var context = folder.Context; var pageName = GetSafePageFileName(definition); var currentPageFile = GetCurrentPage(list, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f => { var file = new FileCreationInformation(); file.Url = pageName; file.Content = Encoding.UTF8.GetBytes(definition.Content); file.Overwrite = definition.NeedOverride; return(folder.Files.Add(file)); }, newFile => { var newFileItem = newFile.ListItemAllFields; context.Load(newFileItem); context.ExecuteQueryWithTrace(); var site = folderModelHost.HostSite; var currentPageLayoutItem = FindPageLayoutItem(site, definition.FileName); var currentPageLayoutItemContext = currentPageLayoutItem.Context; var publishingFile = currentPageLayoutItem.File; currentPageLayoutItemContext.Load(currentPageLayoutItem); currentPageLayoutItemContext.Load(currentPageLayoutItem, i => i.DisplayName); currentPageLayoutItemContext.Load(publishingFile); currentPageLayoutItemContext.ExecuteQueryWithTrace(); FieldLookupService.EnsureDefaultValues(newFileItem, definition.DefaultValues); if (!string.IsNullOrEmpty(definition.Title)) { newFileItem[BuiltInInternalFieldNames.Title] = definition.Title; } newFileItem["MasterPageDescription"] = definition.Description; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = BuiltInPublishingContentTypeId.PageLayout; // custom? if (!string.IsNullOrEmpty(contentTypeId)) { newFileItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } if (!string.IsNullOrEmpty(definition.PreviewImageUrl)) { var urlValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext { Value = definition.PreviewImageUrl, Context = csomModelHost }).Value; var urlFieldValue = new FieldUrlValue { Url = urlValue }; if (!string.IsNullOrEmpty(definition.PreviewImageDescription)) { urlFieldValue.Description = definition.PreviewImageDescription; } newFileItem["PublishingPreviewImage"] = urlFieldValue; } if (siteContentType != null) { newFileItem["PublishingAssociatedContentType"] = String.Format(";#{0};#{1};#", siteContentType.Name, siteContentType.Id.ToString()); } FieldLookupService.EnsureValues(newFileItem, definition.Values, true); newFileItem.Update(); context.ExecuteQueryWithTrace(); }); currentPageFile = GetCurrentPage(folderModelHost.CurrentList, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = definition, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); }
private void ProcessFile( object modelHost, SPFolder folder, ModuleFileDefinition moduleFile) { DeployModuleFile( folder, GetSafeFileUrl(folder, moduleFile), moduleFile.FileName, moduleFile.Content, moduleFile.Overwrite, before => { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = before.Exists ? before : null, ObjectType = typeof(SPFile), ObjectDefinition = moduleFile, ModelHost = modelHost }); }, after => { var shouldUpdateItem = false; if (!string.IsNullOrEmpty(moduleFile.Title)) { after.ListItemAllFields["Title"] = moduleFile.Title; shouldUpdateItem = true; } if (!string.IsNullOrEmpty(moduleFile.ContentTypeId) || !string.IsNullOrEmpty(moduleFile.ContentTypeName)) { var list = folder.ParentWeb.Lists[folder.ParentListId]; if (!string.IsNullOrEmpty(moduleFile.ContentTypeId)) { after.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, moduleFile.ContentTypeId); } if (!string.IsNullOrEmpty(moduleFile.ContentTypeName)) { after.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, moduleFile.ContentTypeName); } shouldUpdateItem = true; } if (moduleFile.DefaultValues.Count > 0) { FieldLookupService.EnsureDefaultValues(after.ListItemAllFields, moduleFile.DefaultValues); shouldUpdateItem = true; } FieldLookupService.EnsureValues(after.ListItemAllFields, moduleFile.Values, true); if (shouldUpdateItem) { after.ListItemAllFields.Update(); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = after, ObjectType = typeof(SPFile), ObjectDefinition = moduleFile, ModelHost = modelHost }); }); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var definition = model.WithAssertAndCast <WebPartGalleryFileDefinition>("model", value => value.RequireNotNull()); var file = GetItemFile(folderModelHost.CurrentList, folder, definition.FileName); var spObject = file.ListItemAllFields; var context = spObject.Context; context.Load(file, f => f.ServerRelativeUrl); context.Load(spObject); context.ExecuteQueryWithTrace(); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.Title, o => o.GetTitle()) .ShouldBeEqual(m => m.FileName, o => o.GetFileLeafRef()) .ShouldBeEqual(m => m.Description, o => o.GetWebPartGalleryFileDescription()) .ShouldBeEqual(m => m.Group, o => o.GetWebPartGalleryFileGroup()) ; assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.Content); //var dstProp = d.GetExpressionValue(ct => ct.GetId()); var isContentValid = true; byte[] dstContent = null; using (var stream = File.OpenBinaryDirect(folderModelHost.HostClientContext, file.ServerRelativeUrl).Stream) dstContent = ModuleFileUtils.ReadFully(stream); var srcStringContent = Encoding.UTF8.GetString(s.Content); var dstStringContent = Encoding.UTF8.GetString(dstContent); srcStringContent = WebpartXmlExtensions .LoadWebpartXmlDocument(srcStringContent) .SetTitle(s.Title) .SetOrUpdateProperty("Description", s.Description) .ToString(); dstStringContent = WebpartXmlExtensions.LoadWebpartXmlDocument(dstStringContent).ToString(); isContentValid = dstStringContent.Contains(srcStringContent); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, // Dst = dstProp, IsValid = isContentValid }); }); if (definition.RecommendationSettings.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(m => m.RecommendationSettings); var isValid = true; // TODO //var targetControlTypeValue = d.GetWebPartGalleryFileRecommendationSettings(); //var targetControlTypeValues = new List<string>(); //for (var i = 0; i < targetControlTypeValue.Count; i++) // targetControlTypeValues.Add(targetControlTypeValue[i].ToUpper()); //foreach (var v in s.RecommendationSettings) //{ // if (!targetControlTypeValues.Contains(v.ToUpper())) // isValid = false; //} return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.RecommendationSettings, "RecommendationSettings is empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual(m => m.ContentTypeName, o => o.GetContentTypeName()); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is NULL. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var definition = model.WithAssertAndCast <PublishingPageDefinition>("model", value => value.RequireNotNull()); var stringCustomContentType = string.Empty; if (!string.IsNullOrEmpty(definition.ContentTypeName) || !string.IsNullOrEmpty(definition.ContentTypeId)) { if (!string.IsNullOrEmpty(definition.ContentTypeName)) { stringCustomContentType = ContentTypeLookupService .LookupContentTypeByName(folderModelHost.CurrentList, definition.ContentTypeName) .Name; } } var spObject = FindPublishingPage(folderModelHost.CurrentList, folder, definition); var context = spObject.Context; context.Load(spObject); context.Load(spObject, o => o.DisplayName); context.Load(spObject, o => o.ContentType); context.ExecuteQueryWithTrace(); var assert = ServiceFactory.AssertService .NewAssert(definition, spObject) .ShouldNotBeNull(spObject) .ShouldBeEqual(m => m.FileName, o => o.GetFileName()) //.ShouldBeEqual(m => m.Description, o => o.GetPublishingPageDescription()) .ShouldBeEndOf(m => m.PageLayoutFileName, o => o.GetPublishingPagePageLayoutFileName()) .ShouldBeEqual(m => m.Title, o => o.GetTitle()); if (!string.IsNullOrEmpty(definition.Description)) { assert.ShouldBeEqual(m => m.Description, o => o.GetPublishingPageDescription()); } else { assert.SkipProperty(m => m.Description, "Description is NULL. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual(m => m.ContentTypeName, o => o.GetContentTypeName()); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is NULL. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeId)) { // TODO } else { assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping."); } if (!string.IsNullOrEmpty(definition.ContentTypeName)) { assert.ShouldBeEqual((p, s, d) => { var srcProp = s.GetExpressionValue(def => def.ContentTypeName); var currentContentTypeName = d.ContentType.Name; var isValis = stringCustomContentType == currentContentTypeName; return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValis }); }); } else { assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping."); } if (definition.DefaultValues.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.DefaultValues) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.DefaultValues); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.DefaultValues, "DefaultValues.Count == 0. Skipping."); } if (definition.Values.Count > 0) { assert.ShouldBeEqual((p, s, d) => { var isValid = true; foreach (var srcValue in s.Values) { // big TODO here for == != if (!string.IsNullOrEmpty(srcValue.FieldName)) { if (d[srcValue.FieldName].ToString() != srcValue.Value.ToString()) { isValid = false; } } if (!isValid) { break; } } var srcProp = s.GetExpressionValue(def => def.Values); return(new PropertyValidationResult { Tag = p.Tag, Src = srcProp, Dst = null, IsValid = isValid }); }); } else { assert.SkipProperty(m => m.Values, "Values.Count == 0. Skipping."); } }