Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dem Muk 5 posts 106 karma points
    Mar 20, 2017 @ 12:04
    Dem Muk
    0

    Adding default image to old content

    I have loads of old content with missing images. I would like to check if there is an image on each of them. If not, then i'll display a default image which sits on the ancestor page. Instead of adding images to each content manually, whats the best approach. I am trying it with xslt as follows.

     <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
        version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Examine="urn:Examine" 
        exclude-result-prefixes="msxml umbraco.library Examine ">
    
    
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
        <xsl:variable name="mediaId" select="number($currentPage/heroImage)" />
        <xsl:choose>
              <xsl:when test="$mediaId &lt; 0">
                      <xsl:variable name="parentID" select="number($currentPage/ancestor-or-self::*[normalize-space(articleDefaultImage)][1]/umbracoImage)" /> 
                <xsl:if test="$parentID &gt; 0">
                  <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($parentID, 0)" />
                  <xsl:if test="$mediaNode/defaultImage">
                                <xsl:value-of select="$mediaNode/defaultImage" disable-output-escaping="yes"/>
                  </xsl:if>                                           
                </xsl:if>
              </xsl:when>
        </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    
  • Sven Geusens 169 posts 881 karma points c-trib
    Mar 22, 2017 @ 17:30
    Sven Geusens
    100

    Use an UmbracoAuthorizedApiController and the ContentService

    public class DefaultImageController: UmbracoAuthorizedApiController //Only works if you are logged into the backend
    {
        public string SetDefaultImages()
        {
            var defaultImageId = 0;
            var contentService = Services.ContentService;
            var rootnodes = Umbraco.TypedContentAtRoot(); //Gets from the cache so fast to get all nodes
            foreach (var node in rootnodes)
            {
                foreach (var descendandNode in node.Descendants().Where(n => n.HasProperty("nameOfImageProperty") && n.HasValue("nameOfImageProperty") == false)) //replace by node.Descendants("Doctypealias")... if you want to filter
                {
                    var editNode = contentService.GetById(node.Id); //Gets from the db = slow, so only do this if you want to edit.
                    editNode.SetValue("nameOfImageProperty", defaultImageId);
                    contentService.SaveAndPublishWithStatus(editNode);
                }
            }
    
            return "All nodes now have an image";
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies