Get the same columns from different classnames

Kenny Deblaere asked on November 10, 2016 13:38

Good afternoon

I want to select items from different classes in Kentico. So I have an array with strings:

var allowedClassNames = new string[] { "project.classnameA", "project.classnameB","project.classnameC"}

Each classname has en need to have following columns:

var columnTitles = "title,DocumentName,DocumentNodeID,DocumentUrlPath,NodeParentID,NodeLevel,ClassName"

Only the column title is a custom column, the other ones are columns from Kentico.

When I'm trying:

var _tree = new TreeNode();
var nodes = _tree.SelectNodes().Types(allowedClassnames).Columns(columnTitles);

This gives an error: ColumnName 'title' does not exist.

If've fixed it with writing:

var _tree = new TreeNode();
var nodes = new List<TreeNode>();
foreach(var item in allowedClassNames) {
    var elements = _tree.SelectNodes().Type(item, x => x.Columns(columnTitles);
    nodes.AddRange(elements);
}

This is working, but the problem is I've been doing allowedClassNames.Length - times a call to the database, which is giving an error on production:

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - The specified network name is no longer available.)

I guess this explains perfectly why I need a solution to do only one call.

Kind regards

Kenny

Recent Answers


Roman Hutnyk answered on November 10, 2016 14:16

You need to do something like this:

var childNodes = DocumentHelper.GetDocuments()
            .Published()
            .Where(where)
            .Columns(new List<string>() {"NodeID","NodeAliasPath","NodeHasChildren","DocumentID","DocumentName","ClassName", "PdfFile"})
            .Type("Type_1", q => q.Columns("PdfFile"))
            .Type("Type_2", q => q.Columns("PdfFile"))
            .Type("Type_3", q => q.Columns("PdfFile"))
            .Type("Type_4", q => q.Columns("PdfFile"))
            .Type("Type_5", q => q.Columns("PdfFile"))
            .Type("Type_6", q => q.Columns("PdfFile"))
            .Type("Type_7", q => q.Columns("PdfFile"))
            .Type("Type_8", q => q.Columns("PdfFile"))
            .Type("Type_9")
            .CheckPermissions();

This is just an example... adjust it to your needs.

1 votesVote for this answer Mark as a Correct answer

Kenny Deblaere answered on November 10, 2016 15:07

Is there a way to dynamically add these Type files?

Just so I do not need to hard-code these items?

Kind regards

Kenny

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on November 10, 2016 15:22

You can build your query dymanicaly:

var childNodes = DocumentHelper.GetDocuments()
            .Published()
            .Where(where)
            .Columns(new List<string>() {"NodeID","NodeAliasPath","NodeHasChildren","DocumentID","DocumentName","ClassName", "PdfFile"});
foreach(var item in allowedClassNames) {
    childNodes = childNodes.Type(item, q => q.Columns("PdfFile"));
}
1 votesVote for this answer Mark as a Correct answer

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