Query library items by tag - General Discussions - General Discussions - Progress Community
 General Discussions

Query library items by tag

  • Query library items by tag
  • Hi all,

    I want to query some items from an image library.
    I have this code:

    var images = App.WorkWith().Images().Where((w) => w.Album.Title == "Visuals" &&
                       w.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).Get();

    ... which works.

    Now I'd like to get only images that are having a specific Tag.
    I want to do something like this, but that isn't allowed, because an Library.Model.Image cannot be cast to a ContenItem.

    List<Telerik.Sitefinity.Libraries.Model.Image> filteredImages = new List<Telerik.Sitefinity.Libraries.Model.Image>();
    foreach (Telerik.Sitefinity.Libraries.Model.Image item in images)
       ContentItem i = item as ContentItem;
       if (i.TagsText.IndexOf("Visual") > -1)
          filteredImages.Add(item);

    An Image doesn't have the TagsText as property.
    How should I do this?

    Regards,
    Daniel
  • Hi Daniel,

    Please take a look at this post.

    Regards,
    Ivan Dimitrov
    the Telerik team
  • Hi Ivan,

    Thanks for the help.
    I don't know the GUID of the Taxon.

    I have to do it by name/title.

    Regards,
    Daniel
  • Hello Daniel,

    Use TaxonomyManager to get the taxon and then its ID.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
  • Thanks that worked.
  • Here is the completed code for convenience:

    public partial class Test : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        
            var taxonomyManager = TaxonomyManager.GetManager();
     
            var taxon = taxonomyManager.GetTaxa<FlatTaxon>().Where(t => t.Name == "tag1").Single();
     
            string itemTypeName = "Telerik.Sitefinity.Libraries.Model.Image";
            Type itemType = TypeResolutionService.ResolveType(itemTypeName);
            var manager = ManagerBase.GetMappedManager(itemType, "");
     
            ContentDataProviderBase contentProvider = manager.Provider as ContentDataProviderBase;
            var items = GetItems(taxon, contentProvider, itemType);
     
            foreach (Telerik.Sitefinity.Libraries.Model.Image item in items)
            
                Response.Write("ID: " + item.Id + "<br />");
                Response.Write("Url Name: " + item.UrlName + "<br />");
            
        
     
        private IEnumerable GetItems(ITaxon taxon, ContentDataProviderBase contentProvider, Type itemType)
        
            TaxonomyPropertyDescriptor prop = GetPropertyDescriptor(itemType, taxon);
            int? totalCount = 0;
            var filter = "Status = Master";
            var items = contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, itemType, filter, string.Empty, 0, 100, ref totalCount);
            return items;
        
     
        private TaxonomyPropertyDescriptor GetPropertyDescriptor(Type itemType, ITaxon taxon)
        
            return TaxonomyManager.GetPropertyDescriptor(itemType, taxon);
        

    Can't this be done with one line of Fluent API?
  • Hello Basem,

    There is a way through the fluent API, but again you should know the category/tag ID

    var categID = newGuid("");

    var content = App.WorkWith().NewsItems().Where(ci => ((IList<Guid>)ci.GetValue("Category")).Contains(categID));



    All the best,
    Ivan Dimitrov
    the Telerik team
  • Hello,

    Does there exist a best practice for getting items by more than one Taxon.  I'm filtering NewsItems by Category and Tag.  I've been able to get a collection filtered by Category and another collection filtered by Tag and taking the intersection to get the result I want.  

    Thanks!

    Michael
  • Hi ,

    You can add logical operands to the LINQ condition.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Thanks for the reply Ivan.

    I'm not sure how I could apply logical operands here.  Here's how I got this to "work".  I know there's got to be a better way.  

    #region News
            
                List<NewsItem> cItemsCatFilter = new List<NewsItem>();
                List<NewsItem> cItemsTagFilter = new List<NewsItem>();
                // Get Category Taxon
                HierarchicalTaxon taxonCat = _resourceCats.SingleOrDefault(rc => rc.Title.ToLower() == "news");
                 
                // Get Items
                string itemTypeName = "Telerik.Sitefinity.News.Model.NewsItem";
                Type itemType = TypeResolutionService.ResolveType(itemTypeName);
                IManager manager = ManagerBase.GetMappedManager(itemType, "");
                ContentDataProviderBase contentProvider = manager.Provider as ContentDataProviderBase;
                // Get collection filter by Category
                IEnumerable n1 = GetItems(taxonCat, contentProvider, itemType);
                cItemsCatFilter.AddRange(n1.OfType<NewsItem>());
                // Get collection filter by Tag
                IEnumerable n2 = GetItems(_tagTaxon, contentProvider, itemType);
                cItemsTagFilter.AddRange(n2.OfType<NewsItem>());
                // Filter by Tags and LifeCycle
                var items = cItemsCatFilter.Intersect<NewsItem>(cItemsTagFilter, new Utility.NewsItemComparer())
                                            .Where(ci => ci.Status != ContentLifecycleStatus.Live)
                                            .OrderBy(ci => ci.PublicationDate)
                                            .Take(5)
                                            .ToList();
     
                if (items.Count != 0)
                
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < items.Count; i++)
                    
                        // Create link with Title
                        sb.AppendLine("<p>" + items[i].Title + "</p>");
                        sb.AppendLine("<div style='height: 1px; background-color: #E2EBE9; margin: 5px 0;'></div>");
                    
                     //add More Link
                    sb.AppendLine("<a href='#' style='color: Black;'><p>More News »</p></a>");
                    ltlNews.Text = sb.ToString();
                
                else
                
                    phNews.Visible = false;
                
           
      #endregion

    Thanks!

    Michael



  • Hello,

    You need to use the query as shown below

    var categID = newGuid("generate a guid here");
    var categID1 = newGuid("generate a guid here");                
     
    var content = App.WorkWith().ContentItem().Where(ci => ((IList<Guid>)ci.GetValue("Category")).Contains(categID) && ((IList<Guid>)ci.GetValue("Category")).Contains(categID1));


    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Hello,

    This doesn't work for filtering by TAG (taxonomy [nme] = 'Tags').  The News object does not have a property TagsText.

    Thanks!

    Michael
  • Hi Michael,

    The objects contain only a refence to the ID of the tag. You have to first get the Tag's ID using the taxonomy manager, then use Ivan's approach for filtering.

    Kind regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • Hello,

    I stumbled upon the solution below after considering writing my own sprocs to get this data.  
    In using GetValue(this IDynamicFieldsContainer dataItem, string fieldName), I'm wondering where I can find all of the "fieldName" options.  

    Get News items by Tag:
    var items = App.WorkWith().NewsItems().Where(ci => ((IList<Guid>)ci.GetValue("Tags")).Contains(tagTaxonID))
                               .Get();

    Get News Items by Category:
    var items = App.WorkWith().NewsItems()
                                        .Where(ci => ((IList<Guid>)ci.GetValue("Category")).Contains(taxonCatID)).Get();

    My Example creating a list of NewsItems filltering by Category and Tag
    items = App.WorkWith().NewsItems()
                                        .Where(ci => ((IList<Guid>)ci.GetValue("Tags")).Contains(tagTaxonID))
                                        .Where(ci => ((IList<Guid>)ci.GetValue("Category")).Contains(taxonCatID))
                                        .Get()
                                        .Where(ci => ci.Status == ContentLifecycleStatus.Live)
                                        .OrderByDescending(ci => ci.PublicationDate)
                                        .ToList();

    Thanks!

    Michael