Displaying DynamicContent by referenced content ID
I created two modules with following fields using Module Builder:
Staff
=====
Name Short text
Bio Long text
CustomBlog
==========
BlogDate Date and Time
BlogAuthor Array of GUIDs
The BlogAuthor field is selector for dynamic items of "Staff".
And I am trying to display latest 2 "CustomBlog" posts on the "Staff" detail page, where "Staff" item is referenced in "CustomBlog" posts.
However, "BlogAuthor" Guid value and "Staff" ID doesn't seem to match.
Here is what I have:
01.
protected
override
void
OnInit(EventArgs e)
02.
03.
base
.OnInit(e);
04.
this
.detailContainer.DataBound += detailContainer_DataBound;
05.
06.
07.
protected
void
detailContainer_DataBound(
object
sender, EventArgs e)
08.
09.
var detailContainer = (DynamicDetailContainer)
this
.FindControl(
"detailContainer"
);
10.
var detailContainerItem = ((Telerik.Sitefinity.DynamicModules.Model.DynamicContent[])detailContainer.DataSource)[0];
11.
12.
Guid staffID = detailContainerItem.Id;
13.
14.
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
15.
Type blogpostType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.CustomBlog.CustomBlog"
);
16.
17.
var blogposts = dynamicModuleManager.GetDataItems(blogpostType)
18.
.Where(i => i.GetValue<TrackedList<Guid>>(
"BlogAuthor"
).ToString() == staffID.ToString())
19.
.OrderByDescending(d => d.GetValue<DateTime?>(
"BlogDate"
))
20.
.Take(2);
21.
22.
List<DynamicContent> blogpostsCollection = blogposts.ToList();
23.
24.
RadListView blogpostsListView = sender
as
RadListView;
25.
blogpostsListView.DataSource = blogposts;
26.
blogpostsListView.DataBind();
27.
I think the issue is here: Where(i => i.GetValue<TrackedList<Guid>>("BlogAuthor").ToString() == staffID.ToString())
you are comparing a Guid string to a TrackedList<Guid>, and these types don't match. You could try different things like GetValue<TrackedList<Guid>>(BlogAuthor).Contains(staffID) but you'd have to run that once for each ID.
Looking at the dynamic content documentation it appears that DynamicContentItems have a property called SystemParentItem that allows you to immediately retrieve the parent types as well as SystemChildItems which returns an IQueryable of DynamicContent items that are children of that type.
I haven't played with any of these items so I can't say from experience how well they work, but if you find this useful, I'd appreciate hearing your take on how well (if at all) it works for you.
Hi SelAromDotNet, thanks for the hint. Using a different way of getting Guid was the key part.
Since the Guid I was looking for an array of guids, I called it as GetValue<Guid[]>("BlogAuthor")
But for me another issue was also, using Id instead of OriginalContentId.
I updated the code accordingly. This works for me:
01.
protected
void
detailContainer_DataBound(
object
sender, EventArgs e)
02.
03.
var detailContainer = (DynamicDetailContainer)
this
.FindControl(
"detailContainer"
);
04.
var detailContainerItem = ((Telerik.Sitefinity.DynamicModules.Model.DynamicContent[])detailContainer.DataSource)[0];
05.
06.
var staffID = detailContainerItem.OriginalContentId;
07.
08.
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
09.
Type blogpostType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.FoundationBlog.FoundationBlog"
);
10.
11.
var blogposts = dynamicModuleManager.GetDataItems(blogpostType).Where(i =>
12.
i.Status == ContentLifecycleStatus.Live &&
13.
i.Visible &&
14.
i.GetValue<Guid[]>(
"BlogAuthor"
).Contains(staffID))
15.
.OrderByDescending(d => d.GetValue<DateTime?>(
"BlogDate"
))
16.
.Take(2);
17.
18.
var blogpostsListView = (RadListView)
this
.detailContainer.Controls[0].FindControl(
"blogpostsListView"
);
19.
blogpostsListView.DataSource = blogposts;
20.