12

We have a Sitecore installation with a content management server and a content delivery server.

Now: I have an item, for which I want to create a link (for sending in an email). The link should be created on the CM server, but point to the relevant page on the CD server.

The servers are on different domain, why it doesn't make sense to send the CM link.

Currently my endeavors are as follows:

string oldSiteName = Sitecore.Context.GetSiteName();
Sitecore.Context.SetActiveSite("website");

UrlOptions urlOptions = new UrlOptions
{
    AlwaysIncludeServerUrl = true,
    SiteResolving = true
};
string link = LinkManager.GetItemUrl(item, urlOptions);

Sitecore.Context.SetActiveSite(oldSiteName);

This, however, still generates links to the CM domain.

Edit: I added a part of the source code I forgot when first creating the post

  • Should not the domain only be changed? For example if on CM, the link is mycmserver/category/item. The CD link would be as follows: mycdserver/category/item. Only the domain is changed. Am I right? – Hishaam Namooya Oct 11 '16 at 7:53
  • The domain should be changed and also http vs / https. But generally I don't want to create the link myself. It is not very flexible. – Mads Buch Oct 11 '16 at 7:59
  • Then, why not implement a job which will perform the generation of the URL. You only need to provide the different hostname and the Scheme. This would be a one off project which you can reuse later – Hishaam Namooya Oct 11 '16 at 8:03
  • Like a service that runs on the CD server, and updates a list with mapping from ItemID's to URL's ? – Mads Buch Oct 11 '16 at 8:04
  • 1
    I need the link in so small quantities that it is infeasible, but a good solution otherwise :-) It doesn't solve my problem either. I would still need to know how to create links from CM – Mads Buch Oct 11 '16 at 8:10
14

You can use targetHostName setting in your site definition. It tells Link Manager to use that host name while generating url.

It doesn't change the way how sites are resolved on your CM and CD servers. This only tells Sitecore to generate links using that particular host name for your site no matter if you're on CM or CD server.

You can read more about site attributes here SDN - Site Attribute Properties

  • 2
    This solution would apply to all links generated in the CM context? Is there a way to make the context switch local? – Mads Buch Oct 11 '16 at 8:59
  • You can try to use Sitecore.Sites.SiteContextSwitcher. You would need to either have additional entry in your sites config called website_web or generate web version of your site and pass it to the switcher dynamically. – Marek Musielak Oct 11 '16 at 9:11
  • 1
    The problem with using the targetHostName attribute is if you have cross-site links then it will direct the users off to the CD servers. It's probably fine, but something to be aware of if people are testing and expect to stay on the same environment. – jammykam Oct 11 '16 at 9:14
3

As Marek mentioned, you should set @targetHostName on /configuration/sitecore/sites/site in your Sitecore.config.

Furthermore, you should create your UrlOptions object like this:

UrlOptions urlOptions = LinkManager.GetDefaultUrlOptions();
urlOptions.AlwaysIncludeServerUrl = true;

This ensures that your code keeps using the settings of LinkManager, except as explicitly overwritten by your code.

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.