2

In our Sitecore 9 setup we are using a CD and CM server.

Currently, we have these config transformations:

<sites env:require="TestCM" xdt:Transform="Insert">
    <site name="website">
      <patch:attribute name="targetHostName">tst-cms-customer.domain.com</patch:attribute>
      <patch:attribute name="hostName">tst-cms-customer.domain.com</patch:attribute>
    </site>
</sites>
<sites env:require="TestCD" xdt:Transform="Insert">
    <site name="website">
      <patch:attribute name="targetHostName">tst-customer.domain.com</patch:attribute>
      <patch:attribute name="hostName">tst-customer.domain.com</patch:attribute>          
    </site>
</sites>

which ensures that the enduser can reach the website using tst-customer.domain.com, while the Sitecore editor login using tst-cms-customer.domain.com (notice the cms part).

Now, we have a custom command, which creates urls to pages using this code:

LinkField lf = item.Fields[fieldId];
var options = LinkManager.GetDefaultUrlOptions();
options.AlwaysIncludeServerUrl = true;
options.SiteResolving = true;
return lf.TargetItem == null ? string.Empty : LinkManager.GetItemUrl(lf.TargetItem, options);

The urls are stored in a file.

All the urls created using this command, contains this: tst-cms-customer.domain.com.

We want the urls to contain the url to the enduser site, i.e. tst-customer.domain.com.

Do we need to change the targethostname (for the CM) to be tst-customer.domain.com? Or how do we accomplish this?

1 Answer 1

2

targetHostName attribute is used to generate URLs from linkManager when SiteResolving is set to true.

The host name to use when generating URLs to items within this site from the context of another site. If the targetHostName attribute is not available, Sitecore uses the value of the hostName attribute instead. The attribute is only used when the value of the Rendering.SiteResolving setting is true.

If the Rendering.SiteResolving setting is true, and the dynamic link manager can determine a logical Web site for the linked item, and that site is not the context site, and the targetHostName attribute of that site has a value, then link manager uses the targetHostName attribute. If the targetHostName attribute has no value, and the hostName attribute has a value, and that value does not contain an asterisk character (“*”) or a pipe character (“|”), then the link manager uses the hostName attribute.

https://doc.sitecore.com/SdnArchive/SDN5/Articles/Administration/Configuring%20Multiple%20Sites/Adding%20New%20Site/site%20Attribute%20Properties/targetHostName.html

So if you want to achieve/use front-end site url for the command and at the same time use cms url for general CM site's urls, then only way I can see is to have a different site definition and run your above mentioned command in that new site definition context.

OR

you can directly feed the hostName into LinkManager.GetDefaultUrlOptions() that you have defined in your command code.

OR

Create a new custom attribute for Site definition node and define/use that attribute value from your Custom Command ex: http://sitecoreblog.blogspot.com/2011/05/add-custom-property-to-site-node.html

4
  • Thanks! My solution is a multisite solution . so does that mean I would have to create a specific site definition for each of the sites, and run my command within the site-context of the new site? Your second suggestion, would mean, that for each item, I would have to determine which site the item belong to - I have not seen builtin logic for this?
    – Hos
    Commented May 18, 2020 at 4:30
  • Other option would be to define a custom site definition attribute and then use that attribute value for your custom command code - sitecoreblog.blogspot.com/2011/05/… Commented May 22, 2020 at 15:49
  • Can you pass that custom attribute value to the linkmanager somehow? Like the "Hostname" property?
    – Hos
    Commented May 24, 2020 at 19:25
  • @Hos you can access that custom property (from your context site which you are running the command code) and map it to options in your shown code. Commented May 24, 2020 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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