Custom data source with linked pages

Tracey Penberthy asked on July 10, 2017 12:43

Hello there

I have built a Custom data source for a side navigation bar (which is outputted via a Hierarchical viewer).

It returns the current pages parent, siblings and children (one level). Here's the code:

protected override object GetDataSourceFromDB()
    {
        // Initializes the data properties according to the filter settings
        if (SourceFilterControl != null)
        {
            SourceFilterControl.InitDataProperties(this);
        }
        List<int> IncludeDocumentIds = new List<int>();
        // get parent
        if (!CurrentDocument.IsRoot())
        {
            if (!CurrentDocument.Parent.IsRoot())
            {
                IncludeDocumentIds.Add(CurrentDocument.Parent.DocumentID);
            }
        }
        // include current page children
        if (CurrentDocument.NodeHasChildren)
        {
            foreach(var c in CurrentDocument.Children)
            {
                IncludeDocumentIds.Add(c.DocumentID);
            }
        }
        // include current page siblings
        if(CurrentDocument.Parent != null)
        { 
            foreach(var s in CurrentDocument.Parent.Children)
            {
                IncludeDocumentIds.Add(s.DocumentID);
            }
        }
        string parentsids = string.Join(",", IncludeDocumentIds);

        // create where condition
        var where = new WhereCondition("DocumentID in (" + parentsids + ") and DocumentMenuItemHideInNavigation = 0");

        // Loads the data. The WhereCondition and OrderBy properties are inherited from the parent class.
        DataSource = DocumentHelper.GetDocuments(standardPageName)
            .Where(where)
            .Where(WhereCondition)
            .Columns(new String[] { "DocumentID", "NodeAliasPath","NodeLevel","DocumentName", "NodeParentID", "NodeID", "DocumentMenuItemHideInNavigation"})
            .OrderBy(OrderBy)
            .TypedResult;

        return DataSource;
    }

This has worked OK until people started adding linked pages and now I am getting some weird results

I have a page Level 1 - 1 and that has a child page of Level 2 - 1.

I created a linked page of Level 2 - 1 and selected it's parent as Level 1 - 2

However whenever I view Level 1 - 1 or Level 1 - 2 both the original and the linked page (Level 2 - 1) appear in the menu (I only want the relevant child to appear)

So for example when I select Level 1 - 1 I get

Home

    Level 1 - 1 (selected)

        Level 2 - 1

    Level 1 - 2

        Level 2 - 1 (linked)

    Level 1 - 3

    Level 1 - 4

When I want to get just

Home

    Level 1 - 1 (selected)

        Level 2 - 1

    Level 1 - 2

    Level 1 - 3

    Level 1 - 4

How can I alter my datasource to take these linked pages into account?

Many Thanks Tracey

Recent Answers


Trevor Fayas answered on July 10, 2017 15:30 (last edited on July 10, 2017 15:31)

I think you're missing NodeOrder in the Columns, in order to build a proper tree you need the NodeLevel, NodeOrder, NodeID, NodeParentID at a minimum.

Also make sure that you are ordering by NodeLevel, NodeOrder in order for it to work properly, that's the required organization for Hierarchy.

0 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on July 10, 2017 15:56

Thanks Trevor

I made the changes you suggested but unfortunately I am still get the related page showing with the original one.

Do I need to include something in my query to ensure Linked items are returned properly?

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 10, 2017 15:59

I'm sorry, i didn't read close enough. If you want to exclude linked documents, you may just need to add it to your where condition, NodeLinkedNodeID is null. The way it's working now is the 'default' way, where it considers a linked document as it's own document that should be displayed. Try that!

0 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on July 10, 2017 16:26

Thanks Trevor

It's not that I want to exclude the linked pages, I want them to display in their appropriate position and not as if they are still in the original position which is what they are doing now. It's like the parent node is not getting got properly for linked pages...

I'm sorry, it's really hard to explain but the lists I depicted in the original post should demonstrate what I mean.

0 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on July 10, 2017 16:32 (last edited on July 10, 2017 16:40)

Trevor

I think I have fixed it! I changed the where condition from "DocumentNodeID in..." to "NodeID in.. " and got NodesIDs rather than DocumentIDs when I was building my IncludeDocumentIds list.

It now works as expected.

Really appreciate your help with this one

2 votesVote for this answer Mark as a Correct answer

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