public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action <object> action) { var listModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = listModelHost.CurrentLibraryFolder; var publishingPageModel = model.WithAssertAndCast <PublishingPageDefinition>("model", value => value.RequireNotNull()); var targetPage = FindPublishingPage(folder, publishingPageModel); ModuleFileModelHandler.WithSafeFileOperation(listModelHost.CurrentLibrary, folder, targetPage.Url, GetSafePublishingPageFileName(publishingPageModel), Encoding.UTF8.GetBytes(PublishingPageTemplates.RedirectionPageMarkup), false, null, afterFile => { using (var webPartManager = afterFile.GetLimitedWebPartManager(PersonalizationScope.Shared)) { var webpartPageHost = new WebpartPageModelHost { HostFile = afterFile, PageListItem = targetPage, SPLimitedWebPartManager = webPartManager }; action(webpartPageHost); } }); }
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; pageItem["vti_title"] = definition.Title; pageItem["MasterPageDescription"] = definition.Description; pageItem[BuiltInInternalFieldNames.ContentTypeId] = BuiltInPublishingContentTypeId.PageLayout; if (!string.IsNullOrEmpty(definition.AssociatedContentTypeId)) { var siteContentType = web.AvailableContentTypes[new SPContentTypeId(definition.AssociatedContentTypeId)]; pageItem["PublishingAssociatedContentType"] = String.Format(";#{0};#{1};#", siteContentType.Name, siteContentType.Id.ToString()); } }); }
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(PublishingPageTemplates.RedirectionPageMarkup), false, null, afterFile => { var siteContentType = web.AvailableContentTypes[new SPContentTypeId(definition.AssociatedContentTypeId)]; var pageItem = afterFile.Item; pageItem[BuiltInFieldId.Title] = definition.Title; pageItem[BuiltInPublishingFieldId.Description] = definition.Description; pageItem[BuiltInFieldId.ContentTypeId] = BuiltInPublishingContentTypeId.PageLayout; //pageItem.SystemUpdate(); pageItem[BuiltInPublishingFieldId.AssociatedContentType] = String.Format(";#{0};#{1};#", siteContentType.Name, siteContentType.Id.ToString()); pageItem.SystemUpdate(); }); }
private void DeployPage(object modelHost, SPList list, SPFolder folder, ContentPageDefinitionBase 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), definition.Content, definition.NeedOverride, null, afterFile => { var pageItem = afterFile.Properties; pageItem["vti_title"] = definition.Title; MapProperties(modelHost, pageItem, definition); //pageItem.SystemUpdate(); }); }
private SPListItem CreateObject(object modelHost, SPFolder folder, MasterPageDefinitionBase definition) { var pageName = GetSafePageFileName(definition); var fileContent = definition.Content; ModuleFileModelHandler.DeployModuleFile(folder, SPUrlUtility.CombineUrl(folder.ServerRelativeUrl, pageName), pageName, fileContent, true, null, null); return(GetCurrentObject(folder, definition)); }
private SPListItem CreateObject(object modelHost, SPFolder folder, PublishingPageLayoutDefinition definition) { var pageName = GetSafePageFileName(definition); var fileContent = Encoding.UTF8.GetBytes(definition.Content); ModuleFileModelHandler.DeployModuleFile(folder, SPUrlUtility.CombineUrl(folder.ServerRelativeUrl, pageName), pageName, fileContent, true, null, null); return(GetCurrentObject(folder, definition)); }
private SPListItem CreatePublishingPage(object modelHost, SPFolder folder, PublishingPageDefinition publishingPageModel) { var pageName = GetSafePublishingPageFileName(publishingPageModel); var fileContent = Encoding.UTF8.GetBytes(PublishingPageTemplates.RedirectionPageMarkup); ModuleFileModelHandler.DeployModuleFile(folder, SPUrlUtility.CombineUrl(folder.ServerRelativeUrl, pageName), pageName, fileContent, true, null, null); return(FindPublishingPage(folder, publishingPageModel)); }
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 publishingPageModel = model.WithAssertAndCast <PublishingPageLayoutDefinition>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentLibraryFolder; var list = folderModelHost.CurrentList; var siteContentType = folderModelHost.HostSite.RootWeb.AvailableContentTypes.GetById(publishingPageModel.AssociatedContentTypeId); folderModelHost.HostSite.Context.Load(siteContentType); folderModelHost.HostSite.Context.ExecuteQuery(); var context = folder.Context; var pageName = GetSafePageFileName(publishingPageModel); var currentPageFile = GetCurrentPage(list, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f => { var file = new FileCreationInformation(); file.Url = pageName; file.Content = Encoding.UTF8.GetBytes(publishingPageModel.Content); file.Overwrite = publishingPageModel.NeedOverride; return(folder.Files.Add(file)); }, newFile => { var newFileItem = newFile.ListItemAllFields; context.Load(newFileItem); context.ExecuteQueryWithTrace(); var site = folderModelHost.HostSite; var currentPageLayoutItem = FindPageLayoutItem(site, publishingPageModel.FileName); var currentPageLayoutItemContext = currentPageLayoutItem.Context; var publishingFile = currentPageLayoutItem.File; currentPageLayoutItemContext.Load(currentPageLayoutItem); currentPageLayoutItemContext.Load(currentPageLayoutItem, i => i.DisplayName); currentPageLayoutItemContext.Load(publishingFile); currentPageLayoutItemContext.ExecuteQueryWithTrace(); newFileItem[BuiltInInternalFieldNames.Title] = publishingPageModel.Title; newFileItem["MasterPageDescription"] = publishingPageModel.Description; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = BuiltInPublishingContentTypeId.PageLayout; newFileItem["PublishingAssociatedContentType"] = String.Format(";#{0};#{1};#", siteContentType.Name, siteContentType.Id.ToString()); 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 = publishingPageModel, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <ContentPageDefinitionBase>("model", value => value.RequireNotNull()); CurrentModel = definition; //var definition = model.WithAssertAndCast<ControlDisplayTemplateDefinition>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var list = folderModelHost.CurrentList; var context = folder.Context; var pageName = GetSafePageFileName(definition); var currentPageFile = GetItemFile(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 = definition.Content; file.Overwrite = definition.NeedOverride; return(folder.Files.Add(file)); }, newFile => { var newFileItem = newFile.ListItemAllFields; context.Load(newFileItem); context.ExecuteQueryWithTrace(); newFileItem["Title"] = definition.Title; MapProperties(modelHost, newFileItem, definition); newFileItem.Update(); context.ExecuteQueryWithTrace(); }); currentPageFile = GetItemFile(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 definition = model.WithAssertAndCast <MasterPageDefinitionBase>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var list = folderModelHost.CurrentList; 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 = 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(); // ** SIC.. found with Problem with url in MasterPageSettings #936 // https://github.com/SubPointSolutions/spmeta2/issues/936 // * /_catalogs/masterpage - would have 'Title' field (and correct content types) // * /my-sub-web/_catalogs/masterpage - would NOT have 'Title' fiels so that provision fails // so performing Title update only for the root web if (folderModelHost.HostSite.ServerRelativeUrl == folderModelHost.HostWeb.ServerRelativeUrl) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Updating master page 'Title' on the root web.", null); newFileItem[BuiltInInternalFieldNames.Title] = definition.Title; } else { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Skipping master page 'Title' update. Subweb is detcted.", null); } newFileItem["MasterPageDescription"] = definition.Description; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = PageContentTypeId; if (definition.UIVersion.Count > 0) { newFileItem["UIVersion"] = string.Join(";#", definition.UIVersion.ToArray()); } newFileItem["DefaultCssFile"] = definition.DefaultCSSFile; 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 DeployPublishingPage(object modelHost, SPList list, SPFolder folder, PublishingPageDefinition publishingPageModel) { var targetPage = FindPublishingPage(folder, publishingPageModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = targetPage == null ? null : targetPage.File, ObjectType = typeof(SPFile), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); if (targetPage == null) { targetPage = CreatePublishingPage(modelHost, folder, publishingPageModel); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = targetPage.File, ObjectType = typeof(SPFile), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, folder, targetPage.Url, GetSafePublishingPageFileName(publishingPageModel), Encoding.UTF8.GetBytes(PublishingPageTemplates.RedirectionPageMarkup), false, null, afterFile => { var web = list.ParentWeb; var currentPageLayoutItem = FindPageLayoutItem(web, publishingPageModel.PageLayoutFileName); var pageItem = afterFile.Item; // settig up dfault values if there is PublishingPageLayout setup FieldLookupService.EnsureDefaultValues(pageItem, publishingPageModel.DefaultValues); pageItem[BuiltInFieldId.Title] = publishingPageModel.Title; pageItem[BuiltInPublishingFieldId.Description] = publishingPageModel.Description; pageItem[BuiltInPublishingFieldId.ExpiryDate] = NeverEndDate; pageItem[BuiltInPublishingFieldId.StartDate] = ImmediateDate; pageItem[BuiltInFieldId.ContentTypeId] = BuiltInPublishingContentTypeId.Page; pageItem[BuiltInPublishingFieldId.Contact] = list.ParentWeb.CurrentUser; if (!string.IsNullOrEmpty(publishingPageModel.Content)) { pageItem[BuiltInPublishingFieldId.PublishingPageContent] = publishingPageModel.Content; } var contentTypeStringValue = ConvertUtils.ToString(currentPageLayoutItem[BuiltInPublishingFieldId.AssociatedContentType]); if (!string.IsNullOrEmpty(contentTypeStringValue)) { var contentTypeValues = contentTypeStringValue.Split(new string[] { ";#" }, StringSplitOptions.None); var contentTypeName = contentTypeValues[1]; var contentTypeId = contentTypeValues[2]; pageItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } // overrideing with custom one if (!string.IsNullOrEmpty(publishingPageModel.ContentTypeName)) { var listContentType = FindListContentType(list, publishingPageModel.ContentTypeName); if (listContentType == null) { throw new ArgumentNullException( string.Format("Cannot find content type with Name:[{0}] in List:[{1}]", new string[] { publishingPageModel.ContentTypeName, list.Title })); } pageItem[BuiltInFieldId.ContentTypeId] = listContentType.Id.ToString(); } pageItem[BuiltInPublishingFieldId.PageLayout] = new SPFieldUrlValue() { Url = currentPageLayoutItem.File.ServerRelativeUrl, Description = currentPageLayoutItem.Title }; pageItem.Properties["PublishingPageLayoutName"] = currentPageLayoutItem.Name; FieldLookupService.EnsureValues(pageItem, publishingPageModel.Values, true); pageItem.SystemUpdate(); }); }
protected virtual void DeployPage(object modelHost, SPList list, SPFolder folder, MasterPageDefinitionBase 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), definition.Content, definition.NeedOverride, null, afterFile => { var pageItem = afterFile.Properties; //pageItem[BuiltInInternalFieldNames.Title] = definition.Title; pageItem["vti_title"] = definition.Title; pageItem["MasterPageDescription"] = definition.Description; pageItem[BuiltInInternalFieldNames.ContentTypeId] = PageContentTypeId; if (definition.UIVersion.Count > 0) { var value = new SPFieldMultiChoiceValue(); foreach (var v in definition.UIVersion) { value.Add(v); } pageItem["UIVersion"] = value.ToString(); } pageItem["DefaultCssFile"] = definition.DefaultCSSFile; //pageItem.Update(); //pageItem.SystemUpdate(); }); }
private void DeployPublishingPage(object modelHost, SPList list, SPFolder folder, PublishingPageDefinition publishingPageModel) { var targetPage = FindPublishingPage(folder, publishingPageModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = targetPage == null ? null : targetPage.File, ObjectType = typeof(SPFile), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); if (targetPage == null) { targetPage = CreatePublishingPage(modelHost, folder, publishingPageModel); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = targetPage.File, ObjectType = typeof(SPFile), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, folder, targetPage.Url, GetSafePublishingPageFileName(publishingPageModel), Encoding.UTF8.GetBytes(PublishingPageTemplates.RedirectionPageMarkup), false, null, afterFile => { var web = list.ParentWeb; var currentPageLayoutItem = FindPageLayoutItem(web, publishingPageModel.PageLayoutFileName); var pageItem = afterFile.Item; pageItem[BuiltInFieldId.Title] = publishingPageModel.Title; pageItem[BuiltInPublishingFieldId.Description] = publishingPageModel.Description; pageItem[BuiltInPublishingFieldId.ExpiryDate] = NeverEndDate; pageItem[BuiltInPublishingFieldId.StartDate] = ImmediateDate; pageItem[BuiltInPublishingFieldId.Contact] = list.ParentWeb.CurrentUser; pageItem[BuiltInPublishingFieldId.PublishingPageContent] = publishingPageModel.Content; var contentTypeStringValue = ConvertUtils.ToString(currentPageLayoutItem[BuiltInPublishingFieldId.AssociatedContentType]); if (!string.IsNullOrEmpty(contentTypeStringValue)) { var contentTypeValues = contentTypeStringValue.Split(new string[] { ";#" }, StringSplitOptions.None); var contentTypeName = contentTypeValues[1]; var contentTypeId = contentTypeValues[2]; pageItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } pageItem[BuiltInPublishingFieldId.PageLayout] = new SPFieldUrlValue() { Url = currentPageLayoutItem.File.ServerRelativeUrl, Description = currentPageLayoutItem.Title }; pageItem.Properties["PublishingPageLayoutName"] = currentPageLayoutItem.Name; pageItem.SystemUpdate(); }); }
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 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(); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentLibraryFolder; var list = folderModelHost.CurrentList; var publishingPageModel = model.WithAssertAndCast <PublishingPageDefinition>("model", value => value.RequireNotNull()); var context = folder.Context; var pageName = GetSafePageFileName(publishingPageModel); var currentPageFile = GetCurrentPage(list, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f => { var file = new FileCreationInformation(); var pageContent = PublishingPageTemplates.RedirectionPageMarkup; file.Url = pageName; file.Content = Encoding.UTF8.GetBytes(pageContent); file.Overwrite = publishingPageModel.NeedOverride; return(folder.Files.Add(file)); }, newFile => { var newFileItem = newFile.ListItemAllFields; context.Load(newFileItem); context.ExecuteQueryWithTrace(); var site = folderModelHost.HostSite; var currentPageLayoutItem = FindPageLayoutItem(site, publishingPageModel.PageLayoutFileName); var currentPageLayoutItemContext = currentPageLayoutItem.Context; var publishingFile = currentPageLayoutItem.File; currentPageLayoutItemContext.Load(currentPageLayoutItem); currentPageLayoutItemContext.Load(currentPageLayoutItem, i => i.DisplayName); currentPageLayoutItemContext.Load(publishingFile); currentPageLayoutItemContext.ExecuteQueryWithTrace(); newFileItem[BuiltInInternalFieldNames.Title] = publishingPageModel.Title; newFileItem[BuiltInInternalFieldNames.Comments] = publishingPageModel.Description; newFileItem[BuiltInInternalFieldNames.PublishingPageLayout] = publishingFile.ServerRelativeUrl + ", " + currentPageLayoutItem.DisplayName; var contentTypeStringValue = ConvertUtils.ToString(currentPageLayoutItem[BuiltInInternalFieldNames.PublishingAssociatedContentType]); if (!string.IsNullOrEmpty(contentTypeStringValue)) { var contentTypeValues = contentTypeStringValue.Split(new string[] { ";#" }, StringSplitOptions.None); var contentTypeName = contentTypeValues[1]; var contentTypeId = contentTypeValues[2]; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } 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 = publishingPageModel, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var folder = folderModelHost.CurrentLibraryFolder; var list = folderModelHost.CurrentList; var publishingPageModel = model.WithAssertAndCast <PublishingPageDefinition>("model", value => value.RequireNotNull()); var context = folder.Context; var stringCustomContentType = string.Empty; // preload custm content type if (!string.IsNullOrEmpty(publishingPageModel.ContentTypeName)) { var listContentTypes = list.ContentTypes; context.Load(listContentTypes); context.ExecuteQueryWithTrace(); var listContentType = listContentTypes.ToList() .FirstOrDefault(c => c.Name.ToUpper() == publishingPageModel.ContentTypeName.ToUpper()); if (listContentType == null) { throw new ArgumentNullException( string.Format("Cannot find content type with Name:[{0}] in List:[{1}]", new string[] { publishingPageModel.ContentTypeName, list.Title })); } stringCustomContentType = listContentType.Id.ToString(); } var pageName = GetSafePageFileName(publishingPageModel); var currentPageFile = GetCurrentPage(list, folder, pageName); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentPageFile, ObjectType = typeof(File), ObjectDefinition = publishingPageModel, ModelHost = modelHost }); ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f => { var file = new FileCreationInformation(); var pageContent = PublishingPageTemplates.RedirectionPageMarkup; file.Url = pageName; file.Content = Encoding.UTF8.GetBytes(pageContent); file.Overwrite = publishingPageModel.NeedOverride; return(folder.Files.Add(file)); }, newFile => { var newFileItem = newFile.ListItemAllFields; context.Load(newFileItem); context.ExecuteQueryWithTrace(); var site = folderModelHost.HostSite; var currentPageLayoutItem = FindPageLayoutItem(site, publishingPageModel.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 EnsureDefaultValues(newFileItem, publishingPageModel); newFileItem[BuiltInInternalFieldNames.Title] = publishingPageModel.Title; newFileItem[BuiltInInternalFieldNames.Comments] = publishingPageModel.Description; newFileItem[BuiltInInternalFieldNames.PublishingPageLayout] = publishingFile.ServerRelativeUrl + ", " + currentPageLayoutItem.DisplayName; var contentTypeStringValue = ConvertUtils.ToString(currentPageLayoutItem[BuiltInInternalFieldNames.PublishingAssociatedContentType]); if (!string.IsNullOrEmpty(contentTypeStringValue)) { var contentTypeValues = contentTypeStringValue.Split(new string[] { ";#" }, StringSplitOptions.None); var contentTypeName = contentTypeValues[1]; var contentTypeId = contentTypeValues[2]; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = contentTypeId; } if (!string.IsNullOrEmpty(stringCustomContentType)) { newFileItem[BuiltInInternalFieldNames.ContentTypeId] = stringCustomContentType; } 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 = publishingPageModel, ModelHost = modelHost }); context.ExecuteQueryWithTrace(); }
public override void DeployModel(object modelHost, DefinitionBase model) { var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull()); var definition = model.WithAssertAndCast <MasterPageDefinitionBase>("model", value => value.RequireNotNull()); var folder = folderModelHost.CurrentListFolder; var list = folderModelHost.CurrentList; 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 = 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(); newFileItem[BuiltInInternalFieldNames.Title] = definition.Title; newFileItem["MasterPageDescription"] = definition.Description; newFileItem[BuiltInInternalFieldNames.ContentTypeId] = PageContentTypeId; if (definition.UIVersion.Count > 0) { newFileItem["UIVersion"] = string.Join(";#", definition.UIVersion.ToArray()); } newFileItem["DefaultCssFile"] = definition.DefaultCSSFile; 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(); }