Copied to clipboard

Flag this post as spam?

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


  • Firas Khalifeh 11 posts 42 karma points
    Jul 24, 2013 @ 15:44
    Firas Khalifeh
    0

    SiteMap for Umbraco V6.x MVC Engine

    Hi,

    is there a best practices on how to create a sitemap for Umbraco v6 using MVC rendering engine.

    I tried a lot, but no solution
    I have 2 problems:
    1. I created a template called sitemap, then i tried to access the template through example.com/sitemap (altTemplate), but cant make to work like >> example.com/sitemap.xml !

    2. cant include the homepage (most parent node ) to the iteration !

    please take a look at my code and advice 

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    <?xml version="1.0" encoding="UTF-8"?>

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

    @traverse(@Model.Content.AncestorOrSelf())

    </urlset>

    @{Response.ContentType = "application/rss+xml";Response.Charset = "utf-8";}

    @helper traverse(dynamic node)

    {var nodes = node.Children; 

     foreach (var n in nodes) 

     {var loc = HttpContext.Current.Request.ServerVariables["HTTP_HOST"] + n.Url;

    <url>

    <loc>@loc</loc>

    <lastmod>@n.UpdateDate.ToString("yyyy-MM-dd")</lastmod>

    </url>

     @traverse(n)

     }

     

    }

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 25, 2013 @ 21:48
    Jeavon Leopold
    0

    Hi Firas,

    1. I would recommend using a IIS rewrire rule to do this, e.g Rewrite sitemap.xml to /xml-sitemap

    2. Take a look at the NavIt package which can be used to generate a page based sitemap, it could be easily adapted to out xml instead.

    Hope that's helpful?

    Jeavon

  • Firas Khalifeh 11 posts 42 karma points
    Jul 26, 2013 @ 15:28
    Firas Khalifeh
    0

    Hi Jeavon,

    thanks a lot, 

    it seems really usefull package.

    but actually i got HTML sitemap not XML sitemap http://promoto.ae/sm

    and the most important i cant find a solution in umbraco, is to create http://promoto.ae/sitemap.xml with MVC + Razor

    thanks again.

    Firas

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 26, 2013 @ 16:54
    Jeavon Leopold
    1

    Ok, so firstly to create a partial that will return your tree with the correct elements for a xml sitemap:

    My file is /Views/Partials/XmlSitemap.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @{                   
        var homePage = Model.AncestorOrSelf(1);
    
        if (homePage != null && (homePage.Children.Any()))
        {        
            <url>
                <loc>@homePage.UrlWithDomain()</loc>                          
            </url>         
            @Traverse(homePage, false)    
        } 
    }</urlset>
    
    @helper Traverse(IPublishedContent parent, bool newList)
    {    
        var subPages = parent.Children.Where(x => x.IsVisible() && !x.HasProperty(DataNodePropertyAlias)).OrderBy(x => x.SortOrder);
        foreach (var page in subPages)
        {     
            <url>             
                <loc>@page.UrlWithDomain()</loc>
            </url>  
            @Traverse(page, true);
        } 
    }
    
    @functions{
        private const string DataNodePropertyAlias = "isDataNode";
    }

     Now it would be easy to include this in a view and it would render the required markup however there is a further complication, we need the response header content type to be "text/xml", this requires a SurfaceController:

    My file is in /Controllers/XmlSitemapSurfaceController.cs but if you don't have a VS.NET solution, place the file in the /App_Code/XmlSitemapSurfaceController.cs location

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Remoting.Messaging;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace MyNameSpace.Controllers
    {
        public class XmlSitemapSurfaceController : SurfaceController
        {
            public ActionResult Index()
            {
                Response.ContentType = "text/xml";
                return PartialView("XmlSitemap",CurrentPage);
            }
        }
    } 

    The final part is to render this from your view.

    My file /Views/XmlSitemap.cshtml

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;  
    }@Html.Action("Index","XmlSitemapSurface")
        

     Now either attach this view to a content node or request it as a alttemplate. e.g http://www.mysite.com/XmlSitemap and you should see the xml sitemap.

    I will address how to rewrite this to sitemap.xml separately! :-)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 26, 2013 @ 17:00
    Jeavon Leopold
    0

    Ok, so now assuming you are requesting your xml sitemap by alttemplate (so you don't need a node) e.g. /XmlSitemap/ we can now rewrite this using a IIS7 Rewrite rule (this extension must be installed in your IIS)

    Add this to your web.config within the system.webServer element:

         <rewrite>
          <rules>
            <rule name="Sitemap XML">
              <match url="sitemap.xml" />
              <action type="Rewrite" url="/XmlSitemap/" appendQueryString="false" />
            </rule>
          </rules>
        </rewrite>    

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 26, 2013 @ 17:53
    Jeavon Leopold
    100

    I forgot to mention that "isDataNode" is a convention that we use for Nodes that should always be ignored by sitemaps and navigation's etc, we add it as a label property to all document types that we know should always be ignored, kind of like a enforced umbracoNaviHide.

  • Firas Khalifeh 11 posts 42 karma points
    Jul 31, 2013 @ 01:21
    Firas Khalifeh
    0

    Thanks a lot,

    it is just works :D

  • molham bakir 18 posts 38 karma points
    Aug 14, 2013 @ 09:48
    molham bakir
    0

    Hi Guys, 

    It worked and has been submited into Google webmaster, but its still not appearing in Google search results! 

    If you type in Google 

    Site:promoto.ae

    you will notice that's there's only two indexed pages, which on of them is promoto.ae/sm 

    so i think we still didn't do it right. 

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 14, 2013 @ 10:06
    Jeavon Leopold
    0

    Hi Molham,

    Your xml sitemap is perfect however you have <meta name="robots" content="noindex"> on most pages while this is in place Google will not index these pages. You should change it to <meta name="robots" content="index,follow">

    Thanks,

    Jeavon

  • molham bakir 18 posts 38 karma points
    Aug 14, 2013 @ 10:20
    molham bakir
    0

    OMG!!! THANKS A LOT MAN. :'( 

    This is the most stupid mistake that shouldn't been done :( 

     

  • molham bakir 18 posts 38 karma points
    Sep 16, 2013 @ 11:02
    molham bakir
    0

    Unfortunity, promoto.ae only indexed my home page but not the others!

    why is that happening? is it because the sitemap.xml showing the page un defined (e.g. promoto.ae/about-us) there's no html or something?

    This is the message from Google webmaster: Googlebot couldn't crawl this URL because it points to a non-existent page. Generally, 404s don't harm your site's performance in search, but you can use them to help improve the user experience. More info.

    Another note: Usually when you copy a text from the home page and past it into Google, the links if that text should appear. but if i copy text from inside promoto.ae home page, google won't results! so basically non of the pages are indexing!

  • Robert Valcourt 71 posts 104 karma points
    Sep 02, 2015 @ 19:47
    Robert Valcourt
    0

    This is a great thread and it got me a working XML sitemap. I would like to enhance it further to include the lastmod property. In XSLT I would have used the following:

    <lastmod><xsl:value-of select="concat(@updateDate,'+00:00')" /></lastmod> 
    

    What would be the Razor/MVC equivalent?

    Update: I added the following to your script:

    <lastmod>@page.UpdateDate</lastmod>
    

    This outputs the date as: 7/31/2015 7:41:52 AM. But it needs to be in this format: 2014-12-01T09:31:24+00:00

    Update #2: this seems to be working thou it might not be the best approach:

    <lastmod>@page.UpdateDate.ToString("yyyy-mm-dd'T'hh:mm:ss'+00:00'")</lastmod>
    
  • 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