How to query Images by checking the Tag using FluentAPI

Posted by Community Admin on 05-Aug-2018 16:27

How to query Images by checking the Tag using FluentAPI

All Replies

Posted by Community Admin on 28-Feb-2011 00:00

Hi,

I have the following Linq query to gets images in an image library called "MyImages". I have Tagged the images in the library with certain keywords. How can I extend the below query to check the Tag field also?

Let's say i need to find images which are Tagged as "2011"

App.WorkWith()
                .Album("MyImages")
                .Images()
                .Where(i.Status == ContentLifecycleStatus.Live)
                .Get();


Thanks!
Duneel

Posted by Community Admin on 28-Feb-2011 00:00

Hi Duneel,

Please take a look at this post.

Kind regards,
Ivan Dimitrov
the Telerik team

Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!

Posted by Community Admin on 28-Feb-2011 00:00

Hi Ivan,

There is no property called ".TagsText" in my Image item where I can check. How should I do that?

Thanks,
Duneel

Posted by Community Admin on 28-Feb-2011 00:00

Hello Duneel,

The exact post I sent you there is no TagsText.

Posted on Feb 1, 2011 (permalink)

Hello Dan,

Ok, I see what the problem is. Actually you have to use the ID of the item


var taxID= new Guid("61FF245B-23C7-453F-8DE6-2FF8EC46E125");

var items = App.WorkWith()
              .ContentItems()
              .Where(n => ((IList<Guid>)n.GetValue("Tags")).Contains(taxID))
              .Get();


Regards,
Ivan Dimitrov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!

Posted by Community Admin on 28-Feb-2011 00:00

Hi Ivan,

This takes the GUID of the tag. But I want to check against the exact Tag text. How shall I do that?

Thanks,
Duneel

Posted by Community Admin on 28-Feb-2011 00:00

Hi Ivan,

another issue. I'm receiving following exception when running the below query. Any idea why? The albem title is specified by a field called ImageLibraryName in my Widget and it contains the correct value.
Query:

Album albumToLocate = App.WorkWith()
                           .Albums()
                           .Where(a => a.Title == this.ImageLibraryName)
                           .Get().FirstOrDefault();

Exception:
Telerik.OpenAccess.Exceptions.QueryException was unhandled by user code
  Message=line 1:132: unexpected token: ["",<1>,line=1,col=132]
Original Query: DEFINE EXTENT extnt FOR Telerik.Sitefinity.Libraries.Model.Album; SELECT * FROM extnt AS t1  WHERE t1.appName =  $1 AND t1.Title_ =
  Source=Telerik.Sitefinity
  CanRetry=true
  StackTrace:
       at Telerik.Sitefinity.Data.Linq.Oql.OqlQueryProvider`2.ExecuteKnownType[TResult](IObjectScope scope, String queryText, Boolean isEnumerable, Int32 skip, Int32 take, IList parameters, ElementOperator op)
       at Telerik.Sitefinity.Data.Linq.Oql.OqlQueryProvider`2.Execute[TResult](Expression expression)
       at Telerik.Sitefinity.Data.Linq.QueryProvider`2.System.Linq.IQueryProvider.Execute[T](Expression expression)
       at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
       at PlayerOfTheWeekDetails.Page_Load(Object sender, EventArgs e) in c:\TFS_Working\NHLPA\www.nhlpa.com_4.0\www.nhlpa.com_4.0\App_Data\CustomControls\PlayerOfTheWeekDetails.ascx.cs:line 54
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:


Thanks,
Duneel

Posted by Community Admin on 01-Mar-2011 00:00

Hi Duneel,

You could use this code to filter on tags:

/// <summary>
        /// Bind data to repeater
        /// </summary>
        private void BindData()
        
            try
            
                // Create a TaxonomyManager
                TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
 
                // Get the tag called 'Visual'
                var taxon = taxonomyManager.GetTaxa<FlatTaxon>().Where(t => t.Name == "Visual").Single();
 
                string itemTypeName = "Telerik.Sitefinity.Libraries.Model.Image";
                Type itemType = Telerik.Sitefinity.Utilities.TypeConverters.TypeResolutionService.ResolveType(itemTypeName);
                var manager = Telerik.Sitefinity.Data.ManagerBase.GetMappedManager(itemType, "");
 
                ContentDataProviderBase contentProvider = manager.Provider as ContentDataProviderBase;
                IEnumerable items = GetItems(taxon, contentProvider, itemType);
 
                this.rptRotator.DataSource = items;
                this.rptRotator.DataBind();
            
            catch (Exception ex)
            
                throw ex;
            
        

You also need these functions to get it to work:
/// <summary>
        /// Get PropertyDescriptor
        /// </summary>
        /// <param name="itemType"></param>
        /// <param name="taxon"></param>
        /// <returns></returns>
        private TaxonomyPropertyDescriptor GetPropertyDescriptor(Type itemType, ITaxon taxon)
        
            return TaxonomyManager.GetPropertyDescriptor(itemType, taxon);
        
 
        /// <summary>
        /// Get Items based on taxon
        /// </summary>
        /// <param name="taxon"></param>
        /// <param name="contentProvider"></param>
        /// <param name="itemType"></param>
        /// <returns></returns>
        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;
        

Regards,
Daniel

This thread is closed