Pages data type selection

Jim Piller asked on February 16, 2021 09:06

I am trying to create a list of related documents in a page type. I have added a field, RelatedArticlesList, and assigned the Pages data type and control to it. I have a very simple query to get the list of documents out, but I keep hitting a null reference exception. My question is, do I need to setup a relationship in the Relationship Name application in order to use this data type or is there some other way of getting the data out besides just a normal document query?

Recent Answers


David te Kloese answered on February 16, 2021 13:24

The null reference probably is hit because you have something like 'WHERE LEFNODEID = {%CurrentNodeID%}' which is fine but the CURRENT NODEID is unknown on NEW pages. As it's not yet inserted.

Could you share that simple query you mention?

0 votesVote for this answer Mark as a Correct answer

Jim Piller answered on February 18, 2021 17:25

here are the pieces of our selection code:

    //Add the necessary columns
    private string[] _resourceArticleColumns = new string[]
    {
        "ArticleName", "ArticleTitle", "ArticleTeaser", "RelatedArticlesList", "ArticleContent", "ArticleHeroImage", "ArticleTeaserImage", "NodeAliasPath", "ShowEmailForm", "CtaPath"
    };

     //Map the exported page type to the DTO object
     private Func<ResourceArticle, ResourceArticlePageDto> ResourceArticleSelect => resourceArticle => new ResourceArticlePageDto()
    {
        Header = resourceArticle.ArticleTitle,
        PageContent = resourceArticle.ArticleContent,
        HeroImage = resourceArticle.Fields.ArticleHeroImage,
        TeaserImage = resourceArticle.Fields.ArticleTeaserImage,
        TeaserText = resourceArticle.ArticleTeaser,
        NodeAliasPath = resourceArticle.NodeAliasPath,
        ShowEmailForm = resourceArticle.ShowEmailForm,
        SidebarCta = (!string.IsNullOrEmpty(resourceArticle.CtaPath) && resourceArticle.CtaPath != null) ? GetGlobalCtaByNodeGuid(resourceArticle.CtaPath) : null,
        **RelatedArticles = resourceArticle.Fields.RelatedArticlesList**
    }; 

    //Run the document query using an interface
    public ResourceArticlePageDto GetArticle(string nodeAliasPath)
    {
        return DocumentQueryService.GetDocument<ResourceArticle>(nodeAliasPath, true)
            .AddColumns(_resourceArticleColumns)
            .Select(ResourceArticleSelect)
            .FirstOrDefault();
    }

    //Document query service that gets the page type
     public DocumentQuery<TDocument> GetDocuments<TDocument>(bool loadBaseDtoFields = false) where TDocument : TreeNode, new()
    {
        var query = DocumentHelper.GetDocuments<TDocument>();

        // Evaluates whether preview mode is enabled
        if (SiteContext.IsPreviewEnabled)
        {
            if (loadBaseDtoFields)
                _coreColumns.Concat(new[] { "HideChildrenFromNavigation", "HideFromNavigation" });

            // Loads the latest version of pages as preview mode is enabled
            query = query
                .Columns(_coreColumns.Concat(new[] { "NodeSiteID" })) // Sets initial columns returned for optimization.
                                                                      //Adds 'NoteSiteD' column required for the Preview mode.
                .OnSite(SiteContext.SiteName) // There could be more sites with matching page
                .LatestVersion()
                .Published(false)
                .Culture(SiteContext.PreviewCulture);
        }

        if (!SiteContext.IsPreviewEnabled)
        {
            // Loads the published version of pages
            query = query
                .Columns(_coreColumns) // Sets initial columns returned for optimization.
                .OnSite(SiteContext.SiteName)
                .Published()
                .PublishedVersion()
                .Culture(SiteContext.CurrentSiteCulture);
        }

        return query;
    }

The bold line is where I am seeing the null reference error, which is actually happening in the generated code from Kentico at this point:

            /// <summary>
        /// Related Articles.
        /// </summary>
        public IEnumerable<TreeNode> RelatedArticlesList
        {
            get
            {
                return mInstance.GetRelatedDocuments("RelatedArticlesList");
            }
        }

RelatedArticlesList is an IEnumerable

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.