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.

public static class PageDetails
        public static IPublishedContent PageContent(int? pageId)
            var pageDetails = new UmbracoHelper(UmbracoContext.Current);
            IPublishedContent content = pageDetails.TypedContent(pageId);
            return content;
     var data =   PageDetails.PageContent(UmbracoContext.Current.PageId);
        var image = data.GetPropertyValue<IPublishedContent>("Image").Url;

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.

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("image", "NA");

                if (int.TryParse(doesImageExist, out _))
                    image = pageContent.GetPropertyValue("image").Url;

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

<img src="/@UmbracoHelperImage.Image(&quot;homePage&quot;)" alt="" />

Option Three.

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.

public static string LinkToPrivacyPolicy()
            UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            IPublishedContent homePageNode = umbracoHelper.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "homePage");
            string privacy = homePageNode.GetPropertyValue("privacyPage").Url;
            return privacy;

Above are three ways to get data about another page in Umbraco 7, I hope you found this post helpful.