Question

I have been using the method described here by attempting to use reflection to turn on the Rating Settings of a list. However, I get the following error:

Exception Message: exception has been thrown by the target of an invocation

Stacktrace Message: Value cannot be null.Parameter name: serviceContext

Here is my attempt:

//Create the list
Guid id = web.Lists.Add("Board Name", "Board Desc", SPListTemplateType.DiscussionBoard);
web.Update();

//Retrieve the list I just created
SPList discBoardList = web.Lists[id];

//Attempt to turn on the Rating Settings for the newly created list
Type type = typeof(Microsoft.SharePoint.Portal.RatingsSettingsPage);
MethodInfo mi = type.GetMethod("EnableRatings", BindingFlags.Static | BindingFlags.NonPublic);
mi.Invoke(null, new object[] { discBoardList, true }); //EXCEPTION THROWN

How do I resolve this issue? Or more importantly is there an easier/better way to turn on Rating Settings for a SharePoint list programmatically?

Was it helpful?

Solution 2

As a workaround I just grab the default "Discussions List" that is created when creating a Community site. When I start adding the list items all numbers in What's Happening come out correctly.

OTHER TIPS

are you creating your list programmatically or it is generated using the OOTB options ?

if you are creating the list programmatically then you can use the below method to enable ratings in your list schema.

Add this to your default view (or any view you want) in Definition Schema of the List

<ViewFields>
          <FieldRef Name="AverageRating" />
          <FieldRef Name="RatingCount" />
  </ViewFields>

Edit

Follow this link

You will have to create a content type containing the references of the ratings field and will have to add that content type in the list schema.

I am not very much sure about it but you can also try This Link as well and see if it solves your problem

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top