Wrong DocumentNamePath when inserting page via API

Gustavo Quevedo asked on August 13, 2019 13:59

Hi,

I'm having an issue when inserting a page (Blog post comment) using the API.

The page is added correctly except for the fact that the DocumentNamePath property is always set with a value at the root level. The DocumentAliasPath and the rest of properties look okay.

This causes that the page appears duplicated in the content tree: in the correct location but also under the root.

I have checked that the parent node has the correct DocumentNamePath value when calling Insert (i.e. not at the root) and I have added a few columns to the query that might help but I'm missing something else.

Please my find code below:

protected readonly string[] BasePageColumns = 
{
    "NodeGUID", "DocumentID", "NodeID", "DocumentPageTitle", "DocumentPageDescription", 
    "DocumentPageKeyWords", "NodeAlias", "NodeAliasPath", "NodeSiteID", "DocumentCulture", 
    "BlogPostID", "DocumentNamePath", "DocumentGUID", "DocumentName", "DocumentUrlPath"
};

public BlogPost GetBlogPostNode(int blogPostId)
{
    return DocumentHelper.GetDocuments<BlogPost>()
        .Columns(BasePageColumns)
        .OnSite(SiteContext.CurrentSiteName)
        .Published()
        .PublishedVersion()
        .WhereEquals("BlogPostID", blogPostId)
        .FirstOrDefault();
}

///

var blogPost = GetBlogPostNode(blogPostId);

var postComment = TreeNode.New<BlogPostComment>("IR.BlogPostComment");
postComment.DocumentName = "Post Comment " + (count++).ToString();
postComment.DocumentCulture = LocalizationContext.CurrentCulture.CultureCode;
postComment.BlogPostID = blogPostId;
postComment.Insert(blogPost);

And this is how the general tab of the new page looks like:

Image Text

Any help would be appreciated.

Kind regards, Gustavo

Correct Answer

Gustavo Quevedo answered on August 13, 2019 17:05

It seems to be resolved after updating the GetBlogPostNode method to retrieve the node with a TreeProvider instead, as below:

public BlogPost GetBlogPostNode(int blogPostId)
{
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
    return tree.SelectNodes<BlogPost>()
        .Where(bp => bp.BlogPostID == blogPostId)
        .FirstOrDefault();
}
0 votesVote for this answer Unmark Correct answer

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