Exemplo n.º 1
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull());
            var webPartModel      = model.WithAssertAndCast <DeleteWebPartsDefinition>("model", value => value.RequireNotNull());

            //var listItem = listItemModelHost.HostListItem;
            var list = listItemModelHost.HostList;

            var context         = list.Context;
            var currentPageFile = GetCurrentPageFile(listItemModelHost);

            ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, pageFile =>
            {
                var fileListItem = pageFile.ListItemAllFields;
                var fileContext  = pageFile.Context;

                fileContext.Load(fileListItem);
                fileContext.ExecuteQueryWithTrace();

                var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

                // web part on the page
                var webpartOnPage      = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart);
                var webPartDefenitions = context.LoadQuery(webpartOnPage);

                context.ExecuteQueryWithTrace();

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioning,
                    Object           = pageFile,
                    ObjectType       = typeof(File),
                    ObjectDefinition = webPartModel,
                    ModelHost        = modelHost
                });

                // clean up
                foreach (var wp in webPartDefenitions)
                {
                    wp.DeleteWebPart();
                }

                context.ExecuteQueryWithTrace();

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

                return(pageFile);
            });
        }
Exemplo n.º 2
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            Guid?OldWebParKey = null;

            var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull());
            var webPartModel      = model.WithAssertAndCast <WebPartDefinitionBase>("model", value => value.RequireNotNull());

            try
            {
                OnBeforeDeploy(listItemModelHost, webPartModel);

                CurrentClientContext = listItemModelHost.HostClientContext;

                var context         = listItemModelHost.HostClientContext;
                var currentPageFile = GetCurrentPageFile(listItemModelHost);


                if (listItemModelHost.HostFolder != null)
                {
                    // TODO, re-implement for SP2010 CSOM
                    // the following stuff is needed only for the web part deployment to the non-web part pages
                    // like, view/upload/details pages in the lost/libs
                    // hope no one would use that case on 2010 - folks, migrate to 2013 at least! :)
#if !NET35
                    if (!listItemModelHost.HostFolder.IsPropertyAvailable("Properties") ||
                        listItemModelHost.HostFolder.Properties.FieldValues.Count == 0)
                    {
                        listItemModelHost.HostFolder.Context.Load(listItemModelHost.HostFolder, f => f.Properties);
                        //folder.Context.Load(folder, f => f.Properties);

                        listItemModelHost.HostFolder.Context.ExecuteQueryWithTrace();
                    }
#endif
                }

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

#if NET35
                // TODO, re-implement for SP2010 CSOM
                // the following stuff is needed only for the web part deployment to the non-web part pages

                var doesFileHasListItem = true;
#endif


                ModuleFileModelHandler.WithSafeFileOperation(listItemModelHost.HostList,
                                                             currentPageFile, pageFile =>
                {
                    Guid?webPartStoreKey = null;

                    InternalOnBeforeWebPartProvision(new WebPartProcessingContext
                    {
                        ListItemModelHost = listItemModelHost,
                        WebPartDefinition = webPartModel,
                        WebPartStoreKey   = webPartStoreKey
                    });

                    //var fileContext = pageFile.Context;
                    ListItem fileListItem = null;

                    if (webPartModel.AddToPageContent)
                    {
                        // pre load here to be used later

                        var fileContext = pageFile.Context;

                        fileListItem = pageFile.ListItemAllFields;

                        fileContext.Load(fileListItem);
                        fileContext.ExecuteQueryWithTrace();
                    }

                    var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

                    // web part on the page
                    var webpartOnPage      = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart);
                    var webPartDefenitions = context.LoadQuery(webpartOnPage);

                    context.ExecuteQueryWithTrace();

                    Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpDefinition;

                    WebPart existingWebPart = null;

                    // TODO
                    var tmpWp = FindExistingWebPart(webPartDefenitions, webPartModel, out wpDefinition);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioning,
                        Object           = existingWebPart,
                        ObjectType       = typeof(WebPart),
                        ObjectDefinition = webPartModel,
                        ModelHost        = modelHost
                    });

                    if (wpDefinition != null)
                    {
                        OldWebParKey = wpDefinition.Id;

                        TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                                                 "Deleting current web part.");

                        wpDefinition.DeleteWebPart();
                        wpDefinition.Context.ExecuteQueryWithTrace();
                    }
                    else
                    {
                        existingWebPart = tmpWp;
                    }

                    Microsoft.SharePoint.Client.WebParts.WebPartDefinition webPartAddedDefinition = null;

                    if (existingWebPart == null)
                    {
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                                                 "Processing new web part");

                        var webPartXML = GetWebpartXmlDefinition(listItemModelHost, webPartModel);
                        webPartXML     = ProcessCommonWebpartProperties(webPartXML, webPartModel);

                        //// handle wiki page
                        //if (webPartModel.AddToPageContent)
                        //{

                        //    HandleWikiPageProvision(fileListItem, webPartModel);
                        //}

                        var webPartDefinition  = webPartManager.ImportWebPart(webPartXML);
                        webPartAddedDefinition = webPartManager.AddWebPart(webPartDefinition.WebPart,
                                                                           webPartModel.ZoneId,
                                                                           webPartModel.ZoneIndex);

                        context.Load(webPartAddedDefinition);
                        context.ExecuteQueryWithTrace();

                        if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false)
                        {
                            existingWebPart = webPartAddedDefinition.WebPart;
                            webPartStoreKey = webPartAddedDefinition.Id;
                        }

                        // handle wiki page
                        if (webPartModel.AddToPageContent)
                        {
                            HandleWikiPageProvision(fileListItem, webPartModel, webPartStoreKey, OldWebParKey);
                        }

                        existingWebPart = webPartDefinition.WebPart;

                        InvokeOnModelEvent(this, new ModelEventArgs
                        {
                            CurrentModelNode = null,
                            Model            = null,
                            EventType        = ModelEventType.OnProvisioned,
                            Object           = existingWebPart,
                            ObjectType       = typeof(WebPart),
                            ObjectDefinition = webPartModel,
                            ModelHost        = modelHost
                        });
                    }
                    else
                    {
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                                                 "Processing existing web part");

                        if (webPartModel.AddToPageContent)
                        {
                            //HandleWikiPageProvision(fileListItem, webPartModel);
                        }

                        InvokeOnModelEvent(this, new ModelEventArgs
                        {
                            CurrentModelNode = null,
                            Model            = null,
                            EventType        = ModelEventType.OnProvisioned,
                            Object           = existingWebPart,
                            ObjectType       = typeof(WebPart),
                            ObjectDefinition = webPartModel,
                            ModelHost        = modelHost
                        });
                    }

                    context.ExecuteQueryWithTrace();

                    if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false)
                    {
                        existingWebPart = webPartAddedDefinition.WebPart;
                        webPartStoreKey = webPartAddedDefinition.Id;
                    }

                    InternalOnAfterWebPartProvision(new WebPartProcessingContext
                    {
                        ListItemModelHost = listItemModelHost,
                        WebPartDefinition = webPartModel,
                        WebPartStoreKey   = webPartStoreKey
                    });

                    return(pageFile);
                }, doesFileHasListItem);
            }
            finally
            {
                OnAfterDeploy(listItemModelHost, webPartModel);
            }
        }
