How to get the domain name of the page from which the link originates

kentico team nextscape asked on October 19, 2022 14:20

Hello, everyone.

There are currently three sites with PortalEngine in Kentico 12.0.95.

I implemented the following process in Transformation (ASCX format) to get the domain of the link source using the links on the pages of other sites as clues.

<script runat="server">

    protected string SiteTopAlias {set; get;}
    protected string ImagePath {set; get;}
    protected string DomainName = "";
    protected int NodeID;
    protected int NodeLinkedNodeID;
    protected int NodeSiteID;

    protected override void OnInit(EventArgs e)
    {
         NodeID = ValidationHelper.GetInteger(Eval("NodeID"), 0);

        CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(CMS.Membership.MembershipContext.AuthenticatedUser);
        CMS.DocumentEngine.TreeNode node = CMS.DocumentEngine.DocumentHelper.GetDocument(NodeID, "global", true, tree);
        NodeLinkedNodeID = node.NodeLinkedNodeID;
        CMS.DocumentEngine.TreeNode originalNode = CMS.DocumentEngine.DocumentHelper.GetDocument(NodeLinkedNodeID, "global", true, tree);

        NodeSiteID = 0;
        if(originalNode != null)
        {
            NodeSiteID = originalNode.NodeSiteID;
            CMS.SiteProvider.SiteInfo siteInfo = CMS.SiteProvider.SiteInfoProvider.GetSiteInfo(NodeSiteID);
            DomainName = "//" + siteInfo.DomainName;
        }

However, this implementation method seems to increase the load on the DB and I am wondering if there is a better way to acquire the original domain. Could you give me some advice?

Correct Answer

Juraj Ondrus answered on October 20, 2022 08:36

  1. Theoretically yes, as there has to be extra logic executed to parse the SCRIPT part. It is just really a bad practice (there still could be legit edge cases to use it this way). The code logic should be in a code file. The ASCX transformations are virtual so there is no code file = add the logic to the custom transformation method class. It is better from the debugging, maintenance and "for future developer" perspective. Using it the way you are using it, it might be difficult to find the code in the future for somebody else. Not mentioning that you will need to move to MVC development model at some point.

  2. Consider switching to text transformation and use macros - no need for extra transformation methods. you could use macro like: {% CurrentDocument.NodeLinkedNode.NodeOriginalNode.Site.SiteDomainName %}

  3. The code could look like this, but I would still recommend using it inside a custom transformation method (this is where I tested it):
    public partial class CMSTransformation {

        public string CustomTransformationMethod(int NodeID)
        {
            TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
            var page = tree.SelectSingleNode(NodeID);
    
            string domainName = page.Site.DomainName;
    
            return domainName; 
        }
    }
    

And then, in the ASCX transformation: <%# CustomTransformationMethod(Eval< int >("NodeLinkedNodeID")) %> (do not forget to add the NodeLinkedNodeID column name to the Columns property list so the listing web part gets the value. )

1 votesVote for this answer Unmark Correct answer

Recent Answers


Juraj Ondrus answered on October 20, 2022 06:27

I would recommend to create a custom transformation method for this. Using it like this is against all best practices - using script in ASCX code.

Then, in the current node object, which is the linked node, you already have the NodeLinkedNodeID - so you can use this one to get the original node right ahead, there is no need to get the linked node data.

0 votesVote for this answer Mark as a Correct answer

kentico team nextscape answered on October 20, 2022 07:41

Hi, Juraj.

Thank you for your answer. I have two questions.

I would recommend to create a custom transformation method for this.

Does this mean that using custom transformation will reduce the load on the database?

You already have the NodeLinkedNodeID - so you can use this one to get the original node right ahead, there is no need to get the linked node data.

Does this one mean that there is some superior (unnecessary) code in my code?

Where exactly is that? How should I write it? I also use 'NodeLinkedNodeID'.

Best regards.

0 votesVote for this answer Mark as a Correct answer

kentico team nextscape answered on October 24, 2022 02:31

Sorry for the delay in replying. Thank you for giving me more details.

It is very helpful. I will continue to study it.

I will close this one.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.