Beispiel #1
0
        protected virtual void MapFolderProperties(SPListItem item, FolderDefinition definition)
        {
            if (!string.IsNullOrEmpty(definition.ContentTypeId) ||
                !string.IsNullOrEmpty(definition.ContentTypeName))
            {
                var list = item.ParentList;

                if (!string.IsNullOrEmpty(definition.ContentTypeId))
                {
                    item["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId);
                }

                if (!string.IsNullOrEmpty(definition.ContentTypeName))
                {
                    item["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName);
                }
            }
        }
        protected virtual void MapListItemProperties(SPListItem item, ListItemDefinition definition)
        {
            FieldLookupService.EnsureDefaultValues(item, definition.DefaultValues);

            if (!string.IsNullOrEmpty(definition.ContentTypeId) ||
                !string.IsNullOrEmpty(definition.ContentTypeName))
            {
                var list = item.ParentList;

                if (!string.IsNullOrEmpty(definition.ContentTypeId))
                {
                    item["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, definition.ContentTypeId);
                }

                if (!string.IsNullOrEmpty(definition.ContentTypeName))
                {
                    item["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, definition.ContentTypeName);
                }
            }

            item[BuiltInInternalFieldNames.Title] = definition.Title;

            FieldLookupService.EnsureValues(item, definition.Values, true);
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull());
            var definition      = model.WithAssertAndCast <FolderDefinition>("model", value => value.RequireNotNull());

            SPFolder spObject = null;

            if (folderModelHost.CurrentLibrary != null)
            {
                spObject = GetLibraryFolder(folderModelHost, definition);
            }
            else if (folderModelHost.CurrentList != null)
            {
                spObject = GetListFolder(folderModelHost, definition);
            }

            var assert = ServiceFactory.AssertService
                         .NewAssert(definition, spObject)
                         .ShouldBeEqual(m => m.Name, o => o.Name);

            var item = spObject.Item;

            var stringCustomContentTypeId = string.Empty;

            if (!string.IsNullOrEmpty(definition.ContentTypeName) ||
                !string.IsNullOrEmpty(definition.ContentTypeId))
            {
                if (!string.IsNullOrEmpty(definition.ContentTypeName))
                {
                    var ct = ContentTypeLookupService
                             .LookupContentTypeByName(item.ParentList, definition.ContentTypeName);

                    stringCustomContentTypeId = ct.ToString();
                }

                if (!string.IsNullOrEmpty(definition.ContentTypeId))
                {
                    var ct = ContentTypeLookupService
                             .LookupListContentTypeById(item.ParentList, definition.ContentTypeId);

                    stringCustomContentTypeId = ct.ToString();
                }
            }

            if (!string.IsNullOrEmpty(definition.ContentTypeId))
            {
                assert.ShouldBeEqual((p, s, d) =>
                {
                    var srcProp = s.GetExpressionValue(def => def.ContentTypeId);
                    var currentContentTypeId = ConvertUtils.ToString(item["ContentTypeId"]);

                    var isValis = currentContentTypeId.StartsWith(s.ContentTypeId);

                    return(new PropertyValidationResult
                    {
                        Tag = p.Tag,
                        Src = srcProp,
                        Dst = null,
                        IsValid = isValis
                    });
                });
            }
            else
            {
                assert.SkipProperty(m => m.ContentTypeId, "ContentTypeId is null or empty. Skipping.");
            }

            if (!string.IsNullOrEmpty(definition.ContentTypeName))
            {
                assert.ShouldBeEqual((p, s, d) =>
                {
                    var srcProp = s.GetExpressionValue(def => def.ContentTypeName);

                    // still validating agains content type ID.
                    // setting up by Name, the item must have correct ID
                    var currentContentTypeId = ConvertUtils.ToString(item["ContentTypeId"]);
                    var isValis = stringCustomContentTypeId.StartsWith(currentContentTypeId);

                    return(new PropertyValidationResult
                    {
                        Tag = p.Tag,
                        Src = srcProp,
                        Dst = null,
                        IsValid = isValis
                    });
                });
            }
            else
            {
                assert.SkipProperty(m => m.ContentTypeName, "ContentTypeName is null or empty. Skipping.");
            }
        }
        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 ProcessFile(
            object modelHost,
            SPFolder folder,
            ModuleFileDefinition moduleFile)
        {
            DeployModuleFile(
                folder,
                GetSafeFileUrl(folder, moduleFile),
                moduleFile.FileName,
                moduleFile.Content,
                moduleFile.Overwrite,
                before =>
            {
                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioning,
                    Object           = before.Exists ? before : null,
                    ObjectType       = typeof(SPFile),
                    ObjectDefinition = moduleFile,
                    ModelHost        = modelHost
                });
            },
                after =>
            {
                var shouldUpdateItem = false;

                if (!string.IsNullOrEmpty(moduleFile.Title))
                {
                    after.ListItemAllFields["Title"] = moduleFile.Title;
                    shouldUpdateItem = true;
                }

                if (!string.IsNullOrEmpty(moduleFile.ContentTypeId) ||
                    !string.IsNullOrEmpty(moduleFile.ContentTypeName))
                {
                    var list = folder.ParentWeb.Lists[folder.ParentListId];

                    if (!string.IsNullOrEmpty(moduleFile.ContentTypeId))
                    {
                        after.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupListContentTypeById(list, moduleFile.ContentTypeId);
                    }

                    if (!string.IsNullOrEmpty(moduleFile.ContentTypeName))
                    {
                        after.ListItemAllFields["ContentTypeId"] = ContentTypeLookupService.LookupContentTypeByName(list, moduleFile.ContentTypeName);
                    }

                    shouldUpdateItem = true;
                }

                if (moduleFile.DefaultValues.Count > 0)
                {
                    FieldLookupService.EnsureDefaultValues(after.ListItemAllFields, moduleFile.DefaultValues);
                    shouldUpdateItem = true;
                }

                FieldLookupService.EnsureValues(after.ListItemAllFields, moduleFile.Values, true);

                if (shouldUpdateItem)
                {
                    after.ListItemAllFields.Update();
                }

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = after,
                    ObjectType       = typeof(SPFile),
                    ObjectDefinition = moduleFile,
                    ModelHost        = modelHost
                });
            });
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull());
            var 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();
            });
        }