Umbraco Multinode picker, match list with currentpage - c#

I need some help with logics how to loop through the items selected in the multinodepicker on all childrens and match them with currentpage type...
Current code:
#{
var constructionInfo = Umbraco.Content(2032); //Driftinfo
}
#Articles(constructionInfo)
#helper Articles(IPublishedContent page)
{
//ToDo: Match contentpicker or tags with currentpage.
var children = page.Children.Where(x => x.GetPropertyValue<string>("relaterandeFastigheter") == Model.Content.DocumentTypeAlias);
var relatedArticles = page.Children;
foreach (var article in children)
{
<article>
<h2>#article.GetPropertyValue("rubrik")</h2>
#article.GetPropertyValue("text")
</article>
}
}
So basicly what I tried doing with
var children = page.Children.Where(x => x.GetPropertyValue<string>("relaterandeFastigheter") == Model.Content.DocumentTypeAlias);
Was to match the property with the Model.Content.DocumentTypeAlias. However, I need to somehow match them with the multiple content in the contentpicker as its not single...
Could anyone assist me in finding a solution?

var children = page.Children.Where(x => x.GetPropertyValue<string>("relaterandeFastigheter").Split(',').ToList().Contains(Model.Content.Id.ToString()));
That's how I solved it!

Related

HTML Agility Pack - removing nested strong and em tags

I need to strip nested bold and italic tags from HTML but keep the content and also keep the top level bold or italic tag.
So for example the following:
<p><strong>Some text has<strong>been</strong>made bold</strong> and some text not bold</p>
Would become this:
<p><strong>Some text has been made bold</strong> and some text not bold</p>
Also this needs to work for multiple nested tags therefore the following:
<p><strong>Some text<strong> has<strong>been</strong>made bold</strong></strong> and some text not bold</p>
Would also become:
<p><strong>Some text has been made bold</strong> and some text not bold</p>
I started writing the following using HTML Agility pack and although this seems to work when there is only 1 nested bold tag it doesn't work correctly when there are multiple nested tags:
// Loop through bold and italic tags
List<string> boldAndItalicTagNames = new List<string>() { "strong", "em" };
var boldAndItalicTags = htmlDoc.DocumentNode.SelectNodes(string.Join("|",
boldAndItalicTagNames.Select(x => "//" + x)));
if (boldAndItalicTags != null)
{
foreach (var tag in boldAndItalicTags)
{
// If tag doensn't have any child nodes (i.e. it is empty)
if (!tag.HasChildNodes)
{
// Remove child and continue to next iteration
tag.ParentNode.RemoveChild(tag);
continue;
}
// If tag has children of same type (i.e. if strong tag has children strong tags)
var childrenOfSameType = tag.ChildNodes.Where(x => x.Name == tag.Name).ToList();
if (childrenOfSameType.Any())
{
// Loop through child nodes
for (var i = childrenOfSameType.Count - 1; i >= 0; i--)
{
// Get child node and remove tags but keep content
var child = childrenOfSameType[i];
child.ParentNode.RemoveChild(child, true);
}
}
}
}
I ended up with the following solution to this problem, it is probably not the best solution but it seems to be working therefore it will have to do for now unless anyone can think of anything better.
// Iterate through p tags
var pTags = htmlDoc.DocumentNode.SelectNodes("//p");
if (pTags != null)
{
foreach (var pTag in pTags)
{
// Iterate through bold and italic tags within P tags
List<string> boldAndItalicTagNames = new List<string>() { "strong", "em" };
var boldAndItalicTags = pTag.SelectNodes(string.Join("|", boldAndItalicTagNames.Select(x => ".//" + x)));
if (boldAndItalicTags != null)
{
foreach (var tag in boldAndItalicTags)
{
// Remove any other nested tags
tag.InnerHtml = tag.InnerHtml.Replace($"<{tag.Name}>", "").Replace($"</{tag.Name}>", "");
}
}
}
}

Filter Umbraco dynamic variable by Alias in foreach

I am trying to filter a dynamic variable by the Alias and would like to know if this is possible as when I write a lamba expression eg
foreach (var item in node.Children.Where(x => x.NodeTypeAlias != "ContentContainer"))
This results in an error - use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delagate or an expression tree type..
Please can someone help me on this.
if (sub.NodeTypeAlias == "MenuExternalLink")
{
UrlPickerState urlPicker = UrlPickerState.Deserialize(sub.GetPropertyValue("LinkURL"));
subUrl = urlPicker.Url;
subTarget = urlPicker.NewWindow ? "_blank" : "";
//get root home page node
var homePage = CurrentModel.AncestorsOrSelf(1).First();
//get the node under the homepage where the node name matches the redirect (MenuExternalLink) node name
var menuNode = homePage.Children.Where(x => x.NodeTypeAlias == "NormalTextPage" && x.Name.Contains(sub.Name) && x.GetPropertyValue("umbracoNaviHide") != "1");
//get the nodes under menuNode
foreach (var q in menuNode)
{
//we have the id of the node so get the child nodes based on that id
var node = Library.NodeById(q.Id);
foreach (var item in node.Children.Where("NodeTypeAlias == \"NormalTextPage\""))
{
<li ><a href="#item.Url" >#item.Name</a></li>
}
}
}

