How to get data about another page in Umbraco 7.7.1

Sometimes we need to return data from another page and display it on the page the user is currently on.

Depending on the amount of data we need to display will depend on how we achieve this.

In the code examples below, I will show you three ways that this can be done.


Option One, create a static class with a static IPublishedContent method, then on the page, we are on, we can pass in the page ID and retrieve the data we require.

I'm not a fan of this approach, as we are putting the code in the view which I'm not a great fan of.

Code
public static class PageDetails
    {
        public static IPublishedContent PageContent(int? pageId)
        {
            var pageDetails = new UmbracoHelper(UmbracoContext.Current);
            IPublishedContent content = pageDetails.TypedContent(pageId);
            return content;
        }
    }
//-------Page---------
 @{
     var data =   PageDetails.PageContent(UmbracoContext.Current.PageId);
        var image = data.GetPropertyValue<IPublishedContent>("Image").Url;
    }
<div>
    <p>@image</p>
</div>

Option Two, again create a static class but this time have it only return the data we need.

Then we can call the static class and display the data we need in the view as shown below.

2nd Option
public static class UmbracoHelperImage
        {
            public static string Image(string pageDocTypeName)
            {
                //Here we are getting an image from the home page and displaying it on another page
                UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                IPublishedContent currentPage = umbracoHelper.AssignedContentItem;
                IPublishedContent pageContent = currentPage.AncestorOrSelf(1).DescendantOrSelf(pageDocTypeName);

                string image = string.Empty;
                string doesImageExist = pageContent.GetPropertyValue<string>("image", "NA");

                if (int.TryParse(doesImageExist, out _))
                {
                    image = pageContent.GetPropertyValue<IPublishedContent>("image").Url;
                }

                return string.IsNullOrEmpty(image) ? "/images/defaultImg.png" : image;

            }
        }
//-------Page------------
<img src=@UmbracoHelperImage.Image("homePage")>

Option 3 My preferred option is to add a content picker to the settings doctype, this could be on the home page or a dedicated doctype that you use to add all your settings.

The advantage of this option is the content editor can delete the doctype you selected and add a new one to display the page. Then all they need to do is update the content picker with the new doctype and the CMS will do the rest.

Below is an example of how to do this.

Code for displaying content picker
public static string LinkToPrivacyPolicy()
        {
            UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            IPublishedContent homePageNode = umbracoHelper.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "homePage");
            string privacy = homePageNode.GetPropertyValue<IPublishedContent>("privacyPage").Url;
            return privacy;
        }

I hope you found this blog helpful, if yes, please leave a comment below.

Blog Form

 Please complete the required fields (*required)

 *
*