Get a sitemap in ASP.NET part from Umbraco - c#

I have a website which has two parts: the first one was made in Umbraco CMS (v4.7), the second one in ASP.NET.
The task is to get a sitemap (not just names of pages, I need some parameters as well) in ASP.NET part from Umbraco.
I have only one idea how I can do such thing - write an XML file in Umbraco and then open it in ASP.NET, but I did not find any way how exactly do it.
If you have any ideas, please help.
Many Thanks
Vlad

you can do something like this...
using umbraco.presentation.nodeFactory;
public string CreateSitemap()
{
var temp = "<ul>" + sitemap(-1) + "</ul>";
return temp;
}
public string sitemap(int nodeID)
{
var rootNode = new umbraco.presentation.nodeFactory.Node(nodeID);
string sitemapstring = "<li>" + rootNode.Name + "</li>";
if(rootNode.Children.Count>0)
{
sitemapstring+="<ul>";
foreach(Node node in rootNode.Children)
{
sitemapstring += sitemap(node.Id);
}
sitemapstring+="</ul>";
}
return sitemapstring;
}

Maybe you should check out the Razor edition of this sitemap package (at the bottom)?

Related

Adding page numbers to PDFs including the cover page

I'm trying to add a page number to the bottom of the page, which doesn't seem to work as expected.
Currently i'm using this:
<span class="page"></span>/<span class="topage"></span>
The problem with this solution is that it doesn't count the cover as a page.
So a 7 page PDF "has" 6 pages according to my code.
I'm looking for a way to include the Cover as a page, so the number is correct.
Currently i'm looking into some JS to manipulate it afterwards, but there have to me some "official" solution?
Solved using javascript. :)
If anyone are looking for the solution here you go:
var x=window.location.search.substring(1).split('&');
for (var i in x) {
var z=x[i].split('=',2);
vars[z[0]] = unescape(z[1]);
}
var pageNumberStart = parseInt(vars.page);
var pageNumberEnd = parseInt(vars.topage);
if (pageNumberStart != null && pageNumberEnd != null) {
document.getElementById('page').innerHTML = pageNumberStart + 1;
document.getElementById('topage').innerHTML = pageNumberEnd + 1;
}
Maybe someone got the official way to do it? :D

parsing and display xml data in asp.net core

Im still working on my first asp.net core project and now I want to display "a qoute of the day".
I have the qoutes in a xml file stored in a folder called File under wwwroot.
Im planning on my making this a View Component.
Im used to working with web forms so it seems like Im spending alot of time on small issues, but I guess its the only way to learn.
I've created a folder named Custom where I plan to hold all my custom classes. the QuoteController.cs is located in the Controllers folder.
So yeah, I think I know how to crate the View Component. "I think" is an important factor here.
Im also used to using XmlDocument, so Im trying my best to get XmlReader to work. But any hint or tips would be highly appreciated.
This is what I got so far. QuoteController.cs
public class QuoteController : Controller
{
public Custom.Quote Index()
{
Custom.Quote result = new Custom.Quote();
XmlReader rdr = XmlReader.Create(#"\File\qoutes.xml");
Random rnd = new Random(DateTime.Now.Millisecond);
int tmp = rdr.AttributeCount;
int count = rnd.Next(0, tmp);
int i = 0;
while (rdr.Read())
{
if (count.Equals(i))
{
result = new Custom.Quote(rdr.GetAttribute("q"), rdr.GetAttribute("author"));
break;
}
i++;
}
rdr.Dispose();
rdr = null;
rnd = null;
return result;
}
}
I guess the next step will be to add some visuals, but I cant imagine that my code actully works. Does anybody know how to easily parse through and xml file i CORE? Should I go for async?
I guess it doesnt matter, but the xml file is formated like:
<quotes>
<q>Be Strong</b>
<author>Stein The Ruler</author>
</quotes>
Again, I will be very happy if you take the time to look at this :)
Thank you!
My way to implement this:
1)convert the xmldocument to look like this
<quotes>
<quote Content="Be Strong" Author="Stein..."/>
</quotes>
2) Fix the Custom.Quote object to contain these 2 (public getters, setters string) fields: Content and Author,
and then,3) use this code to turn the xml to a list:
XDocument quotesDoc = XDocument.Parse('your path');
List<Custom.Quote> quotes = quotesDoc.Root
.Elements("quote")
.Select(x => new Speaker
{
Content= (string)x.Attribute("Content"),
Author = (string)x.Attribute("Author")
})
.ToList<Custom.Quote>();
Hope this helps!

kendo treeview mvc - error when clicking node

