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

Forums / Developing with Sitefinity / Fluent Query: News Items by Category

Fluent Query: News Items by Category

5 posts, 0 answered
  1. SVA Webmaster
    SVA Webmaster avatar
    85 posts
    Registered:
    18 Jul 2007
    14 Jun 2011
    Link to this post
    I am building a simple news scroller in a usercontrol and trying to figure out how to pull only news items assigned to a specific category. I have everything working fine pulling all live news items (App.WorkWith().NewsItems().Where(n => n.Status = ContentLifecycleStatus.Live), but can't find a way to tack on a filter by category. I've spent an entire day trying to figure this out. Any assistance for a Sitefinity/Fluent/Linq newb is appreciated! 
  2. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    15 Jun 2011
    Link to this post
    Yeah it's weird...not very fluent ATM :)

    Please vote on the issue I have on PITS for this, :)

    Here's how to do it though

    if (!this.IsDesignMode() && !this.IsBackend()) {
                    if (!HttpContext.Current.Request.Url.ToString().Contains("PagesApprovalWorkflow")) {
                        if (this.CategoryID != String.Empty) {
                            var rotatorCategory = new Guid(this.CategoryID);
                            var content = App.WorkWith()
                            .NewsItems()
                            .Where(
                                ci => ci.GetValue<IList<Guid>>("Category").Contains(rotatorCategory) &&
                                      ci.Status ==
                                      Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).Get();
     
                            Rotator1.DataSource = content;
                            Rotator1.DataBind();
                        }
                        else {
                            this.Controls.Add(
                                new LiteralControl(
                                    "<div color='red'>You need to go into Edit mode to specify the CategoryID which contains the Rotator items</div>"));
                        }
                    }
                }

    So this control has a public property called CategoryID which the person can set, the checks at the top might be useless to you, but in this context it throws an exception if you're trying to bind in design mode.

  3. SVA Webmaster
    SVA Webmaster avatar
    85 posts
    Registered:
    18 Jul 2007
    15 Jun 2011
    Link to this post
    Thanks for the reply, Steve. You'll have to forgive me though as I am a complete newbie to Sitefinity, Fluent, Linq and c#. My background is in ASP.Net VB webforms based development.  So where do I get the CategoryID mentioned?? I have no idea what my CategoryID's are and would prefer to refer to them by name (string). Is this even possible? 

    Thanks again for your help!
  4. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    15 Jun 2011
    Link to this post
    Oh sorry :)  It's a public property on the control

    private string _categoryID = String.Empty;
    public string CategoryID{
           get{return _categoryID;}
           set{_categoryID = value;}
    }

    ...OR

    If you know it will never change, just hardcode the Guid in with a string.  You can find the GUIDs for the Taxons in the DB, I dont have a SQL DB right now so I'm not sure where, but it's one of the "Taxa" tables (if not THE taxa table)
  5. Matthew
    Matthew avatar
    33 posts
    Registered:
    15 Dec 2008
    26 Jun 2011
    Link to this post
    Excellent post, you saved me a ton of time trying to figure this out. I agree, not very FLUENT but works.
5 posts, 0 answered