Thanks, I realize that each node may have several relations and I need id of relation type "RotatorImage"
which has an id of '3'.
I got this:
var s = ApplicationContext.Current.Services.RelationService.GetByParentId(item.Id);
var rotatorImageId = 0;
foreach (var relationItem in s)
{
if(relationItem.RelationTypeId == 3)
{
rotatorImageId = relationItem.ChildId;
}
}
<p>@rotatorImageId</p>
This works, but I am just curious....
For better self-documentation, it would be nice to use .RelationType == "RotatorImage"
or even better, use a method that allows filtering by RelationType. For the former,
I see that both .RelationType and .RelationType.ToString() returns literally:
"Umbraco.Core.Models.RelationType"
That is an object? And for the latter, I don't see how to filter the relations on a given node, by type.
It seems like since var s above holds all my relations, I should be able to filter on
a specific one of those relations, by it's type. But none of the methods seem to do that directly.
For example, I could get a relation type object for my Relation Type w/ id = 3 (that's
"RotatorImage"):
var t = ApplicationContext.Current.Services.RelationService.GetRelationTypeById(3);
And then use some method like:
s.GetAllRelationsByRelationType(t)
But my 's' variable does not have the method GetAllRelationsByRelationType
(I'm looking up the object methods and properties using VS intellisense).
Anyways, what we got gets me through this, so no big deal. But if the answer is obvious
or my understanding is way off, please let me know!
var rotatorImageId = 0;
var s = ApplicationContext.Current.Services.RelationService.GetByParentId(item.Id).FirstOrDefault(x => x.RelationTypeId == 3);
if (s != null)
{
rotatorImageId = s.ChildId;
}
Also there many other methods from the RelationService and they are documentated here
I was testing with the below and it works for me without any using statement:
@{
var item = Model.Content;
var rotatorImageId = 0;
var s = ApplicationContext.Current.Services.RelationService.GetByParentOrChildId(item.Id).FirstOrDefault(x => x.RelationTypeId == 1);
if (s != null)
{
rotatorImageId = s.ChildId;
}
}
I tried this exactly w/in the context that item.Id is known to be good:
var rotatorImageId = 0;
var s = ApplicationContext.Current.Services.RelationService.GetByParentOrChildId(item.Id).FirstOrDefault(x => x.RelationTypeId == 3);
if (s != null)
{
rotatorImageId = s.ChildId;
}
And I get error. This works:
var rotatorImageId = 0;
var s = ApplicationContext.Current.Services.RelationService.GetByParentId(item.Id);
foreach (var relationItem in s)
{
if(relationItem.RelationTypeId == 3)
{
rotatorImageId = relationItem.ChildId;
}
}
I'm using the Umbraco Partial View Macro Files editor. When I load the page that uses this macro, the page works,
but where the Macro would be I just get:
var dynamicPublishedMNTPNodeList = new List<string>();
foreach (var id in CurrentPage.cprHighlights)
{
dynamicPublishedMNTPNodeList.Add(id.InnerText);
}
var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
@foreach (var item in dynamicMNTPCollection)
{...}
Ah yes, that is it, as you have used dynamic CurrentPage instead of Model.Content we need to cast the Id to use the lambda expression. It would be like this:
var s = ApplicationContext.Current.Services.RelationService.GetByParentId((int)item.Id).FirstOrDefault(x => x.RelationTypeId == 3);
Phew, I never like it when I can't work out why something works for one person but not another! :-)
Relation Types + Partial View Macro + Umbraco 6
I have this old XSLT from Umbraco version 4.5 that gets node ids given a relation type. It worked through the "ps" namespace below:
Later in the XSLT, I can get the node ids by relation type, like this:
I want to rewrite this XSLT to run in a Razor Partial View File.
Inside of a for-loop, I have access to my node ids (stored in @item).
I have tried a couple different things that I found, but can't get it to work.
I tried this:
Umbraco Relations Documentation
I added these usings in my Razor Partial View:
But as soon as I try to use it, like this:
I get error.
I thought I could filter by the type "RotatorImage" (that's the Alias of my Relation Type), so I can get at the image related to each node like this:
.....
So if I can't do that, I also found this on GitHub:
Relation Service Documentation
So in this case, I got these usings statements:
Then, I can get at my relations, by @item.Id like this:
But how can I get at the nodeId of s? I believe my s object is a Relation object in Umbraco.Core.Models.Relation.
Hi Jacob,
How about:
Jeavon
Thanks, I realize that each node may have several relations and I need id of relation type "RotatorImage" which has an id of '3'.
I got this:
This works, but I am just curious....
For better self-documentation, it would be nice to use .RelationType == "RotatorImage" or even better, use a method that allows filtering by RelationType. For the former, I see that both .RelationType and .RelationType.ToString() returns literally:
That is an object? And for the latter, I don't see how to filter the relations on a given node, by type.
It seems like since var s above holds all my relations, I should be able to filter on a specific one of those relations, by it's type. But none of the methods seem to do that directly.
For example, I could get a relation type object for my Relation Type w/ id = 3 (that's "RotatorImage"):
And then use some method like:
But my 's' variable does not have the method GetAllRelationsByRelationType (I'm looking up the object methods and properties using VS intellisense).
Anyways, what we got gets me through this, so no big deal. But if the answer is obvious or my understanding is way off, please let me know!
thanks Jeavon
Hi Jacob,
You could do something like this:
Also there many other methods from the RelationService and they are documentated here
Jeavon
Thanks for reference and I tried the above...
I can't get it to work, but have narrowed it down to this:
Do I need to include a using statement for Linq, like @using System.Linq or something?
Hi Jacob,
I was testing with the below and it works for me without any using statement:
Jeavon
Thx Jeavon,
I tried this exactly w/in the context that item.Id is known to be good:
And I get error. This works:
No big deal, it's working.
Hi Jacob,
Strange it doesn't work for you. Does it cause an error or just nothing displays?
Jeavon
Jeavon,
I'm using the Umbraco Partial View Macro Files editor. When I load the page that uses this macro, the page works, but where the Macro would be I just get:
Hi Jacob,
Could you check your log4net (\App_Data\Logs\UmbracoTraceLog.txt) file to see if there is some more info in there?
Jeavon
Last error is:
Ah, I think I know what it is. How do you set what "item" is?
item is accessed in the foreach loop
Ah yes, that is it, as you have used dynamic CurrentPage instead of Model.Content we need to cast the Id to use the lambda expression. It would be like this:
Phew, I never like it when I can't work out why something works for one person but not another! :-)
Yes, it works. Success. Thx.
is working on a reply...