public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                var context = web.Context as ClientContext;

                // We extract the Image Renditions if and only if the Publishing feature is enabled
                if (web.IsFeatureActive(Constants.FeatureId_Web_Publishing))
                {
                    // This Object Handler will be invoked after the Publishing handler
                    // Thus, we should have the Publishing property assigned in the template
                    if (template.Publishing == null)
                    {
                        // And if not, we create it
                        template.Publishing = new Publishing();
                    }

                    var renditions = SiteImageRenditions.GetRenditions(context);
                    context.ExecuteQueryRetry();

                    foreach (var r in renditions)
                    {
                        template.Publishing.ImageRenditions.Add(new Model.ImageRendition
                        {
                            Name   = r.Name,
                            Height = r.Height,
                            Width  = r.Width,
                        });
                    }
                }
            }
            return(template);
        }
Beispiel #2
0
        /// <summary>
        /// Returns all image renditions
        /// </summary>
        /// <param name="web">SharePoint Web</param>
        public static IList <ImageRendition> GetPublishingImageRenditions(this Web web)
        {
            var imageRenditions = SiteImageRenditions.GetRenditions(web.Context);

            web.Context.ExecuteQueryRetry();
            return(imageRenditions);
        }
Beispiel #3
0
        /// <summary>
        /// Creates an Image Rendition if the name of the Image Rendition doesn't already exist.
        /// </summary>
        /// <param name="web">SharePoint Web</param>
        /// <param name="imageRenditionName">The display name of the Image Rendition</param>
        /// <param name="imageRenditionWidth">The width of the Image Rendition</param>
        /// <param name="imageRenditionHeight">The height of the Image Rendition</param>
        public static void CreatePublishingImageRendition(this Web web, string imageRenditionName, int imageRenditionWidth, int imageRenditionHeight)
        {
            List <string>         imageRenditionNames     = new List <string>();
            List <ImageRendition> existingImageRenditions = SiteImageRenditions.GetRenditions(web.Context) as List <ImageRendition>;

            web.Context.ExecuteQueryRetry();
            foreach (ImageRendition existingImageRendition in existingImageRenditions)
            {
                imageRenditionNames.Add(existingImageRendition.Name);
            }
            if (!imageRenditionNames.Contains(imageRenditionName))
            {
                Log.Info(Constants.LOGGING_SOURCE, CoreResources.WebExtensions_CreatePublishingImageRendition, imageRenditionName, imageRenditionWidth, imageRenditionHeight);
                ImageRendition newImageRendition = new ImageRendition();
                newImageRendition.Name   = imageRenditionName;
                newImageRendition.Width  = imageRenditionWidth;
                newImageRendition.Height = imageRenditionHeight;
                existingImageRenditions.Add(newImageRendition);
                SiteImageRenditions.SetRenditions(web.Context, existingImageRenditions);
                web.Context.ExecuteQueryRetry();
            }
            else
            {
                Log.Info(Constants.LOGGING_SOURCE, CoreResources.WebExtensions_CreatePublishingImageRendition_Error, imageRenditionName);
            }
        }
        /// <summary>
        /// Method to ensure an image matchingRenditions in the current site
        /// </summary>
        /// <param name="site">The current site</param>
        /// <param name="imageRenditionInfo">The image matchingRenditions to add/update</param>
        public void EnsureImageRendition(SPSite site, ImageRenditionInfo imageRenditionInfo)
        {
            var imageRendition = SetImageRenditionProperties(imageRenditionInfo);

            // Error checking
            if (site == null || imageRendition == null || !imageRendition.IsValid)
            {
                this.logger.Error("Error ensuring image rendition. Argument is null or invalid.");
                throw new ArgumentException("ImageRenditionHelper : argument is null of invalid.");
            }

            // Get the image existingImageRendition collection of the current site.
            var imageRenditionCollection = SiteImageRenditions.GetRenditions(site);

            if (!imageRenditionCollection.Any(x => x.Name == imageRendition.Name))
            {
                this.logger.Info("Adding image rendition '{0}' with width '{1}' and height '{2}'", imageRendition.Name, imageRendition.Width, imageRendition.Height);
                imageRenditionCollection.Add(imageRendition);
            }
            else
            {
                this.logger.Info("Updating image rendition '{0}' with width '{1}' and height '{2}'", imageRendition.Name, imageRendition.Width, imageRendition.Height);

                var existingImageRendition = imageRenditionCollection.First(x => x.Name == imageRendition.Name);

                existingImageRendition.Width  = imageRendition.Width;
                existingImageRendition.Height = imageRendition.Height;
            }

            imageRenditionCollection.Update();
        }
        /// <summary>
        /// Updates image renditions configured in settings.xml pnp:ImageRenditions for the site configured in app.config.
        /// Deletes existing image renditions with the same name and different attributes first.
        /// </summary>
        /// <param name="clientContext"></param>
        private static void CreateImageRenditions(ClientContext clientContext)
        {
            var imageRenditions = SiteImageRenditions.GetRenditions(clientContext);

            clientContext.ExecuteQueryRetry();

            foreach (var node in _Branding.GetDescendants(_NameSpace + "ImageRenditions", _NameSpace + "ImageRendition"))
            {
                ImageRendition rendition = new ImageRendition()
                {
                    Name   = node.GetAttributeValue(_TokenParser, "Name"),
                    Width  = node.GetAttributeValue <int>(_TokenParser, "Width"),
                    Height = node.GetAttributeValue <int>(_TokenParser, "Height")
                };

                var existing = imageRenditions.FirstOrDefault(i => i.Name.Equals(rendition.Name, StringComparison.OrdinalIgnoreCase));
                if (existing != null && (existing.Width != rendition.Width || existing.Height != rendition.Height))
                {
                    imageRenditions.Remove(existing);
                    imageRenditions.Add(rendition);
                }
                else if (existing == null)
                {
                    imageRenditions.Add(rendition);
                }
            }

            SiteImageRenditions.SetRenditions(clientContext, imageRenditions);

            clientContext.ExecuteQueryRetry();
        }
