Get external URL without languageId

Vote:
 

Hi,

I have a scheduled job that is traversing pages on an enterprise site, and collecting their external URLs. The site consists of 4 domains with a shared database. 

My issue is that when trying to get external URL for pages that are not in the same language as the site executing the scheduled job, the URLs will contain the langauge code.

Example when running from local.mysite.no:

var url = "http://local.mysite.se/link/9621840053124503b70fd153698bac6b.aspx";
UrlBuilder uburl = new UrlBuilder(url);
Global.UrlRewriteProvider.ConvertToExternal(uburl, NULL, Encoding.UTF8);
// result: http://local.mysite.se/no/TheFriendlyUrlToMyArticle/

When browsing the converted url, I'll get a 404. However, if I remove the languageId it'll work fine.

Is it possible to get the external link regardless of the context you're in?

Thanks!

#119236
Mar 25, 2015 9:09
Vote:
 

Which EPiServer version you are using? What about UrlResolver? I guess language existance is controlled by multiple factors (SDK: Determining-languages). 

#119265
Mar 25, 2015 15:53
Vote:
 

Hi,

I'm at update 58. Using UrlResolver to get the external URL gives me the same result.

 

I'd could write some work-around code to remove the languageId based on the domain, but I'd rather do it the right way, if possible.

#119283
Mar 26, 2015 9:06
Vote:
 

Hi,

Not sure that I got you rsetup correctly but here are my results:

* setup single site to reply to 3 domains (alloy.se, alloy.dk, alloy.no);

* create single page (content Id: 166), translated to all 3 languages;

* created job that collects all 3 external links for all 3 languages for that page;

Code:

var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
var resolver = ServiceLocator.Current.GetInstance<UrlResolver>();

var page = loader.Get<StandardPage>(new ContentReference(166));
var branches = repo.GetLanguageBranches<StandardPage>(page.ContentLink);

var links = branches.Select(branch => resolver.GetUrl(branch.ContentLink, branch.Language.Name)).ToList();

Gave me following results:

links = ["http://alloy.no/test"; "http://alloy.se/test"; "http://alloy.dk/test"];
#119802
Apr 02, 2015 19:22
Vote:
 

NOTE: I think Valdis Iljuconoks is a lot nicer though... :)

We do the same at work, we have a site that has 16 different languages thus making it 16 different domains. We got a scheduled job that creates a sitemap for each domain. We solved the problem by changing the "current culture" like this:

//Save the current culture

var currentCulture = EPiServer.Globalization.GlobalizationSettings.CultureLanguageCode;

//Get all active languages, then loop through them

var availableLanguages = ServiceLocator
                                    .Current
                                   .GetInstance<ILanguageBranchRepository>()
                                    .ListEnabled()
                                    .OrderBy(l => l.Name)
                                    .ToList();

//In the loop set the culture to the domains culture

foreach(var language in availableLanguages)
{
       EPiServer.Globalization.ContentLanguage.Instance.SetCulture(language.Culture.Name);

//Get the link

var url = "http://local.mysite.se/link/9621840053124503b70fd153698bac6b.aspx";

UrlBuilder uburl = new UrlBuilder(url);

Global.UrlRewriteProvider.ConvertToExternal(uburl, NULL, Encoding.UTF8);

}  
//When the job is done, "restore" the culture
EPiServer.Globalization.ContentLanguage.Instance.SetCulture(currentCulture);
#119853
Edited, Apr 04, 2015 12:48
Vote:
 

Ok, I think I got it incorrectly. I was using translated page to all 3 languages. I tried with page in "svenska" from "norwegian" site with following code and UrlResolver was able to resolve from permanent link to correct external site url with correct host name mapping (when job is running executed manually and also when auto-scheduled):

var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
var resolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var linker = ServiceLocator.Current.GetInstance<PermanentLinkMapper>();

// get page that exists only in "svenska" culture
var page = loader.Get<StandardPage>(new ContentReference(169));

var links = new List<string>();
var maps = linker.Find(page.ContentLink); // "/link/79975d557c7241ed832849b047bea189.aspx"

var externalUrl = new UrlBuilder(maps.MappedUrl.ToString());
EPiServer.Global.UrlRewriteProvider.ConvertToExternal(externalUrl, null, Encoding.UTF8);

links.AddRange(new[]
{
    resolver.GetUrl(maps.MappedUrl.ToString()),
    resolver.GetUrl("http://alloy.se" + maps.MappedUrl),
    externalUrl.ToString()
});

All 3 links are: "http://alloy.se/svenska-page/".

If you are on update 58 you should really reconsider using something from EPiServer.Global :)

Can you paste a bit more code about how you are retrieving reference to the page to which you need to get external url (just wondering about usage of permanent links here).

#119863
Apr 04, 2015 18:58
Vote:
 

If you are on update 58 you should really reconsider using something from EPiServer.Global :)

Actually I am using EPiServer.Global :-) here:

Global.UrlRewriteProvider.ConvertToExternal(uburl, NULL, Encoding.UTF8);

(The using EPiserver; statement wasnt included in my snippet above, my bad).

Can you paste a bit more code about how you are retrieving reference to the page to which you need to get external url (just wondering about usage of permanent links here).

Yes. Whenenever the scheduled job is executed, it fetches a list of strings from an external source. These strings are in fact pageIds. 

The code iterates over these ids, fetching the PageData, like so:

var pagedata = DataFactory.Instance.GetPage(new PageReference(12345));



Afterwards the code I had in my first post is executed (I get the permalink from pagedata.StaticLinkURL).

I also tried the PermanentLinkMapper approach, but with the same result.

I'll give Josef's suggestion a go next.

#120183
Apr 13, 2015 13:17
Vote:
 

I managed to get it working by using Josef's approach.

I'll be looking out for a less hacky-way of doing this in the future though :-)

#120184
Apr 13, 2015 13:29
Vote:
 

Nice! Yeah I actually rewrote my code when I saw Valdis Iljuconoks answer, it's a lot cleaner, weird that it doesn't work for you but glad that my hacky solution worked :)

#120185
Apr 13, 2015 13:31
Vote:
 

Interesting to discover something new every day :) Glad that you got your issue resolved!

#120186
Apr 13, 2015 13:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.