Fluent Query: News Items by Category

Posted by Community Admin on 04-Aug-2018 22:01

Fluent Query: News Items by Category

All Replies

Posted by Community Admin on 14-Jun-2011 00:00
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! 
Posted by Community Admin on 15-Jun-2011 00:00
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.

Posted by Community Admin on 15-Jun-2011 00:00
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!
Posted by Community Admin on 15-Jun-2011 00:00
Oh sorry :)  It's a public property on the control

private string _categoryID = String.Empty;
public string CategoryID
       getreturn _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)
Posted by Community Admin on 26-Jun-2011 00:00
Excellent post, you saved me a ton of time trying to figure this out. I agree, not very FLUENT but works.
This thread is closed