1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Get tags from PageControls

Get tags from PageControls

7 posts, 0 answered
  1. Erik
    Erik avatar
    62 posts
    Registered:
    02 May 2011
    04 Apr 2012
    Link to this post
    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?
  2. Erik
    Erik avatar
    62 posts
    Registered:
    02 May 2011
    05 Apr 2012
    Link to this post
    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.
  3. Erik
    Erik avatar
    62 posts
    Registered:
    02 May 2011
    10 Apr 2012
    Link to this post
    Wow, I've wasted a lot of time trying to figure this out. Anyone got an idea?
  4. Erik
    Erik avatar
    62 posts
    Registered:
    02 May 2011
    10 Apr 2012
    Link to this post
    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"
  5. Erik
    Erik avatar
    62 posts
    Registered:
    02 May 2011
    10 Apr 2012
    Link to this post
    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.
  6. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    10 Apr 2012
    Link to this post
    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))

    so that you only get the control you need.

    For a sample of this in action you can set a breakpoint on and observe, take a look at the Sitemap Module example which is available in the Sitefinity SDK. the configuration class uses this method to find a specific control type from all the pages in the site.

    I hope this is helpful, let me know if you need more help on this topic!
  7. Erik
    Erik avatar
    62 posts
    Registered:
    02 May 2011
    10 Apr 2012
    Link to this post
    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);
    }

    Here is an example of how I call this function (where control is a PageControl):

    item = GetContentItemFromControl<NewsItem>(control, NewsManager.GetManager());


    For others (Documents, Videos, Images, ContentBlocks) I have to get the data item id(s) from the PageControl properties like follows (this example is for ContentBlocks):

    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);
    }

    You have to change the Property Name and the Manager based on the Object Type. On the result of these calls (the data item) I call this code:
    (TrackedList<Guid>)item.GetValue("Tags")

    And that is how the magic happens.
7 posts, 0 answered