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

Forums / Sitefinity SDK / How to query Images by checking the Tag using FluentAPI

How to query Images by checking the Tag using FluentAPI

7 posts, 1 answered
  1. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    28 Feb 2011
    Link to this post
    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
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    28 Feb 2011
    Link to this post
    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!
  3. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    28 Feb 2011
    Link to this post
    Hi Ivan,

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

    Thanks,
    Duneel
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    28 Feb 2011
    Link to this post
    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!
  5. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    28 Feb 2011
    Link to this post
    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
  6. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    28 Feb 2011
    Link to this post
    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
  7. Daniel Plomp
    Daniel Plomp avatar
    112 posts
    Registered:
    14 Jun 2012
    01 Mar 2011
    Link to this post
    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
    Answered
7 posts, 1 answered