Copied to clipboard

Flag this post as spam?

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


  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 10:58
    n00b
    0

    Adding query to sitemap generator file (check for boolean value)

    Hi all. Me again!

    Wondered if someone could offer me a solution (probably a real easy syntax issue) to this:

    I'd like to add a query that checks to see whether a checkbox is ticked on every page. If it is, it needs excluding from the sitemap generator.

    @using Umbraco.Core.Models
    @using Umbraco.Web
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @ListChildNodes(Model.Content.AncestorOrSelf(1))
    
    @helper ListChildNodes(IPublishedContent startNode)
    {
        const int maxLevelForSiteMap = 100;
    
        foreach (var node in startNode.Children.Where(x => Umbraco.MemberHasAccess(x.Id, x.Path)).Where(x => !Umbraco.IsProtected(x.Id, x.Path)).Where(x => x.IsVisible())
        .Where(x => x.HasProperty("CanonicalTag")).Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")) == true))
       .Where(x => x.HasProperty("hideFromSitemap")).Where(x => 0(x.GetPropertyValue("hideFromSitemap")==true))
        {
            if (node.TemplateId > 0)
            {
                <url>
                    <loc>@node.UrlWithDomain()</loc>
                    <lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
                    @{
                        var freq = node.GetPropertyValue<string>("SearchEngineSitemapChangeFreq");
                        var pri = node.GetPropertyValue<string>("SearchEngineSitemapPriority");
                    }
    
                    @if (!string.IsNullOrEmpty(freq))
                    {
                        <changefreq>@freq</changefreq>
                    }
                    @if (!string.IsNullOrEmpty(pri))
                    {
                        <priority>@pri</priority>
                    }
                </url>
            }
    
            if (node.Level <= maxLevelForSiteMap && node.Children.Any())
            {
                @ListChildNodes(node)
            }
        }
    }
    

    My addition is the .where(x =>x.HasProperty(hideFromSitemap) line.

    Any help appreciated! (ps. i know my CanonicalTag doesn't follow naming convention properly! )

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 11:03
    Alex Skrypnyk
    0

    Hi n00b

    I think you need this code:

    .Where(x => x.GetPropertyValue<bool>("hideFromSitemap"))
    

    Also, I'm not sure that you have right brackets, my VS shows that it should be like that:

    foreach (var node in startNode.Children.Where(x => Umbraco.MemberHasAccess(x.Id, x.Path)).Where(x => !Umbraco.IsProtected(x.Id, x.Path))
    .Where(x => x.IsVisible())
    .Where(x => x.HasProperty("CanonicalTag"))
    .Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")) == true)
    .Where(x => x.HasProperty("hideFromSitemap"))
    .Where(x => x.GetPropertyValue<bool>("hideFromSitemap")))
    

    Hope it will help, thanks/

    Alex

  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 11:07
    n00b
    0

    I should also add, that my intention was to remove the 'Is.Visible' query to enable all pages to be included unless my hideFromSitemap is checked.

    (The underlying issue is that 'IsVisible' not only removes pages from the menu, but means they're excluded from sitemap generation too).

    My checkbox was to allow all pages to be included, and go through manually making sure unimportant ones had the box ticked to exclude.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 11:15
    Alex Skrypnyk
    0

    Also, I'm not sure that this line is working, what type of data in "CanonicalTag" property?

    .Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")) == true)
    
  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 11:20
    n00b
    0

    Wow! you're fast. Thanks guys.

    The CanonicalTag is a text string. What should happen is:

    Check if there's a canonical tag If the canonicaltag is different to the page url { exclude this page from sitemap}

    The hideFromSitemap is a boolean checkbox What should happen is:

    if hideFromSitemap is True { exclude this page from sitemap}

    Will try both your suggestions, thanks.

  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 11:31
    n00b
    0
    .Where(x => x.IsVisible())
        .Where(x => x.HasProperty("CanonicalTag"))
        .Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")) == true))
        .Where(x => x.HasProperty("hideFromSitemap"))
        .Where(x => x.GetPropertyValue<bool>("hideFromSitemap"))
    

    This outputs nothing in the sitemap xml.

    the CanonicalTag needs to be checked against the page URL. If it's different, don't include it. If it's the same, it can be included in the list.

    I think i'm struggling to understand the use of 'IsVisible' - we have a checkbox on every page called 'Hide Page' (umbracoNaviHide) which I understand it hides from menu, but not sure whether this is hiding pages from the sitemap too.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 11:33
    Alex Skrypnyk
    0

    Try this one:

    .Where(x => x.IsVisible())
    .Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")))
        .Where(x => !x.GetPropertyValue<bool>("hideFromSitemap"))
    
  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 11:40
    n00b
    0

    Thanks Alex, It partially generates the page, but not quite!

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    Error loading Partial View script (file: ~/App_Plugins/CultivSearchEngineSitemap/Views/MacroPartials/Sitemap.cshtml)
    </urlset>
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 11:50
    Alex Skrypnyk
    0
    .Where(x => x.IsVisible())
    .Where(x => x.HasProperty("CanonicalTag"))
    .Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")))
    .Where(x => x.HasProperty("hideFromSitemap"))
        .Where(x => x.GetPropertyValue<bool>("hideFromSitemap")))
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 11:44
    Alex Skrypnyk
    0

    and can you check what is the error? maybe in the log?

  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 11:56
    n00b
    0

    i can only view Trace Logs (if this is what you mean), however there are no entries for the last 2 hours.

    the last entry was:

    Application shutdown. Details: HostingEnvironment

    _shutDownMessage=HostingEnvironment initiated shutdown
    HostingEnvironment caused shutdown
    
    _shutDownStack=   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
       at System.Environment.get_StackTrace()
       at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
       at System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand()
       at System.Web.Hosting.PipelineRuntime.StopProcessing()
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 12:56
    Alex Skrypnyk
    0

    What version of your code doesn't throw an error?

  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 12:58
    n00b
    0
    @using Umbraco.Core.Models
    @using Umbraco.Web
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @ListChildNodes(Model.Content.AncestorOrSelf(1))
    
    @helper ListChildNodes(IPublishedContent startNode)
    {
        const int maxLevelForSiteMap = 100;
    
        foreach (var node in startNode.Children.Where(x => Umbraco.MemberHasAccess(x.Id, x.Path)).Where(x => !Umbraco.IsProtected(x.Id, x.Path)).Where(x => x.IsVisible())
        .Where(x => x.HasProperty("CanonicalTag")).Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")) == true))
        {
            if (node.TemplateId > 0)
            {
                <url>
                    <loc>@node.UrlWithDomain()</loc>
                    <lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
                    @{
                        var freq = node.GetPropertyValue<string>("SearchEngineSitemapChangeFreq");
                        var pri = node.GetPropertyValue<string>("SearchEngineSitemapPriority");
                    }
    
                    @if (!string.IsNullOrEmpty(freq))
                    {
                        <changefreq>@freq</changefreq>
                    }
                    @if (!string.IsNullOrEmpty(pri))
                    {
                        <priority>@pri</priority>
                    }
                </url>
            }
    
            if (node.Level <= maxLevelForSiteMap && node.Children.Any())
            {
                @ListChildNodes(node)
            }
        }
    }
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 12, 2017 @ 13:39
    Alex Skrypnyk
    0

    You have to add to this code check for your new property:

    .Where(x => !x.GetPropertyValue<bool>("hideFromSitemap"))
    

    If 'hideFromSitemap' checked => do not include page in sitemap

  • n00b 33 posts 134 karma points
    Jun 12, 2017 @ 14:09
    n00b
    0

    Ok thanks. Done that, however I may be missing something? It doesn't work, which leads me to think that I may not have created the boolean value correctly.

    All i did to add the boolean value was edit the composite to feature another field. Should i republish site or carry out any other tasks to complete this or is that all I need to do? enter image description here

  • n00b 33 posts 134 karma points
    Jun 13, 2017 @ 08:47
    n00b
    0

    (almost) Solved it! name of variable/property hideFromSitemap wasn't correct - fixed. brackets in wrong places - fixed.

    now i have another issue whereby it's pulling in pages from the manually created redirect list in web.config! AARRRGGGHHHH

    Here's the working code for anyone else facing a similar issue:

    @using Umbraco.Core.Models
    @using Umbraco.Web
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @ListChildNodes(Model.Content.AncestorOrSelf(1))
    
    @helper ListChildNodes(IPublishedContent startNode)
    {
        const int maxLevelForSiteMap = 100;
    
        foreach (var node in startNode.Children.Where(x => Umbraco.MemberHasAccess(x.Id, x.Path)).Where(x => !Umbraco.IsProtected(x.Id, x.Path))
        .Where(x => x.HasProperty("CanonicalTag")).Where(x => string.IsNullOrEmpty(x.GetPropertyValue<string>("CanonicalTag")) == true)
        .Where(x => x.HasProperty("hideFromSitemap")).Where(x => x.GetPropertyValue<bool>("hideFromSitemap") == false))
        {
            if (node.TemplateId > 0)
            {
                <url>
                    <loc>@node.UrlWithDomain()</loc>
                    <lastmod>@(string.Format("{0:s}+00:00", node.UpdateDate))</lastmod>
                    @{
                        var freq = node.GetPropertyValue<string>("SearchEngineSitemapChangeFreq");
                        var pri = node.GetPropertyValue<string>("SearchEngineSitemapPriority");
                    }
    
                    @if (!string.IsNullOrEmpty(freq))
                    {
                        <changefreq>@freq</changefreq>
                    }
                    @if (!string.IsNullOrEmpty(pri))
                    {
                        <priority>@pri</priority>
                    }
                </url>
            }
    
            if (node.Level <= maxLevelForSiteMap && node.Children.Any())
            {
                @ListChildNodes(node)
            }
        }
    }
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jun 13, 2017 @ 08:53
    Alex Skrypnyk
    0

    Hi

    How did you create manually created redirects?

    Is it like a list of urls, then you parse this list and if page is in this list - redirect?

    Alex

  • n00b 33 posts 134 karma points
    Jun 13, 2017 @ 09:02
    n00b
    0

    I have no access to the server to do this in IIS, so had to add them to the web.config :

    <rewrite>
    <!-- rewrite map added 07-06-17 -->
    <rewriteMaps>
    <rewriteMap name="MyRewriteMap">
    <add key="/our-product-range/xxxx123/" value="/" />
    </rewriteMap>
    </rewriteMaps>
     <rules>
            <!-- rewrite map rule added 07-06-17 -->
            <rule name="Rewrite rule1 for MyRewriteMap" stopProcessing="true">
              <match url=".*" />
                <conditions>
                  <add input="{MyRewriteMap:{REQUEST_URI}}" pattern="(.+)" />
                </conditions>
                <action type="Redirect" url="{C:1}" appendQueryString="false" />
            </rule>
          </rules>
    </rewrite>
    
Please Sign in or register to post replies

Write your reply to:

Draft