Sharepoint 2010 Programmatically adding custom List view with rating column on page does'nt display the rating stars

問題 Sharepoint 2010 Programmatically adding custom List view with rating column on page does'nt display the rating stars

  • 2012年5月2日 上午 05:03
     
     

    I am trying to create a new custom list view with rating column in it and adding that view on page  as a list view webpart.

    List view is getting added on the page but ratings stars are not visible on the page.

    When I am editing the webpart from UI and modify the view and saving that view without doing any changes it starts showing the ratings starts.

所有回覆

  • 2012年5月2日 上午 06:56
     
     
    Is it any custom column type you have created ? 

    Regards,
    Milan Chauhan

  • 2012年5月2日 上午 06:58
     
     
    Check this : http://www.sharemuch.com/2009/12/31/programaticaly-enable-rating-on-sharepoint-2010-lists/

    get2pallav
    Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • 2012年5月2日 上午 07:26
     
     
    No, I am using out of the box Rating column. I am just enabling the rating feature on list and adding list view webpart on my home page using the defualt view of the list which is having the rating field too.
  • 2012年5月2日 上午 07:39
     
      包含代碼

    Hey,

    This code will help work for you

     private void averagerating(SPWeb web)
            {
                web.AllowUnsafeUpdates = true;
                SPList list1 = web.Lists["List Name"];
     
                string averagerating = web.Fields.Add("Average Rating", SPFieldType.Calculated,false);
                SPFieldCalculated average = (SPFieldCalculated)web.Fields["Average Rating"];
                string abc = average.InternalName;
    
                average.Formula = @"=[Rating (0-5)]";
                average.OutputType = SPFieldType.Number;
                average.Update();
         
                
                SPField field1 = web.AvailableFields["Average Rating"];
                
                if (!list1.Fields.ContainsField("Average Rating"))
                {
                    list1.Fields.Add(field1);
                    
                }
                         
                list1.Update();
               
               SPView view1 = list1.DefaultView;
           
              
                view1.ViewFields.DeleteAll();
               
             
                view1.ViewFields.Add("Rating (0-5)");
                view1.ViewFields.Add("Average Rating");
                         
    
                web.Update();
                web.AllowUnsafeUpdates = false;
                
            
            }

    Let me know if you have any queries :)

    Mark as answer if it solves your problem :)


    "The Only Way To Get Smarter Is By Playing A Smarter Opponent"

  • 2012年5月2日 上午 08:22
     
     
    @Ravi , I am able to add rating column and can see them in default view of list. My main issue is that when I  add a listview webpart programatically using default view of list on my home page , stars are not visible and user is not able to rate the content.