1
votes

have a multi-lingual site, e.g.

Content
 +UK Home
  -About Us
 +US HOme
  -About Us

How can I get US "about us" page based on current node of UK "about us" via razor? The US site is copied from UK site using relation.

When a visitor visits US "About Us", and the visitor is redirected to UK "About Us" based on business rule. The problem is that I need to programmatically get the valid url for UK "About Us" page, and redirect the visitor to it. It seems there is no much support from Umbraco. For example, Umbraco relation works only on home page level. Each site has a tree node with many levels.

Any idea would be appreciated.

Umbraco 4.10

2

2 Answers

2
votes

I just had to figure this out myself using 7.2.6...

Because BeaverProj's Answer is not complete and depricated, I thought I would share.

It would be nice if someone had a shorter bit of code to do the same.

@{

var rs = ApplicationContext.Current.Services.RelationService;
var currentPageId = CurrentPage.AncestorOrSelf().Id;

var relations = rs.GetByParentOrChildId(currentPageId);


        <h2>Relations</h2>
foreach (Relation relation in relations)
{
    if (relation.RelationType.Alias == "relateDocumentOnCopy")
    {
        // the relation has two ids... 
        // 1. parent
        // 2. child
        // we want the opposite one as the id above is for the current node
        umbraco.NodeFactory.Node node;

        if (relation.ParentId == currentPageId)
        {
            //get child
            node = new umbraco.NodeFactory.Node(relation.ChildId);
        }
        else
        {
            node = new umbraco.NodeFactory.Node(relation.ParentId);
        }

       <text>@node.NiceUrl</text>            <br />
    }

} }

I also tried to use razor with a lambda [where] statement to query the result only for the alias but it didn't work within the view. I may move this to a controller but it works in the view.

0
votes

When you created the multi-lingual site did you "Relate copied items to original" in the copy box? Or is there a relationship between the pages?

In Razor you should be able to access the Relation API native to Umbraco.

Something like this:

@using umbraco.cms.businesslogic.relation;
@using umbraco.cms.businesslogic.member;

@{
    RelationType relationType = RelationType.GetByAlias("relateDocumentOnCopy");
    Relation[] relations = Relation.GetRelations(Model.Id, relationType);

  <h2>Relations</h2>
    foreach (Relation relation in relations)
    {
...
    }
}

I got this code basically from this forum post: http://our.umbraco.org/forum/developers/razor/28103-Using-relations-with-razor

Here's a blog post with more info on the Relations API: http://blog.hendyracher.co.uk/umbraco-relation-api/

Also, if you have access to a paid Umbraco.tv account, this would help: http://umbraco.com/help-and-support/video-tutorials/developing-with-umbraco/relations/simple-document-to-document-relation/TVPlayer