Beispiel #6
0
        /// <summary>
        /// Removes an existing image rendition
        /// </summary>
        /// <param name="web">SharePoint Web</param>
        /// <param name="imageRenditionName">The name of the image rendition</param>
        public static void RemovePublishingImageRendition(this Web web, string imageRenditionName)
        {
            var imageRenditions = SiteImageRenditions.GetRenditions(web.Context);

            web.Context.ExecuteQueryRetry();
            var newRenditionList = imageRenditions.Where(i => i.Name != imageRenditionName).ToList();

            SiteImageRenditions.SetRenditions(web.Context, newRenditionList);
            web.Context.ExecuteQueryRetry();
        }
        private void DeployImageRenditionSettings(object modelHost, SiteModelHost siteModelHost,
                                                  ImageRenditionDefinition imageRenditionModel)
        {
            var renditions       = SiteImageRenditions.GetRenditions(siteModelHost.HostSite);
            var currentRendition = GetCurrentImageRendition(renditions, imageRenditionModel);

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

            if (currentRendition == null)
            {
                currentRendition = new ImageRendition
                {
                    Name   = imageRenditionModel.Name,
                    Width  = imageRenditionModel.Width,
                    Height = imageRenditionModel.Height
                };

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

                renditions.Add(currentRendition);
                renditions.Update();
            }
            else
            {
                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = currentRendition,
                    ObjectType       = typeof(ImageRendition),
                    ObjectDefinition = imageRenditionModel,
                    ModelHost        = modelHost
                });
            }
        }
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            var siteModelHost = modelHost.WithAssertAndCast <SiteModelHost>("modelHost", value => value.RequireNotNull());
            var definition    = model.WithAssertAndCast <ImageRenditionDefinition>("model", value => value.RequireNotNull());

            var renditions = SiteImageRenditions.GetRenditions(siteModelHost.HostSite);
            var spObject   = GetCurrentImageRendition(renditions, definition);

            var assert = ServiceFactory.AssertService
                         .NewAssert(definition, spObject)
                         .ShouldNotBeNull(spObject);
        }
        /// <summary>
        /// Method to remove an image matchingRenditions if is exist
        /// </summary>
        /// <param name="site">The current site</param>
        /// <param name="imageRenditionInfo">The image matchingRenditions to remove</param>
        public void RemoveImageRendition(SPSite site, ImageRenditionInfo imageRenditionInfo)
        {
            var imageRendition = SetImageRenditionProperties(imageRenditionInfo);

            if (site == null || imageRendition == null || !imageRendition.IsValid)
            {
                this.logger.Error("Error removing image rendition. Argument is null or invalid.");
                throw new ArgumentException("ImageRenditionHelper : argument is null of invalid.");
            }

            // Get the image existingImageRendition collection of the current site.
            var imageRenditionCollection = SiteImageRenditions.GetRenditions(site);

            var existingRendition = imageRenditionCollection.FirstOrDefault(x => x.Name == imageRendition.Name && x.Width == imageRendition.Width && x.Height == imageRendition.Height);

            if (existingRendition != null)
            {
                imageRenditionCollection.Remove(existingRendition);
                this.logger.Info("Removing image rendition '{0}' with width '{1}' and height '{2}'", imageRendition.Name, imageRendition.Width, imageRendition.Height);
            }

            imageRenditionCollection.Update();
        }
        /// <summary>
        /// Method to remove an image matchingRenditions if is exist
        /// </summary>
        /// <param name="site">The current site</param>
        /// <param name="containsPattern">Pattern to match to remove Image Rendition</param>
        public void RemoveImageRendition(SPSite site, string containsPattern)
        {
            if (site == null || string.IsNullOrEmpty(containsPattern))
            {
                this.logger.Error("Error removing image rendition. Argument is null or invalid.");
                throw new ArgumentException("ImageRenditionHelper : argument is null of invalid.");
            }

            // Get the image existingImageRendition collection of the current site.
            var imageRenditionCollection = SiteImageRenditions.GetRenditions(site);

            var matchingRenditions = imageRenditionCollection.Where(x => x.Name.ToUpperInvariant().Contains(containsPattern.ToUpperInvariant())).ToList();

            if (matchingRenditions != null && matchingRenditions.Any())
            {
                foreach (var imageRendition in matchingRenditions)
                {
                    imageRenditionCollection.Remove(imageRendition);
                    this.logger.Info("Removing image rendition '{0}' with width '{1}' and height '{2}'", imageRendition.Name, imageRendition.Width, imageRendition.Height);
                }
            }

            imageRenditionCollection.Update();
        }
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                var context = web.Context as ClientContext;
                var site    = (web.Context as ClientContext).Site;

                // Check if this is not a noscript site as publishing features are not supported
                if (web.IsNoScriptSite())
                {
                    scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_Publishing_SkipProvisioning);
                    return(parser);
                }

                var webFeatureActive  = web.IsFeatureActive(Constants.FeatureId_Web_Publishing);
                var siteFeatureActive = site.IsFeatureActive(Constants.FeatureId_Site_Publishing);
                if (template.Publishing.AutoCheckRequirements == AutoCheckRequirementsOptions.SkipIfNotCompliant && !webFeatureActive)
                {
                    scope.LogDebug("Publishing Feature (Web Scoped) not active. Skipping provisioning of Publishing Image Renditions");
                    return(parser);
                }
                else if (template.Publishing.AutoCheckRequirements == AutoCheckRequirementsOptions.MakeCompliant)
                {
                    if (!siteFeatureActive)
                    {
                        scope.LogDebug("Making site compliant for publishing");
                        site.ActivateFeature(Constants.FeatureId_Site_Publishing);
                        web.ActivateFeature(Constants.FeatureId_Web_Publishing);
                    }
                    else
                    {
                        if (!web.IsFeatureActive(Constants.FeatureId_Web_Publishing))
                        {
                            scope.LogDebug("Making site compliant for publishing");
                            web.ActivateFeature(Constants.FeatureId_Web_Publishing);
                        }
                    }
                }
                else if (!webFeatureActive)
                {
                    throw new Exception("Publishing Feature not active. Provisioning failed");
                }

                if (template.Publishing != null &&
                    template.Publishing.ImageRenditions != null &&
                    template.Publishing.ImageRenditions.Count > 0)
                {
                    var renditions = SiteImageRenditions.GetRenditions(context);
                    context.ExecuteQueryRetry();

                    foreach (var r in template.Publishing.ImageRenditions)
                    {
                        var rendition = new Microsoft.SharePoint.Client.Publishing.ImageRendition();
                        rendition.Name   = r.Name;
                        rendition.Height = r.Height;
                        rendition.Width  = r.Width;

                        if (!renditions.Any(rd => rd.Name == rendition.Name && rd.Height == rendition.Height && rd.Width == rendition.Width))
                        {
                            renditions.Add(rendition);
                        }
                    }

                    SiteImageRenditions.SetRenditions(context, renditions);
                    context.ExecuteQueryRetry();
                }
            }
            return(parser);
        }
        private void DeployImageRenditionSettings(object modelHost, SiteModelHost siteModelHost,
                                                  ImageRenditionDefinition imageRenditionModel)
        {
            var context    = siteModelHost.HostSite.Context;
            var renditions = SiteImageRenditions.GetRenditions(siteModelHost.HostSite.Context);

            context.ExecuteQueryWithTrace();

            var currentRendition = GetCurrentImageRendition(renditions, imageRenditionModel);

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

            if (currentRendition == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new Image Rendition");

                var newRendition = new ImageRendition
                {
                    Name   = imageRenditionModel.Name,
                    Width  = imageRenditionModel.Width,
                    Height = imageRenditionModel.Height
                };

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

                renditions.Add(newRendition);
                SiteImageRenditions.SetRenditions(context, renditions);

                context.ExecuteQueryWithTrace();
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing existing Image Rendition");

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