Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Sharing content across multiple sites in Kentico

Modified: 2010/02/24 16:04 by vanthoog - Categorized as: Kentico
Suppose you have a Kentico installation containing multiple websites, for example a corporate website and a Dutch website. And suppose you want to show news items from the corporate website in the Dutch website (using the Dutch masterpage and style sheet). This article shows two ways of achieving this.

Note: All code in this article has been written for testing and prototyping purposes only. For use in a proper application this code needs to be optimized and refactored.

The first approach is to use the (Kentico) CMSRepeater. This control contains a property SiteName which can be used to specify from which site to retrieve the content. Here is an example showing how to retrieve news items from the corporate site:

    <cms:CMSRepeater ID="CMSRepeater1" runat="server" 
        SiteName="CorporateSiteASPX"
        ClassNames="CMS.News" 
        CultureCode="en-US"
        TransformationName="cms.news.AVHPreview" 
        ItemSeparator="<hr/>"
        Path="/News/%" 
        ZeroRowsText="No news items found"
        />

The transformation, in this case "cms.news.AVHPreview", contains the code necessary for displaying the items:

<div class="newsItem">
<h3><a href="<%# AVHFunctions.FixUrlForCorporateItems(GetDocumentUrl()) %>"><%# Eval("NewsTitle") %></a></h3>
<p><%# AVHFunctions.FixImgSrcForCorporateItems(Eval("NewsSummary")) %></p>
</div>

Two special functions have been used in this transformation. The first one, AVHFunctions.FixUrlForCorporateItems, generates proper url’s to the news items in the corporate website. This is necessary because GetDocumentUrl returns a relative url and in this case we need absolute url’s. The second function, AVHFunctions.FixImgSrcForCorporateItems, fixes the url of src attribute in img-tags. This is necessary because, again, these url’s are relative and we need absolute url’s. Here is the code of these two functions:

    public static string FixImgSrcForCorporateItems(object txtValue)
    {
        if (txtValue == null || txtValue == DBNull.Value)
        {
            return ("");
        }
        else
        {
            string baseUrl = "http://localhost/KenticoCMS2_www";
            string result = txtValue.ToString();

            if (result.ToLower().Contains("src=\"~"))
            {
                // Fix url in image tags.
                result = result.Replace("src=\"~", "src=\"" + baseUrl);
            }

            return (result);
        }
    }

    public static string FixUrlForCorporateItems(object txtValue)
    {
        if (txtValue == null || txtValue == DBNull.Value)
        {
            return ("");
        }
        else
        {
            string baseUrl = "http://localhost";
            return (baseUrl + txtValue.ToString());
        }
    }

The second approach is to use a normal repeater and retrieve the proper news items using logic in the code-behind of the template. So the first step is to add a repeater to the page template:

    <asp:Repeater ID="LeftColumnRepeater" runat="server">
        <ItemTemplate>
            <b><%# Eval("Title") %></b>
            <br />
            <%# Eval("Content") %>
            <br />
            <a href="<%# Eval("Url") %>"><%# Eval("Url") %></a>
            <hr />
        </ItemTemplate>
    </asp:Repeater>

Now in code-behind the method SelectNodes of the TreeProvider class can be used to retrieve the proper items. The crucial point here is the first parameter which specifies from which site to retrieve the content.

    private void LoadItems()
    {
        // Retrieve all news items from the corporate site.

        // Retrieve the site object of the corporate site.
        CMS.SiteProvider.SiteInfo siteCorporate = CMS.SiteProvider.SiteInfoProvider.GetSiteInfo("CorporateSiteASPX");

        if (siteCorporate != null)
        {
            string baseUrl = ConvertUrlToFullUrl(siteCorporate.DomainName) + "/KenticoCMS2_www";
            
            CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider();

            // Retrieve the news items
            DataSet ds = provider.SelectNodes(siteCorporate.SiteName, "/News/%", CMSContext.PreferredCultureCode, true);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables0.Rows.Count > 0)
            {
                List<CorporateNewsItem> lfItems = new List<CorporateNewsItem>();

                for (int ii = 0; ii < ds.Tables0.Rows.Count; ii++)
                {
                    CMS.TreeEngine.TreeNode item = provider.SelectSingleNode((int)ds.Tables0.Rowsii"NodeID");

                    if (item != null)
                    {
                        // Create an object containing all the relevant data.
                        CorporateNewsItem lfItem = new CorporateNewsItem(item.NodeID, item.NodeAliasPath);

                        object tempObject = item.GetValue("NewsTitle");

                        if (tempObject != null)
                        {
                            lfItem.Title = tempObject.ToString();
                        }

                        tempObject = item.GetValue("NewsSummary");

                        if (tempObject != null)
                        {
                            string tempString = tempObject.ToString();

                            if (tempString.ToLower().Contains("src=\"~"))
                            {
                                // Fix for url in image tags.
                                tempString = tempString.Replace("src=\"~", "src=\"" + baseUrl);
                            }

                            lfItem.Content = tempString;
                        }

                        lfItem.Url = String.Format("{0}{1}.aspx", baseUrl, item.NodeAliasPath);

                        lfItems.Add(lfItem);
                    }
                }

                LeftColumnRepeater.DataSource = lfItems;
                LeftColumnRepeater.DataBind();
            }
        }

    }

    private string ConvertUrlToFullUrl(string url)
    {
        if (url != null && url != "" && !url.ToLower().Contains("http"))
        {
            return ("http://" + url);
        }
        else
        {
            return (url);
        }
    }

    protected class CorporateNewsItem
    {
        public int ID { get; set; }

        public string Path { get; set; }

        public string Title { get; set; }

        public string Content { get; set; }

        public string Url { get; set; }

        public CorporateNewsItem(int _iD, string _path)
        {
            this.ID = _iD;
            this.Path = _path;
        }
    }

Just like in the first approach we need some special logic to generate the proper url to the news items in the corporate site and to fix the url in src attribute of img-tags. Also notice how the method GetSiteInfo of the class CMS.SiteProvider.SiteInfoProvider has been used to retrieve the SiteInfo object of the corporate site. This SiteInfo object is used in various parts of the code to retrieve data (i.e. properties) of the corporate site.

 © Evident Interactive BV