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

Forums / Developing with Sitefinity / News Based On Role?

News Based On Role?

8 posts, 0 answered
  1. Aaron
    Aaron avatar
    39 posts
    Registered:
    16 Apr 2009
    06 Jul 2009
    Link to this post
    I would like to have a news control that would filter by category the news displayed based on the users role. 
    i.e. 
    if (user role = "group1")
           NewsViewControl Filterexpression = "Category IN ("group1")"
    else if (user role = "group2")
          NewsViewControl Filterexpression = "Category IN ("group2")"
    else if (user role = "group3")
          NewsViewControl Filterexpression = "Category IN ("group3")"
    else
         NewsViewControl Filterexpression = ""

    end if




    I have been searching for two days and can't figure this one out, any ideas?


  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    06 Jul 2009
    Link to this post
    I'm not sure if this is what you have in mind, but the way I have done this is to manually bind to a repeater using the News API:

    var mgr = new NewsManager("News");  
     
    // initialize search filters  
    var filters = new IMetaSearchInfo[3];  
    filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);  
    filters[1] = new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", DateTime.Now, SearchCondition.LessOrEqual);  
     
    if (Page.User.IsInRole("A"))  
        filters[2] = new MetaSearchInfo(MetaValueTypes.ShortText, "Category""A");  
    if (Page.User.IsInRole("B"))  
        filters[2] = new MetaSearchInfo(MetaValueTypes.ShortText, "Category""B");  
     
    // ...  
              
    //retrieve the news items  
    var newsList = mgr.Content.GetContent("Publication_Date DESC", filters); 

    one thing about this method is that it retrieves ALL news items, including drafts. to exclude them use the following
    // create list to filter out draft items  
    var published = new List<IContent>();  
    foreach (IContent item in newsList)  
        if (item.Status == ContentStatus.Published) published.Add(item); 

    then just bind your repeater to the published list:
    NewsRepeater.DataSource = newsList;  
    NewsRepeater.DataBind(); 

    Your repeater would bind to the IContent properties:
    <asp:Repeater ID="navRepeater" runat="server" EnableViewState="false">  
        <ItemTemplate> 
            <href="<%# Eval("Url", "/news/default{0}.aspx") %>title="<%# Eval("Title") %>"><%# Eval("Title") %></a>  
        </ItemTemplate> 
    </asp:Repeater> 

    this is the way I've done it on my sites, but I've never made direct use of the filterexpression. please excuse me if this is not what you had in mind, but I hope this was helpful!
  3. Aaron
    Aaron avatar
    39 posts
    Registered:
    16 Apr 2009
    06 Jul 2009
    Link to this post
    Hey thanks for responding.

    It looks like your method could work, I'm going to try it out, but I'd still like to hear if anyone else has done this a different way. 

    I would ideally like to display the news in a rad grid with title linked to article.
  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    06 Jul 2009
    Link to this post
    it would be pretty simple to replace the repeater with a rad grid, though you might have to play with the columns to get them to bind correctly. Since you're binding manually you lose the design time support, however the dataitems are simply the public properties of the IContent item, and Url and Title are both part, so you could bind directly to these in the grid columns.

    let me know how it works out, if I can be of any help let me know!
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    06 Jul 2009
    Link to this post
    Hello,

    The categories does not have anything to do with the logged users. Another option would be creating a filter after the user has been authenticated. You can get the currently logged user using UserManager.IsUserInRole. Then you need to create a filter depending on a user by one of the metakeys.
    SelArom's example shows this approach very well. Instead of using  MetaSearchInfo filter you can use ContentFilterBuilder.

    ContentFilterBuilder filterBuilder = new ContentFilterBuilder(EventsView1); 
    filterBuilder.AddFilter(new ContentFilterStatement(combo1.SelectedValue.ToString(), rightOperand, ContentFilter.Condition.In, ContentFilter.JoinType.And)); 



    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Aaron
    Aaron avatar
    39 posts
    Registered:
    16 Apr 2009
    06 Jul 2009
    Link to this post
    Ok thank you both for you great help. I think I was making it too complicated.  This was all I needed.

        protected void Page_Load(object sender, EventArgs e)  
        {  
             
            NewsView newsViewControl = new NewsView(); 
            if (Telerik.Security .UserManager .IsCurrentUserInRole("Administrator")) 
                newsViewControl.FilterExpression = ""
            else if( Telerik.Security .UserManager .IsCurrentUserInRole("DCC")) 
                newsViewControl.FilterExpression = "Category IN (DCC-News)"
            else if( Telerik.Security .UserManager .IsCurrentUserInRole("TA Referrer")) 
                newsViewControl.FilterExpression = "Category IN (TA Referrer-News)"
             
            this.Controls.Add(newsViewControl); 

  7. Laura
    Laura avatar
    311 posts
    Registered:
    25 Feb 2008
    19 Oct 2010
    Link to this post
    If I am using the template News/ListPageMaster.ascx.  where should I put the code that Aaron posted? Can I add code behind to the template?
    Thank you.
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Oct 2010
    Link to this post
    Hi Laura,

    He is using a custom control and crates new instance of NewsView inside PageLoad, the he sets the properties of the control and add it to the controls collection.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
8 posts, 0 answered