I have an article document type and in that I have a section for adding related links to other articles. In the related articles section I can add links to an external site by just providing the URL or I can add a link to an internal article I wrote.
When retrieving the related articles I need to be able to determine if this is one of my internal articles or a link to another site. The best way to do this would be to just grab the document type, then do a switch on that.
How would I go about getting the document type of a property within another document type?
Here is my foreach I'm using to iterate through the related articles:
@foreach (var article in CurrentPage.relatedArticles)
{
// render html based on articles document type
}
Not sure if I'm able to even get the document type of article as I can't see that property anywhere in the debugger.
I forgot to explain that some of my "internal" articles are actually just links to external articles that are related to my articles. I just set them up as internal articles so I can store meta data I get from the external article in my database. So they are all considered "internal" as far as Umbraco is concerned. This means that the link generated by Umbraco for that node goes nowhere.
What I ended up doing was taking the internal id in my foreach loop and using Umbraco.Content(internalId) to get the External Article node. If it actually is a node and not just a link then the id is greater than 0. After that I could get the DocumentTypeAlias and perform my if statement.
It's kind of a mess but it works.
Thanks for the help Laura!
Code for anyone interested:
var internalId = item.Value<int>("internal");
MyMetaInformationModel externalNewsArticle = null;
if (internalId > 0)
{
var node = Umbraco.Content(internalId);
if (node.DocumentTypeAlias == "externalArticleNews")
{
externalNewsArticle = JsonConvert.DeserializeObject<MyMetaInformationModel>(node.GetPropertyValue<string>("externalUrl"));
}
}
if (externalNewsArticle != null)
{
var linkUrl = externalNewsArticle.Url;
// got the external url YAY!
}
else
{
// just a regular article
}
Get Document Type Of Internal Related Article
I have an article document type and in that I have a section for adding related links to other articles. In the related articles section I can add links to an external site by just providing the URL or I can add a link to an internal article I wrote.
When retrieving the related articles I need to be able to determine if this is one of my internal articles or a link to another site. The best way to do this would be to just grab the document type, then do a switch on that.
How would I go about getting the document type of a property within another document type?
Here is my foreach I'm using to iterate through the related articles:
Not sure if I'm able to even get the document type of article as I can't see that property anywhere in the debugger.
Hey David,
What datatype are you using to store the related articles?
Is it the built-in picker or something like https://our.umbraco.com/packages/backoffice-extensions/multi-url-picker/ ?
The RJP MultiUrlPicker surfaces a property indicating what type of link has been added, which might be helpful for you :)
Cheers,
Laura
Hey Laura,
I'm actually using the obsolete Related Links data type (https://our.umbraco.com/documentation/getting-started/backoffice/property-editors/built-in-property-editors/related-links)
It's an older site and there's a lot of document types that are using this data type so unfortunately I don't think I can switch :(.
Best,
David
Hey David,
Looks like you might still be able to get the link type using one of the snippets posted on that page as a base:
The helpful bit being
If it is, then you can use the id to search for the content node, if it's not then it's presumably an external link.
Cheers,
Laura
Hey Laura,
I forgot to explain that some of my "internal" articles are actually just links to external articles that are related to my articles. I just set them up as internal articles so I can store meta data I get from the external article in my database. So they are all considered "internal" as far as Umbraco is concerned. This means that the link generated by Umbraco for that node goes nowhere.
What I ended up doing was taking the internal id in my foreach loop and using Umbraco.Content(internalId) to get the External Article node. If it actually is a node and not just a link then the id is greater than 0. After that I could get the DocumentTypeAlias and perform my if statement.
It's kind of a mess but it works.
Thanks for the help Laura!
Code for anyone interested:
Awesome :) glad you got it working!
is working on a reply...