Exemplo n.º 3
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var folderModelHost = modelHost.WithAssertAndCast <FolderModelHost>("modelHost", value => value.RequireNotNull());
            var definition      = model.WithAssertAndCast <MasterPageDefinition>("model", value => value.RequireNotNull());

            var folder = folderModelHost.CurrentLibraryFolder;
            var list   = folderModelHost.CurrentList;

            var context = folder.Context;

            var pageName        = GetSafePageFileName(definition);
            var currentPageFile = GetCurrentPage(list, folder, pageName);

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

            ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f =>
            {
                var file = new FileCreationInformation();

                file.Url       = pageName;
                file.Content   = definition.Content;
                file.Overwrite = definition.NeedOverride;

                return(folder.Files.Add(file));
            },
                                                         newFile =>
            {
                var newFileItem = newFile.ListItemAllFields;
                context.Load(newFileItem);
                context.ExecuteQueryWithTrace();

                var site = folderModelHost.HostSite;
                var currentPageLayoutItem = FindPageLayoutItem(site, definition.FileName);


                var currentPageLayoutItemContext = currentPageLayoutItem.Context;
                var publishingFile = currentPageLayoutItem.File;

                currentPageLayoutItemContext.Load(currentPageLayoutItem);
                currentPageLayoutItemContext.Load(currentPageLayoutItem, i => i.DisplayName);
                currentPageLayoutItemContext.Load(publishingFile);

                currentPageLayoutItemContext.ExecuteQueryWithTrace();

                newFileItem[BuiltInInternalFieldNames.Title]         = definition.Title;
                newFileItem["MasterPageDescription"]                 = definition.Description;
                newFileItem[BuiltInInternalFieldNames.ContentTypeId] = BuiltInContentTypeId.MasterPage;

                if (definition.UIVersion.Count > 0)
                {
                    newFileItem["UIVersion"] = string.Join(";#", definition.UIVersion.ToArray());
                }

                newFileItem["DefaultCssFile"] = definition.DefaultCSSFile;

                newFileItem.Update();

                context.ExecuteQueryWithTrace();
            });

            currentPageFile = GetCurrentPage(folderModelHost.CurrentList, folder, pageName);

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

            context.ExecuteQueryWithTrace();
        }
