Getting all Tags from all items on Page where my Widget is on - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Getting all Tags from all items on Page where my Widget is on

  • Getting all Tags from all items on Page where my Widget is on
  • 97764f1e-37c1-6deb-a958-ff0000446526_untitled.png

    Hi guys,

    Here is what I'm trying to build: http://imgur.com/l7dSYgE

    I'm struggling with the following:
    Is it possible to find all current widgets on a page where my widget is on and get their associated tags? And if so, how?

    I think I'll be able to manage to put the resulting content (related news, etc) together myself.

  • This may not be the cleanest code you've seen and I'm sure there could be some improvement to it but I managed to figure it out myself.

    public class SitefinityHelper
        
            public static List<string> GetTagsForSitefinityObject(IDataItem dataItem)
            
                if (dataItem != null)
                
                    var propDesc = OrganizerBase.GetProperty(dataItem.GetType(), "Tags") as TaxonomyPropertyDescriptor;
                    if (propDesc != null)
                    
                        var taxonIds = propDesc.GetValue(dataItem) as IList<Guid>;
                        var taxonomyManager = TaxonomyManager.GetManager();
                        var taxa = new List<string>();
                        foreach (var taxonId in taxonIds)
                        
                            taxa.Add(taxonomyManager.GetTaxon(taxonId).Title);
                        
                        return taxa;
                    
                
                return new List<string>();
            
     
        

    protected IList<object> GetAllTagsFromAllWidgetsOnThisPage()
            
                using (var fluent = App.WorkWith())
                
                    SiteMapNode currentNode = SiteMap.CurrentNode;
                    if (currentNode != null)
                    
                        var pm = PageManager.GetManager();
                        var controls = fluent.Page(new Guid(currentNode.Key)).AsStandardPage().CheckOut().Get().Controls;
                        string controlInfo = string.Empty;
                         
                        var tags = new List<string>();
     
                        if (controls != null || controls.Count > 0)
                        
                            foreach (PageDraftControl pageControl in controls)
                            
                                switch (pageControl.ObjectType.ToString())
                                
                                    case "Telerik.Sitefinity.Web.UI.LayoutControl, Telerik.Sitefinity":
                                    case "~/Custom/ControlWidgets/RelatedItemsWidget.ascx":
                                        break; //ignore these
                                    case "Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock":
                                        // get tags from contentblock
                                        tags.AddRange(getTagsFromContentBlock(pageControl));
                                        break;
                                    case "Telerik.Sitefinity.Modules.News.Web.UI.NewsView":
                                        // get tags from newsview
                                        tags.AddRange(getTagsFromNewsView(pageControl));
                                        break;
                                    case "Telerik.Sitefinity.Modules.Blogs.Web.UI.BlogPostView":
                                        // get tags from blogpostview
                                        tags.AddRange(getTagsFromBlogPostView(pageControl));
                                        break;
                                    case "Telerik.Sitefinity.Modules.Events.Web.UI.EventsView":
                                        // get tags from eventsview
                                        tags.AddRange(getTagsFromEventsView(pageControl));
                                        break;
                                    case "Telerik.Sitefinity.Modules.Libraries.Web.UI.Videos.MediaPlayerControl, Telerik.Sitefinity":
                                        // get tags from mediaplayercontrol
                                        tags.AddRange(getTagsFromMediaPlayerControl(pageControl));
                                        break;
                                    case "Telerik.Sitefinity.Modules.Libraries.Web.UI.Documents.DocumentLink":
                                        // get tags from documentlink (libraryitem)
                                        tags.AddRange(getTagsFromDocumentLink(pageControl));
                                        break;
                                
     
                                controlInfo += string.Format("0<br/ >", pageControl.ObjectType);
                            
                        
                        LabelExistingTags.Text += controlInfo + "<br/> All The TAGS:<br/>" + string.Join(", ", tags.ToArray());
                    
                
                return null;
            
     
            private List<string> getTagsFromDocumentLink(PageDraftControl pageControl)
            
                var ctl = PageManager.GetManager().LoadControl(pageControl) as DocumentLink;
                if (ctl == null) return null;
                var dataItems = App.WorkWith().Documents().Where(d => d.Id == ctl.DocumentId);
                var response = new List<string>();
                int c;
                dataItems.Count(out c);
                if (c > 0)
                    dataItems.ForEach(x => response.AddRange(SitefinityHelper.GetTagsForSitefinityObject(x)));
                return response;
            
            private List<string> getTagsFromMediaPlayerControl(PageDraftControl pageControl)
            
                var ctl = PageManager.GetManager().LoadControl(pageControl) as MediaPlayerControl;
                if (ctl == null) return null;
                var dataItems = App.WorkWith().Videos().Where(d => d.Id == ctl.MediaContentId);
                var response = new List<string>();
                int c;
                dataItems.Count(out c);
                if (c > 0)
                    dataItems.ForEach(x => response.AddRange(SitefinityHelper.GetTagsForSitefinityObject(x)));
                return response;
            
            private List<string> getTagsFromEventsView(PageDraftControl pageControl)
            
                var ctl = PageManager.GetManager().LoadControl(pageControl) as EventsView;
                if (ctl == null) return null;
                var id = (ctl.ControlDefinition.Views[1] as EventsViewDetailDefinition).DataItemId;
                var dataItems = App.WorkWith().Events().Where(d => d.Id == id);
                var response = new List<string>();
                int c;
                dataItems.Count(out c);
                if (c > 0)
                    dataItems.ForEach(x => response.AddRange(SitefinityHelper.GetTagsForSitefinityObject(x)));
                return response;
            
            private List<string> getTagsFromBlogPostView(PageDraftControl pageControl)
            
                var ctl = PageManager.GetManager().LoadControl(pageControl) as BlogPostView;
                if (ctl == null) return null;
                var id = (ctl.ControlDefinition.Views[1] as Telerik.Sitefinity.Web.UI.ContentUI.ContentViewDetailDefinition).DataItemId;
                var dataItems = App.WorkWith().BlogPosts().Where(d => d.Id == id);
                var response = new List<string>();
                int c;
                dataItems.Count(out c);
                if(c > 0)
                    dataItems.ForEach(x => response.AddRange(SitefinityHelper.GetTagsForSitefinityObject(x)));
                return response;
            
            private List<string> getTagsFromNewsView(PageDraftControl pageControl)
            
                var ctl = PageManager.GetManager().LoadControl(pageControl) as NewsView;
                if (ctl == null) return null;
                var id = (ctl.ControlDefinition.Views[1] as Telerik.Sitefinity.Web.UI.ContentUI.ContentViewDetailDefinition).DataItemId;
                var dataItems = App.WorkWith().NewsItems().Where(d => d.Id == id);
                var response = new List<string>();
                int c = 0;
                dataItems.Count(out c);
                if(c > 0)
                    dataItems.ForEach(x => response.AddRange(SitefinityHelper.GetTagsForSitefinityObject(x)));
                return response;
            
            private List<string> getTagsFromContentBlock(PageDraftControl pageControl)
            
                var ctl = PageManager.GetManager().LoadControl(pageControl) as ContentBlock;
                if (ctl == null) return null;
                var dataItems = App.WorkWith().ContentItems().Where(d => d.Id == ctl.SharedContentID);
                var response = new List<string>();
                int c=0;
                dataItems.Count(out c);
                if (c > 0)
                    dataItems.ForEach(x => response.AddRange(SitefinityHelper.GetTagsForSitefinityObject(x)));
                return response;