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

Forums / Sitefinity SDK / How to filter News by custom classification?

How to filter News by custom classification?

13 posts, 0 answered
  1. Sandeep
    Sandeep avatar
    5 posts
    Registered:
    10 Nov 2010
    11 Mar 2011
    Link to this post
    Is there a way to filter News by custom classification?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    11 Mar 2011
    Link to this post
    Hi Sandeep,

    There is a FilterExpression property in the lists definition of the control which you can use to filter the control by taxon, but you should know its ID.

    Taxonomy.Contains("{A9F2C13A-9472-4BF6-AB3D-19B2EF05B043}")

    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. Sandeep
    Sandeep avatar
    5 posts
    Registered:
    10 Nov 2010
    17 Mar 2011
    Link to this post
    Adding the code you posted to the filter expression of the NewsFrontEndList control causes an Object Reference error. Is there a special syntax for this type of filter?
    Filter Expression Used:
    Visible = true AND Status = Live AND Taxonomy.Contains("{3707be29-9620-4aab-b7a7-6457a6dc3269}") 
    Received Error
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
     
    Source Error:
     
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
     
    Stack Trace:
     
    [NullReferenceException: Object reference not set to an instance of an object.]
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.FindMethod(Type type, String methodName, Boolean staticAccess, Expression[] args, MethodBase& method) +58
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseMemberAccess(Type type, Expression instance) +665
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParsePrimary() +51
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseUnary() +114
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseMultiplicative() +54
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseAdditive() +59
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseComparison() +54
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseLogicalAnd() +54
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseLogicalOr() +54
       Telerik.Sitefinity.Data.Linq.Dynamic.ExpressionParser.ParseExpression() +27
       Telerik.Sitefinity.Data.Linq.Dynamic.DynamicExpression.ParseLambda(ParameterExpression[] parameters, Type resultType, String expression, Object[] values) +65


    Thanks
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    18 Mar 2011
    Link to this post
    Hello Sandeep,

    What is the name of your custom taxonomy - "Taxonomy" or another one?

    Regards,
    Ivan Dimitrov
    the Telerik team
  5. Sandeep
    Sandeep avatar
    5 posts
    Registered:
    10 Nov 2010
    18 Mar 2011
    Link to this post
    Ivan,

    My custom classification's name is not Taxonomy, it is 'PressRoomClassification'.
    I also tried the following filter expression and still getting the same error.
    Visible = true AND Status = Live AND PressRoomClassification.Contains("{A9F2C13A-9472-4BF6-AB3D-19B2EF05B043}")

    Thanks
  6. Drew Greenwell
    Drew Greenwell avatar
    66 posts
    Registered:
    11 Feb 2010
    23 Mar 2011
    Link to this post
    Hey guys, this question has been sitting for a few days now. The project Sandeep is working on is coming close to deadline. Is there a functional way to filter by a custom Taxonomy?
  7. Bonny
    Bonny avatar
    58 posts
    Registered:
    13 Sep 2016
    28 Mar 2011
    Link to this post
    Hi Sandeep,
     
    We currently don't support filtering for custom classifications.

    However here is the way you can do it:

    This gets a list of news items with the tag "one" that were live:

    private TaxonomyPropertyDescriptor GetPropertyDescriptor(Type itemType, ITaxon taxon)
     {
         return TaxonomyManager.GetPropertyDescriptor(itemType, taxon);
     }
     
     private IEnumerable GetItems(ITaxon taxon, ContentDataProviderBase contentProvider, Type itemType)
     {
         TaxonomyPropertyDescriptor prop = GetPropertyDescriptor(itemType, taxon);
         int? totalCount = 0;
         var items = contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, itemType, string.Empty, string.Empty, 0, 100, ref totalCount);
         return items;
     }
     
     public void Button1_Click(object sender, EventArgs e)
     {
         List<NewsItem> niL = new List<NewsItem>();
         TaxonomyManager taxManager = TaxonomyManager.GetManager();
     
         //Get the tag called "One"
         var taxon = taxManager.GetTaxa<FlatTaxon>().Where(t => t.Name == "One").Single();
     
         //I want the news items
         string itemTypeName = "Telerik.Sitefinity.News.Model.NewsItem";
         Type itemType = TypeResolutionService.ResolveType(itemTypeName);
         var manager = ManagerBase.GetMappedManager(itemType, "");
         ContentDataProviderBase contentProvider = manager.Provider as ContentDataProviderBase;
          
         var v = GetItems(taxon, contentProvider, itemType);
         //Load into strongly typed list
         foreach (NewsItem n in v)
         {
             niL.Add(n);
         }
         Response.Write(niL.Where(a => a.Status == ContentLifecycleStatus.Live).First());
     }


    Kind regards,
    the Telerik team
  8. John Tolar
    John Tolar avatar
    87 posts
    Registered:
    07 May 2003
    08 Jun 2011
    Link to this post
    To anyone that comes across this in the future, here is a function to handle getting items by custom classification.

    private static IList<T> GetItemsByTaxonomy<T>(ITaxon taxon, ContentDataProviderBase contentProvider)
            {
                var prop = TaxonomyManager.GetPropertyDescriptor(typeof (T), taxon);
                    //GetPropertyDescriptor(typeof(T), taxon);
                int? totalCount = 0;
                return
                    contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, typeof (T),
                                                    string.Empty, string.Empty, 0, 100, ref totalCount).Cast<T>().ToList();
            }


    Usage:

    var taxon = TaxonomyManager.GetManager().GetTaxa<FlatTaxon>().Where(t => t.Name == "CarrierHostedCall").SingleOrDefault();
     
    if (taxon != null)
        {
            var evnts = GetItemsByTaxonomy<Event>(taxon, EventsManager.GetManager().Provider);
            foreach(Event evnt in events)
                do something....
        }




  9. Webteam
    Webteam avatar
    28 posts
    Registered:
    30 Nov 2009
    11 Jan 2012
    Link to this post
    I know this forum is a little old but for the sake of someone coming across this down the road I find that filtering new/events etc... by custom classification using filterexpression in the view works fine for me, such as
    Visible = true AND Status = Live AND CustomClassification.Contains("{A9F2C13A-9472-4BF6-AB3D-19B2EF05B043}")

    Is this something that was fixed in one of the releases because I see below you say it isn't supported.
  10. Bonny
    Bonny avatar
    58 posts
    Registered:
    13 Sep 2016
    16 Jan 2012
    Link to this post
    Hello,

    The filter for custom classifications was not working and this was a known issue. A lot of fixes were made lately and this is the reason for your successful filtering. However, if you find any problems with it, feel free to contact us.

    Kind regards,
    Bonny
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  11. Dan
    Dan avatar
    1 posts
    Registered:
    24 Aug 2010
    07 Mar 2012
    Link to this post
    What is now (SiteFinity v4.4) the recommended way to filter RealEstate items by complex criteria including custom classification?
  12. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    13 Mar 2012
    Link to this post
    Hi Dan,
    Not sure which is the recommended method of filtering custom classifications, but in 4.4 only this worked for me:

    var filterExpression = DefinitionsHelper.GetFilterExpression(masterDefinition);
    if (!String.IsNullOrEmpty(LocationFilter()))
        filterExpression += " AND Locations.Contains(\"{" + LocationFilter() + "}\")";

    where LocationFilter was returning a guid as a string. i haven't upgraded my current project to 5 yet so maybe it is working there.
    Thanks
    Faiz
  13. ming
    ming avatar
    8 posts
    Registered:
    26 Dec 2008
    19 Feb 2013 in reply to Bonny
    Link to this post

    I am on Sitefintiy 5.0.1, and when I tried

    Visible = true AND Status = Live AND Taxonomy.Contains("{d004e36c-a186-47bd-a321-eb834f100162}","{9de21464-b26e-4945-8be4-fc8f2a0ed264}")

    OR

    Visible = true AND Status = Live AND CustomClassification.Contains("{d004e36c-a186-47bd-a321-eb834f100162}","{9de21464-b26e-4945-8be4-fc8f2a0ed264}")

    Both were accepted; solution recompiled, but it will not be usable when tried to drag to be used.

                 Ming

13 posts, 0 answered