Umbraco foreach children of a child page

I have news posts within a news page within a homepage on my content structure
Example:
Homepage
- News
-- News Posts
I'm looking to have some of the news feed on my homepage in a foreach statement. In my head it should be as simple as:
#foreach (var homenews in CurrentPage.Children.Children)
{
if (homenews.Name == "News Post")
{
//Do some stuff//
}
}
Obviously that doesn't work so has anybody got any ideas? Thanks
When you're walking the tree you have to remember that a property (or method) like Children or Descendants() will return a collection of objects, so you can't just call Children of a collection. You can only call Children on a single object.
You can find the correct child of the homepage by using something like var newsPage = CurrentPage.Children.Where(x => x.DocumentTypeAlias == "NewsListingPage") and then extract the children of that page.
I ended up getting the news page by its id and then getting it's children from there. The below code worked for me. Thanks guys.
#{
var node = Umbraco.Content(1094);
<p>#node.Id</p> // output is 1094
foreach (var item in node.Children.Where("Visible").Take(3))
{
<p>#item.exampleText</p>
}
}
You need to reference the required node by NodeTypeAlias of it's Document Type.
So assuming the alias of the DocType of your News Posts is “NewsPosts” then...
#foreach (var homenews in #Model.Descendants().Where("NodeTypeAlias == \"NewsPosts\"")).Take(3)
{
<p>#homenews.Name<p>
}
...should return the name of the first 3 news posts.
I had the exact scenario, this is how I got mine working. NodeByID was very useful nad -1 indicates the root
#foreach(var item in Model.NodeById(-1).Children)
{
string itemName = item.Name;
switch(itemName)
{
case "News":
#* News *#
<div id="News">
<h3>#item.Name</h3>
#foreach (var newsPost in item.Children.OrderBy("UpdateDate desc").Take(4).Items)
{
<p>
#newsPost.Title
</p>
}
</div>
}
}

Umbraco get llist of pages created by user

Is possible to get pages which were created by user in umbraco cms. For example my username is admin, and I want to get list all of pages which were created by me using c# code (page name and page url).
It is possible and pretty straight forward. Here are a couple of examples (I'm using razor to spit out the values, but this could easily be put into a user control, or written out to a file or whatever):
Using DynamicNode:
#{
var userId = 0; //admin
var root = Library.NodeById(-1);
var nodes = root.Descendants().Where("CreatorId == #0", userId);
foreach (var node in nodes)
{
#:#node.Id, #node.Name, #node.Url<br />
}
}
Using NodeFactory and uQuery:
#{
var userId = 0; //admin
var root = new Node(-1);
var nodes = root.GetDescendantNodes(n => n.CreatorID == userId);
foreach (var node in nodes)
{
#:#node.Id, #node.Name, #node.Url<br />
}
}
Just replace 0 withe the Id of your user.

preventing duplicates when inserting nodes to treeview control

I want to create a hierarchical view of strings based on first two characters.
If the strings are:
AAAA,AAAA,BBDD,AABB,AACC,BBDD,BBEE
I want to reate a treeview that looks like this:
AA
AAAA
AABB
AACC
BB
BBDD
BBEE
I currently have some code that looks like this (inside a loop over the strings):
TreeNode pfxNode;
if (treeView1.Nodes[pfx]!=null) {
pfxNode = treeView1.Nodes[pfx];
}
else {
pfxNode = treeView1.Nodes.Add(pfx);
}
if (!pfxNode.Nodes.ContainsKey(string)) {
pfxNode.Nodes.Add(string, string + " some info");
}
For some reason this ends up with multiple "AA" nodes at the top level.
What am I missing?
please no pre-filtering of strings I want to be able to check if a specific treenode exists based on its key.
thanks
else {
pfxNode = treeView1.Nodes.Add(pfx);
}
There's your mistake, you are forgetting to set the key of the tree node. So the next ContainsKey() won't find it. Fix:
pfxNode = treeView1.Nodes.Add(pfx, pfx);
Use this:
var q = from s in arr
group s by s.Substring(0, 2) into g
select new
{
Parent = g.Key,
Children = g.Select (x => x).Distinct()
};
foreach (var item in q)
{
var p = new TreeNode(item.Parent);
TreeView1.Nodes.Add(p);
foreach (var item2 in item.Children)
p.Nodes.Add(new TreeNode(item2));
}

Categories