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);
        }
Beispiel #3
0
        private File ProcessFile(FolderModelHost folderHost, ModuleFileDefinition definition)
        {
            var context = folderHost.CurrentListFolder.Context;

            var web    = folderHost.CurrentWeb;
            var list   = folderHost.CurrentList;
            var folder = folderHost.CurrentListFolder;

            context.Load(folder, f => f.ServerRelativeUrl);

#if !NET35
            context.Load(folder, f => f.Properties);
#endif

            context.ExecuteQueryWithTrace();

            var stringCustomContentType = ResolveContentTypeId(folderHost, definition);

            if (list != null)
            {
                context.Load(list, l => l.EnableMinorVersions);
                context.Load(list, l => l.EnableVersioning);
                context.Load(list, l => l.EnableModeration);

                context.ExecuteQueryWithTrace();
            }

            var file = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, definition));

            context.Load(file, f => f.Exists);
            context.ExecuteQueryWithTrace();

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = file.Exists ? file : null,
                ObjectType       = typeof(File),
                ObjectDefinition = definition,
                ModelHost        = folderHost
            });

#if !NET35
            var doesFileHasListItem =
                //Forms folders
                !(folder != null
                  &&
                  (folder.Properties.FieldValues.ContainsKey("vti_winfileattribs") &&
                   folder.Properties.FieldValues["vti_winfileattribs"].ToString() == "00000012"));
#endif

#if NET35
            var doesFileHasListItem = true;
#endif

            WithSafeFileOperation(list, file, f =>
            {
                var fileName    = definition.FileName;
                var fileContent = definition.Content;

                var fileCreatingInfo = new FileCreationInformation
                {
                    Url       = fileName,
                    Overwrite = file.Exists
                };

                if (fileContent.Length < ContentStreamFileSize)
                {
                    TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.Content for small file less than: [{0}]", ContentStreamFileSize);
                    fileCreatingInfo.Content = fileContent;
                }
                else
                {
#if NET35
                    throw new SPMeta2Exception(string.Format("SP2010 CSOM implementation does no support file more than {0}. Checkout FileCreationInformation and avialabe Content size.", ContentStreamFileSize));
#endif

#if !NET35
                    TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Using fileCreatingInfo.ContentStream for big file more than: [{0}]", ContentStreamFileSize);
                    fileCreatingInfo.ContentStream = new System.IO.MemoryStream(fileContent);
#endif
                }

                TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Overwriting file");
                var updatedFile = folder.Files.Add(fileCreatingInfo);

                FieldLookupService.EnsureDefaultValues(updatedFile.ListItemAllFields, definition.DefaultValues);


                if (!string.IsNullOrEmpty(stringCustomContentType))
                {
                    updatedFile.ListItemAllFields[BuiltInInternalFieldNames.ContentTypeId] = stringCustomContentType;
                }

                if (!string.IsNullOrEmpty(definition.Title))
                {
                    updatedFile.ListItemAllFields[BuiltInInternalFieldNames.Title] = definition.Title;
                }


                FieldLookupService.EnsureValues(updatedFile.ListItemAllFields, definition.Values, true);

                if (!string.IsNullOrEmpty(stringCustomContentType) ||
                    definition.DefaultValues.Count > 0 ||
                    definition.Values.Count > 0 ||
                    !string.IsNullOrEmpty(definition.Title))
                {
                    updatedFile.ListItemAllFields.Update();
                }


                return(updatedFile);
            }, doesFileHasListItem);

            var resultFile = web.GetFileByServerRelativeUrl(GetSafeFileUrl(folder, definition));

            context.Load(resultFile, f => f.Exists);
            context.ExecuteQueryWithTrace();

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = resultFile,
                ObjectType       = typeof(File),
                ObjectDefinition = definition,
                ModelHost        = folderHost
            });

            return(resultFile);
        }
        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);
        }
Beispiel #5
0
        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();
            });
        }
        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();
        }
Beispiel #7
0
        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 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 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();
            }
        }
Beispiel #10
0
        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 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
                });
            }
        }