I'm getting an error within the kendo.all.min.js when I try and click on one of the nodes in a TreeView - anyone know if I have to further configure it to avoid this?
The error I'm getting is:
JavaScript runtime error: Unable to get property 'set' of undefined or null reference
and the rough area that is highlighted on error by VS is:
return arguments.length?(n=e(n,r).closest(q),r.find(".k-state-selected").each(function(){var e=i.dataItem(this);e.set("selected",!1),delete e.selected}),n.length&&i.dataItem(n).set("selected",!0)...
What i'm after is basically a list of files within a parent folder, and if the file is active and has child files, then I'd like it to give it a URL so they can open the link, but if not, I'd like nothing to happen, but I don't want to completely disable the node as I'd like the user to be able to expand it or shrink it if required.
Here is the code I'm using for the treeview in MVC:
#(Html.Kendo().TreeView()
.Name("treeview")
.Items(level1 =>
{
level1.Add().Text(rootFolderName)
.SpriteCssClasses("folder")
.Expanded(true)
.Items(level2 =>
{
int count = 0;
foreach (var node in list)
{
string title = node.Title;
string url = "";
if (node.HasChildren)
{
title = node.Title + "(" + node.ChildrenCount + ")";
url = "/Secure/Areas/Compliance.aspx?id=" + node.ItemId;
}
level2.Add().Text(title).Url(url)
.Expanded(true);
}
});
})
)
Anyone used these before and know what else I'm needing to do to achieve my aim?
Ok, learning more as I go. Ended up using an event like so:
#(Html.Kendo().TreeView()
.Name("treeview")
.Events(events => events
.Select("onSelect")
)
.Items(level1 =>
and then added a preventDefault if the url is empty
function onSelect(e) {
var dataItem = $('#treeview').data('kendoTreeView').dataItem(e.node);
if (dataItem.href == 'undefined')
e.preventDefault();
else
location.href = dataItem.href;
}
There is maybe a better way, but this seems to work so I'm happy for now.

Umbraco. Get node's url in console application

I work with Umbraco from Console application.
When I try get NiceUrl for some node it is impossible because UmbracoContext.Current is null.
I can get node path with ids like this: "-1,1067,1080", but don't know how convert it in url format.
How Can I get NiceUrl for Node in console application?
I did next:
In my console application I get node by Id, simple like this:
Node someNode = new Node(nodeId);
When I try get NiceUrl:
string url = someNode.NiceUrl;
get ArgumentNullException.
I checked why it: found next answer NiceUrl uses UmbracoContext so it is not possible because it's null.
Also I can't use this: UmbracoContext.Current.ContentCache.GetById(someidhere).Url
Thanks.
Without the UmbracoContext I don't think it's possible in V6 to get the URL of an IContent node.
I looked through the Umbraco source code and decided to recreate the way it's done there. I came up with this, which worked for my needs.
https://gist.github.com/petergledhill/ca2a3a0ea81b06abcb08
public static class ContentExtensions
{
public static string RelativeUrl(this IContent content)
{
var pathParts = new List<string>();
var n = content;
while (n != null)
{
pathParts.Add(n.UrlName());
n = n.Parent();
}
pathParts.RemoveAt(pathParts.Count() - 1); //remove root node
pathParts.Reverse();
var path = "/" + string.Join("/", pathParts);
return path;
}
public static string UrlName(this IContent content)
{
return new DefaultUrlSegmentProvider().GetUrlSegment(content).ToLower();
}
}
Yes, you can't use: UmbracoContext.Current.ContentCache because this is accessing the same context.
It looks like you are using v6+, so instead you will need to use the API services that Umbraco provide, specifically the ContentService.
There is a thread here that looks into the same thing you are asking: http://our.umbraco.org/forum/developers/api-questions/37981-Using-v6-API-ContentService-in-external-application
And an example of a solution here: https://github.com/sitereactor/umbraco-console-example

How to verify a Hyperlink exists on a webpage?

I have a need to verify a specific hyperlink exists on a given web page. I know how to download the source HTML. What I need help with is figuring out if a "target" url exists as a hyperlink in the "source" web page.
Here is a little console program to demonstrate the problem:
public static void Main()
{
var sourceUrl = "http://developer.yahoo.com/search/web/V1/webSearch.html";
var targetUrl = "http://developer.yahoo.com/ypatterns/";
Console.WriteLine("Source contains link to target? Answer = {0}",
SourceContainsLinkToTarget(
sourceUrl,
targetUrl));
Console.ReadKey();
}
private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
{
string content;
using (var wc = new WebClient())
content = wc.DownloadString(sourceUrl);
return content.Contains(targetUrl); // Need to ensure this is in a <href> tag!
}
Notice the comment on the last line. I can see if the target URL exists in the HTML of the source URL, but I need to verify that URL is inside of a <href/> tag. This way I can validate it's actually a hyperlink, instead of just text.
I'm hoping someone will have a kick-ass regular expression or something I can use.
Thanks!
Here is the solution using the HtmlAgilityPack:
private static bool SourceContainsLinkToTarget(string sourceUrl, string targetUrl)
{
var doc = (new HtmlWeb()).Load(sourceUrl);
foreach (var link in doc.DocumentNode.SelectNodes("//a[#href]"))
if (link.GetAttributeValue("href",
string.Empty).Equals(targetUrl))
return true;
return false;
}
The best way is to use a web scraping library with a built in DOM parser, which will build an object tree out of the HTML and let you explore it programmatically for the link entity you are looking for. There are many available - for example Beautiful Soup (python) or scrapi (ruby) or Mechanize (perl). For .net, try the HTML agility pack. http://htmlagilitypack.codeplex.com/

Categories

Resources