Exemplo n.º 4
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull());
            var webPartModel      = model.WithAssertAndCast <WebPartDefinitionBase>("model", value => value.RequireNotNull());

            var listItem = listItemModelHost.HostListItem;

            var context         = listItem.Context;
            var currentPageFile = GetCurrentPageFile(listItemModelHost);

            ModuleFileModelHandler.WithSafeFileOperation(listItem.ParentList, currentPageFile, pageFile =>
            {
                Guid?webPartStoreKey = null;

                InternalOnBeforeWebPartProvision(new WebPartProcessingContext
                {
                    ListItemModelHost = listItemModelHost,
                    WebPartDefinition = webPartModel,
                    WebPartStoreKey   = webPartStoreKey
                });

                //var fileContext = pageFile.Context;
                var fileListItem = pageFile.ListItemAllFields;
                var fileContext  = pageFile.Context;

                fileContext.Load(fileListItem);

                fileContext.ExecuteQueryWithTrace();

                var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

                // web part on the page
                var webpartOnPage      = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart);
                var webPartDefenitions = context.LoadQuery(webpartOnPage);

                context.ExecuteQueryWithTrace();

                Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpDefinition;

                WebPart existingWebPart = null;

                // TODO
                var tmpWp = FindExistingWebPart(webPartDefenitions, webPartModel, out wpDefinition);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioning,
                    Object           = existingWebPart,
                    ObjectType       = typeof(WebPart),
                    ObjectDefinition = webPartModel,
                    ModelHost        = modelHost
                });

                if (wpDefinition != null)
                {
                    TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                                             "Deleting current web part.");

                    wpDefinition.DeleteWebPart();
                    wpDefinition.Context.ExecuteQueryWithTrace();
                }
                else
                {
                    existingWebPart = tmpWp;
                }

                Microsoft.SharePoint.Client.WebParts.WebPartDefinition webPartAddedDefinition = null;

                if (existingWebPart == null)
                {
                    TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                                             "Processing new web part");

                    var webPartXML = GetWebpartXmlDefinition(listItemModelHost, webPartModel);
                    webPartXML     = ProcessCommonWebpartProperties(webPartXML, webPartModel);

                    // handle wiki page

                    HandleWikiPageProvision(fileListItem, webPartModel);

                    var webPartDefinition  = webPartManager.ImportWebPart(webPartXML);
                    webPartAddedDefinition = webPartManager.AddWebPart(webPartDefinition.WebPart,
                                                                       webPartModel.ZoneId,
                                                                       webPartModel.ZoneIndex);

                    context.Load(webPartAddedDefinition);

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

                    existingWebPart = webPartDefinition.WebPart;

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioned,
                        Object           = existingWebPart,
                        ObjectType       = typeof(WebPart),
                        ObjectDefinition = webPartModel,
                        ModelHost        = modelHost
                    });

                    InvokeOnModelEvent <WebPartDefinition, WebPart>(null, ModelEventType.OnUpdated);
                }
                else
                {
                    TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                                             "Processing existing web part");

                    HandleWikiPageProvision(fileListItem, webPartModel);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioned,
                        Object           = existingWebPart,
                        ObjectType       = typeof(WebPart),
                        ObjectDefinition = webPartModel,
                        ModelHost        = modelHost
                    });
                }

                context.ExecuteQueryWithTrace();

                if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false)
                {
                    existingWebPart = webPartAddedDefinition.WebPart;
                    webPartStoreKey = webPartAddedDefinition.Id;
                }

                InternalOnAfterWebPartProvision(new WebPartProcessingContext
                {
                    ListItemModelHost = listItemModelHost,
                    WebPartDefinition = webPartModel,
                    WebPartStoreKey   = webPartStoreKey
                });

                return(pageFile);
            });
        }