protected override void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartModel)
        {
            _host = host;

            var typedModel = webpartModel.WithAssertAndCast<ClientWebPartDefinition>("webpartModel", value => value.RequireNotNull());
            typedModel.WebpartType = typeof(ClientWebPart).AssemblyQualifiedName;
        }
        public override void WithResolvingModelHost(ModelHostResolveContext modelHostContext)
        {
            var modelHost = modelHostContext.ModelHost;
            var model = modelHostContext.Model;
            var childModelType = modelHostContext.ChildModelType;
            var action = modelHostContext.Action;


            var folderModelHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull());
            var webpartPageModel = model.WithAssertAndCast<WebPartPageDefinition>("model", value => value.RequireNotNull());

            //var list = listModelHost.HostList;
            var folder = folderModelHost.CurrentLibraryFolder;

            var targetFile = GetOrCreateNewWebPartFile(modelHost, folder, webpartPageModel);

            using (var webPartManager = targetFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                var webpartPageHost = new WebpartPageModelHost
                {
                    HostFile = targetFile,
                    PageListItem = targetFile.Item,
                    SPLimitedWebPartManager = webPartManager
                };

                action(webpartPageHost);
            }
        }
        public override void WithResolvingModelHost(ModelHostResolveContext modelHostContext)
        {
            var modelHost = modelHostContext.ModelHost;
            var model = modelHostContext.Model;
            var childModelType = modelHostContext.ChildModelType;
            var action = modelHostContext.Action;


            var folderHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull());
            var moduleFile = model.WithAssertAndCast<ModuleFileDefinition>("model", value => value.RequireNotNull());

            var folder = folderHost.CurrentLibraryFolder;
            var file = GetFile(folderHost, moduleFile);

            if (childModelType == typeof(WebPartDefinition))
            {
                using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
                {
                    var webpartPageHost = new WebpartPageModelHost
                    {
                        HostFile = file,
                        PageListItem = file.Item,
                        SPLimitedWebPartManager = webPartManager
                    };

                    action(webpartPageHost);
                }
            }
            else
            {
                action(file);
            }
        }
        public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            var folderModelHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull());
            var wikiPageModel = model.WithAssertAndCast<WikiPageDefinition>("model", value => value.RequireNotNull());

            var folder = folderModelHost.CurrentLibraryFolder;

            var targetPage = FindWikiPageItem(folder, wikiPageModel);

            ModuleFileModelHandler.WithSafeFileOperation(folderModelHost.CurrentLibrary, folder,
                targetPage.Url,
                GetWikiPageName(wikiPageModel),
                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);

                        // targetPage.Update();
                    }
                });
        }
        public override void WithResolvingModelHost(ModelHostResolveContext modelHostContext)
        {
            var modelHost = modelHostContext.ModelHost;
            var model = modelHostContext.Model;
            var childModelType = modelHostContext.ChildModelType;
            var action = modelHostContext.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);
                    }
                });
        }
        public override void WithResolvingModelHost(object modelHost, DefinitionBase model, Type childModelType, Action<object> action)
        {
            var list = modelHost.WithAssertAndCast<SPList>("modelHost", value => value.RequireNotNull());
            var webpartPageModel = model.WithAssertAndCast<WebPartPageDefinition>("model", value => value.RequireNotNull());

            var targetPage = GetOrCreateNewWebPartPage(list, webpartPageModel);

            using (var webPartManager = targetPage.File.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                var webpartPageHost = new WebpartPageModelHost
                {
                    PageListItem = targetPage,
                    SPLimitedWebPartManager = webPartManager
                };

                action(webpartPageHost);
            }
        }
        protected override void OnAfterDeployModel(WebpartPageModelHost host, WebPartDefinition definition)
        {
            var typedDefinition = definition.WithAssertAndCast<XsltListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
            {
                var existingWebPart = host.SPLimitedWebPartManager
                    .WebParts
                    .OfType<System.Web.UI.WebControls.WebParts.WebPart>()
                    .FirstOrDefault(wp => !string.IsNullOrEmpty(wp.ID) &&
                                          wp.ID.ToUpper() == definition.Id.ToUpper());

                if (existingWebPart != null)
                {
                    // patching up the view -> ToolbarType
                    var xsltWebPart = existingWebPart as XsltListViewWebPart;

                    if (xsltWebPart != null)
                    {

                        // big TODO for .NET 35
                        // xsltWebPart.View does not exist for .NET 35
                        // the implementation will be done upon the community demand

#if !NET35

                        var targetView = xsltWebPart.View;

                        // fixing up the Toolbar
                        if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
                        {
                            var htmlSchemaXml = XDocument.Parse(targetView.HtmlSchemaXml);

                            var useShowAlwaysValue =
                                (typedDefinition.Toolbar.ToUpper() == BuiltInToolbarType.Standard.ToUpper())
                                && typedDefinition.ToolbarShowAlways.HasValue
                                && typedDefinition.ToolbarShowAlways.Value;

                            var toolbarNode = htmlSchemaXml.Root
                                .Descendants("Toolbar")
                                .FirstOrDefault();

                            if (toolbarNode == null)
                            {
                                toolbarNode = new XElement("Toolbar");
                                htmlSchemaXml.Root.Add(toolbarNode);
                            }

                            toolbarNode.SetAttributeValue("Type", typedDefinition.Toolbar);

                            if (useShowAlwaysValue)
                            {
                                toolbarNode.SetAttributeValue("ShowAlways", "TRUE");
                            }
                            else
                            {
                                XAttribute attr = toolbarNode.Attribute("ShowAlways");
                                if (attr != null && string.IsNullOrEmpty(attr.Value))
                                    attr.Remove();
                            }

                            var field = targetView.GetType()
                                                  .GetProperty("ListViewXml",
                                                     BindingFlags.NonPublic | BindingFlags.Instance);

                            if (field != null)
                            {
                                field.SetValue(targetView, htmlSchemaXml.Root.GetInnerXmlAsString(), null);
                            }
                        }

                        targetView.Update();

#endif
                    }
                }
            }
        }
        protected override void OnAfterDeployModel(WebpartPageModelHost host, WebPartDefinition definition)
        {
            var typedDefinition = definition.WithAssertAndCast<ListViewWebPartDefinition>("webpartModel", value => value.RequireNotNull());

            if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
            {
                var existingWebPart = host.SPLimitedWebPartManager
                    .WebParts
                    .OfType<System.Web.UI.WebControls.WebParts.WebPart>()
                    .FirstOrDefault(wp => !string.IsNullOrEmpty(wp.ID) &&
                                          wp.ID.ToUpper() == definition.Id.ToUpper());

                if (existingWebPart != null)
                {
                    // patching up the view -> ToolbarType
                    var xsltWebPart = existingWebPart as ListViewWebPart;

                    if (xsltWebPart != null)
                    {
                        if (!string.IsNullOrEmpty(xsltWebPart.ViewGuid))
                        {
                            var targetWeb = new LookupFieldModelHandler().GetTargetWeb(
                                            host.PageListItem.Web.Site,
                                            typedDefinition.WebUrl,
                                            typedDefinition.WebId);

                            var list = XsltListViewWebPartModelHandler.GetTargetList(targetWeb,
                                            typedDefinition.ListTitle,
                                            typedDefinition.ListUrl,
                                            typedDefinition.ListId);

                            var targetView = list.Views[ConvertUtils.ToGuid(xsltWebPart.ViewGuid).Value];

                            // fixing up the Toolbar
                            if (!string.IsNullOrEmpty(typedDefinition.Toolbar))
                            {
                                var htmlSchemaXml = XDocument.Parse(targetView.HtmlSchemaXml);

                                var useShowAlwaysValue =
                                    (typedDefinition.Toolbar.ToUpper() == BuiltInToolbarType.Standard.ToUpper())
                                    && typedDefinition.ToolbarShowAlways.HasValue
                                    && typedDefinition.ToolbarShowAlways.Value;

                                var toolbarNode = htmlSchemaXml.Root
                                    .Descendants("Toolbar")
                                    .FirstOrDefault();

                                if (toolbarNode == null)
                                {
                                    toolbarNode = new XElement("Toolbar");
                                    htmlSchemaXml.Root.Add(toolbarNode);
                                }

                                toolbarNode.SetAttributeValue("Type", typedDefinition.Toolbar);

                                if (useShowAlwaysValue)
                                {
                                    toolbarNode.SetAttributeValue("ShowAlways", "TRUE");
                                }
                                else
                                {
                                    XAttribute attr = toolbarNode.Attribute("ShowAlways");
                                    if (attr != null && string.IsNullOrEmpty(attr.Value))
                                        attr.Remove();
                                }

                                var field = targetView.GetType()
                                    .GetProperty("ListViewXml",
                                        BindingFlags.NonPublic | BindingFlags.Instance);

                                if (field != null)
                                {
                                    field.SetValue(targetView, htmlSchemaXml.Root.GetInnerXmlAsString(), null);
                                }
                            }

                            targetView.Update();
                        }
                    }
                }
            }
        }
