3

I would like to have a sitecore task to run differently depending on the current site. My task is scheduled to run every 24h to regenerate the sitemap.xml file. Im working on a multisite environement where I have different domains :

  • DOMAIN.com
  • fr.DOMAIN.com
  • cn.DOMAIN.com

The problem is, when I launch my task from cn.DOMAIN.com, the Sitecore.Context is not set to my CN site, the language is not and even when using Sitecore.Globals.ServerUrl Im getting the base website url (DOMAIN.com) instead of the CN one.

My goal is to get the domain or the language of the site, FR for fr.DOMAIN.com or CN for cn.DOMAIN.com, to generate my sitemap.xml for each of my website in the correct language.

Any tips ?

  • Is the domain available from the site definition? If yes, you can use the Site Name as this is unique for each site. You can have a list of site names delimited by | for example. Then when the task runs, you split by | and then find the site and proceed with your inner logics. – Hishaam Namooya Aug 28 '17 at 7:48
3

If the different domains map 1 to 1 with a site node and those site nodes have different root paths then this should help. You should be able to make an extension method that finds the context site based on the root path like the following:

public static SiteInfo GetSite(this Item source)
{
   return Factory.GetSiteInfoList().FirstOrDefault(
      site => source.Paths.FullPath.StartsWith(site.RootPath));
}

Note that you might want to avoid other site contexts (i.e. modules_website).

Then you could use the site context switcher. Inside the using your context site should be set to the correct site context if your root path is set up correctly:

using (new SiteContextSwitcher(Factory.GetSite(item.GetSite().Name)))
{
   //Your code here
}

If your domains are all a part of one site definition using | as a delimiter in the hostname attribute this solution will not work. I found something that may be useful in this case: Sitecore.Data.Proxies.DomainSwitcher. I have never used this personally though.

Since you are using multiple site nodes that point to the same root item you can use the following to get the correct site contexts.

public static IEnumerable<SiteInfo> GetSites(this Item source)
{
   return Factory.GetSiteInfoList().Where(
      site => source.Paths.FullPath.StartsWith(site.RootPath));
}
| improve this answer | |
  • Sadly the rootpath is the same. At the moment I generate the sitemap for each site and only interact with the Robot.txt file to point to the right sitemap file. – TeKilla Aug 29 '17 at 10:36
  • Are these separate site nodes all pointing to the same rootpath or is it one site node that has a pipe delimited hostname attribute? – Teeknow Aug 29 '17 at 12:09
  • they are all pointing to the same rootpath, thus, the only possibility I see is to get the hostname to differentiate them but I can't access it :/ – TeKilla Aug 29 '17 at 12:13
  • Is there a site node for each domain or do any have a hostname attribute like this: hostName="example.com|www-origin.example.com"? If they do not have a hostName attribute like that, I have added another extension method that should help for your purposes. – Teeknow Aug 29 '17 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.