Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

How to get data about another page in Umbraco 7

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>
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")>
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;
        }
Umbraco Content Picker

Comments Page 1 of 1 (1 Post)

Thank you 3rd option helped a lot

Comment posted by Tom Jordan on 14 Nov 2018 12:02


Thanks, Tom

Pleased you found it helpful

 


Please enter your comment.