Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

New Post: Selecting a specific localisation of a page title in C#

$
0
0
When you call Page.Data it tries to find a page by key in the current language/publication scope, if it is missing, it will throw an exception.
The valud is cached afterwards for the DataReference<Page> object it is called on.
using System;
using System.Linq;
using System.Globalization;
using Composite.Core.Xml;
using Composite.Data;
using Composite.Data.Types;

namespace My.Data.Method
{
    public static class InlineMethodFunction
    {
        public static string GetPageTitle(DataReference<IPage> Page)
        {
            if (Page == null)
            {
                return null;
            }
            
            using (var conn = new DataConnection(new CultureInfo("en-GB"))
            {
                Guid pageId = (Guid)Page.KeyValue
                return conn.Get<IPage>().First(p => p.Id == pageId).Title;
            
                // Alternative, but may not work if page.Data is already evaluated and cached for another version
                // return page.Data.Title;
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 2540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>