Beispiel #9
0
        protected virtual void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartPageModel)
        {

        }
Beispiel #10
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var host = modelHost.WithAssertAndCast<WebpartPageModelHost>("modelHost", value => value.RequireNotNull());
            var webpartModel = model.WithAssertAndCast<WebPartDefinition>("model", value => value.RequireNotNull());

            CurrentHost = host;

            OnBeforeDeployModel(host, webpartModel);

            InvokeOnModelEvent<FieldDefinition, SPField>(null, ModelEventType.OnUpdating);
            WebPartExtensions.DeployWebPartToPage(host.SPLimitedWebPartManager, webpartModel,
                onUpdatingWebpartInstnce =>
                {
                    if (onUpdatingWebpartInstnce == null)
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new web part");
                    else
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part");

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioning,
                        Object = onUpdatingWebpartInstnce,
                        ObjectType = typeof(System.Web.UI.WebControls.WebParts.WebPart),
                        ObjectDefinition = model,
                        ModelHost = modelHost
                    });

                    InvokeOnModelEvent<WebPartDefinition, WebPart>(onUpdatingWebpartInstnce, ModelEventType.OnUpdating);

                },
                onUpdatedWebpartInstnce =>
                {
                    HandleWikiOrPublishingPageProvision(host.HostFile.ListItemAllFields, webpartModel);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model = null,
                        EventType = ModelEventType.OnProvisioned,
                        Object = onUpdatedWebpartInstnce,
                        ObjectType = typeof(System.Web.UI.WebControls.WebParts.WebPart),
                        ObjectDefinition = model,
                        ModelHost = modelHost
                    });

                    InvokeOnModelEvent<WebPartDefinition, WebPart>(onUpdatedWebpartInstnce, ModelEventType.OnUpdated);
                },
                ProcessWebpartProperties);
        }
 protected override void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartModel)
 {
     var typedModel = webpartModel.WithAssertAndCast<PictureLibrarySlideshowWebPartDefinition>("webpartModel", value => value.RequireNotNull());
     typedModel.WebpartType = typeof(PictureLibrarySlideshowWebPart).AssemblyQualifiedName;
 }
 protected override void OnBeforeDeployModel(WebpartPageModelHost host, WebPartDefinition webpartModel)
 {
     var typedModel = webpartModel.WithAssertAndCast<BlogMonthQuickLaunchDefinition>("webpartModel", value => value.RequireNotNull());
     typedModel.WebpartType = typeof(BlogMonthQuickLaunch).AssemblyQualifiedName;
 }
        public override void WithResolvingModelHost(ModelHostResolveContext modelHostContext)
        {
            var modelHost = modelHostContext.ModelHost;
            var model = modelHostContext.Model;
            var childModelType = modelHostContext.ChildModelType;
            var action = modelHostContext.Action;

            var listModelHost = modelHost.WithAssertAndCast<ListModelHost>("modelHost", value => value.RequireNotNull());

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

            if (typeof(WebPartDefinitionBase).IsAssignableFrom(childModelType))
            {
                var listViewDefinition = model.WithAssertAndCast<ListViewDefinition>("model", value => value.RequireNotNull());
                var currentView = FindView(list, listViewDefinition);

                var serverRelativeFileUrl = string.Empty;

                if (currentView != null)
                    serverRelativeFileUrl = currentView.ServerRelativeUrl;
                else
                {
                    //  maybe forms files?
                    // they aren't views, but files

                    if (list.BaseType == SPBaseType.DocumentLibrary)
                    {
                        serverRelativeFileUrl = UrlUtility.CombineUrl(new[]
                        {
                            list.RootFolder.ServerRelativeUrl, 
                            "Forms",
                            listViewDefinition.Url
                        });
                    }
                    else
                    {
                        serverRelativeFileUrl = UrlUtility.CombineUrl(new[]
                        {
                            list.RootFolder.ServerRelativeUrl, 
                            listViewDefinition.Url
                        });
                    }
                }

                var targetFile = web.GetFile(serverRelativeFileUrl);

                using (var webPartManager = targetFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
                {
                    var webpartPageHost = new WebpartPageModelHost
                    {
                        HostFile = targetFile,
                        PageListItem = targetFile.Item,
                        SPLimitedWebPartManager = webPartManager
                    };

                    action(webpartPageHost);
                }
            }
            else
            {
                action(modelHost);
            }
        }
        public override void WithResolvingModelHost(ModelHostResolveContext modelHostContext)
        {
            var modelHost = modelHostContext.ModelHost;
            var model = modelHostContext.Model;
            var childModelType = modelHostContext.ChildModelType;
            var action = modelHostContext.Action;

            var folderModelHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull());
            var webpartPageModel = model.WithAssertAndCast<WebPartPageDefinition>("model", value => value.RequireNotNull());

            //var list = listModelHost.HostList;
            var folder = folderModelHost.CurrentLibraryFolder;

            // Web part provision seems to put only the last web part on the page #869
            // https://github.com/SubPointSolutions/spmeta2/issues/869
            var targetFile = FindWebPartPage(folder, webpartPageModel);
            //var targetFile = GetOrCreateNewWebPartFile(modelHost, folder, webpartPageModel);

            using (var webPartManager = targetFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                var webpartPageHost = new WebpartPageModelHost
                {
                    HostFile = targetFile,
                    PageListItem = targetFile.Item,
                    SPLimitedWebPartManager = webPartManager
                };

                action(webpartPageHost);
            }
        }