Get tags from PageControls
I have some widgets on a page that I want to change based on what other content on a page is tagged with. This needs to happen programmatically without any user interaction. How could I go about getting the tags that are on various pieces of content on the page? For example, how could I get the tags off of a news or event item that is on the same page?
What I want to know in the end is how to get the DataItem out of the PageControl so that I can then get the "Tags" off of it.
Wow, I've wasted a lot of time trying to figure this out. Anyone got an idea?
Well, I finally figured it out. I was testing with a shared content item so here is my solution based on that. I had to get the shared content id from the properties and use the content manager to retrieve the content and then get the value of the Tags as follows:
foreach (var control in page.Controls)
var value = control.Properties.FirstOrDefault(x => x.Name == "SharedContentID").Value;
var id = Guid.Parse(value.ToString());
var taxonIds = (TrackedList<Guid>)contentManager.GetContent(id).GetValue("Tags");
This code just works for my specific test. I will need to get the object type and handle each object differently likely but this works for a control with an object type of "Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock"
Lies...this only works for ContentBlocks. I still can't figure out how to get the ID's for other types like News and Events.
Erik, I was literally about to reply with the control.Properties suggestion that you discovered, sorry that I wasn't able to get to you sooner, but I'm very glad you figured it out!
You can improve this however by eliminating the foreach and using LINQ directly on the page control:
p.Page.Controls.Where(c => c.ObjectType.StartsWith(
typeof
(WhateverType).FullName))
Here is the solution I ended on to retrieve tags from controls on the page. I loop over the controls on the page and call the appropriate methods based on the ObjectType. For a number of the objects (News, BlogPosts, Events) I have to use the following method to find the UserControl that relates to the PageControl:
public
T GetContentItemFromControl<T>(ObjectData control, IManager manager) where T : IDynamicFieldsContainer
//get the current page
var page = HttpContext.Current.Handler
as
Page;
if
(page ==
null
)
return
default
(T);
//get the id of the coresponding user control
var userControlId = control.Properties.FirstOrDefault(x => x.Name ==
"ID"
).Value;
//find the user control by its id
var view = (IContentView)page.FindControl(userControlId);
if
(view ==
null
|| view.DetailViewDefinition ==
null
)
return
default
(T);
//get the id of the detail data item (IContentViewDetailDefinition)
var itemId = view.DetailViewDefinition.DataItemId;
if
(itemId == Guid.Empty)
return
default
(T);
//get the news item by its id
return
(T)manager.GetItem(
typeof
(T), itemId);
item = GetContentItemFromControl<NewsItem>(control, NewsManager.GetManager());
public
ContentItem GetContentBlockFromControl(ObjectData control)
//get the id of the content item
var property = control.Properties.FirstOrDefault(x => x.Name ==
"SharedContentID"
);
if
(property ==
null
)
return
null
;
var itemId = Guid.Parse(property.Value);
//get the content item
return
ContentManager.GetManager().GetContent(itemId);
(TrackedList<Guid>)item.GetValue(
"Tags"
)