nHow to enable and disable SharePoint 2010 rating programmatically
n
nIf you have referred the details in the previous post on Working with the SharePoint 2010 Rating – Part 1 for theoretical explanation on what happens in the background, now let’s do something interesting in Visual Studio.
n
n
nIf you have referred the details in the previous post on Working with the SharePoint 2010 Rating – Part 1 for theoretical explanation on what happens in the background, now let’s do something interesting in Visual Studio.
n
nWhen you visit the list settings page and click on rating settings link , you gets redirected the application page called as “RatingSettings.aspx” with the query string as the list GUID and on this page you can decide the setting about enabling and disabling ratings on that list.
n
nI didn’t see any other way to achieve this with programmatic approach in the server object model in SPList class and so for curiosity I opened up the reflector to see what Microsoft has done.
n
nAnd so here are the ways to programmatically enable the ratings on list
n
n1. Using the reflection
n
ntypeof(Microsoft.SharePoint.Portal.RatingsSettingsPage).
n
nGetMethod(“EnableRatings”, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).
n
nInvoke(null, new object[] { _objectofSPList, true });
n
n
n
nThere is a method “EnableRatings” in class “RatingSettingsPage” and method is internal and static. So I am just invoking the method here. This method takes two parameters.
n
na. object of SPList on which you need to enable the ratings
n
nb. a Boolean value (true/false) – keep true to prorogating rating on list items
n
nThere is one more method of this class, which allows us to disable the rating
n
ntypeof(Microsoft.SharePoint.Portal.RatingsSettingsPage).
n
nGetMethod(“DisableRatings”, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).
n
nInvoke(null, new object[] { _objectOfSPList });
n
n
n
nThis method accepts a single parameter which is as the SPList and this is the target list.
n
n
n2. Using the actual code
n
n
n
n Rating can also be enabled without using the reflection, but in this case you write much more code than in previous method.
n
n
nThis method is used for enabling the rating settings on a list. You can see that there are two fields which are being add in the list.
n
n private void EnableRatingSetting(SPList _list)
n
n {
n
n SPFieldCollection _allfields = _list.Fields;
n
n SPFieldCollection _availFields = _list.ParentWeb.AvailableFields;
n
n if (!_allfields.Contains(FieldId.AverageRatings))
n
n {
n
n SPField field = _availFields[FieldId.AverageRatings];
n
n_list.Fields.AddFieldAsXml(field.SchemaXmlWithResourceTokens, true, SPAddFieldOptions.AddFieldToDefaultView | SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
n
n }
n
n if (!_allfields.Contains(FieldId.RatingsCount) && _availFields.Contains(FieldId.RatingsCount))
n
n {
n
n SPField field2 = _availFields[FieldId.RatingsCount];
n
n _list.Fields.AddFieldAsXml(field2.SchemaXmlWithResourceTokens, false, SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
n
n }
n
n _list.Update();
n
n
n
n }
n
n
n
nAnd this is the method to propagate the changes to the list items.
n
nThere is a method available with the SocialRatingManager class called as PropogateRating which takes care of this.
n
nprivate void PropogateChanges(SPList _list)
n
n{
n
nSocialRatingManager _socialRatingMgr = new SocialRatingManager(SPServiceContext.Current);
n
n string _baseUrl = _list.ParentWeb.Url;
n
n if (_baseUrl.EndsWith(“/”, StringComparison.OrdinalIgnoreCase))
n
n {
n
n _baseUrl = _baseUrl.TrimEnd(new char[] { ‘/’ });
n
n }
n
n foreach (SPListItem item in _list.Items)
n
n {
n
n string _itemUrl = item.Url;
n
n if (_itemUrl.StartsWith(“/”, StringComparison.OrdinalIgnoreCase))
n
n {
n
n _itemUrl = _itemUrl.TrimStart(new char[] { ‘/’ });
n
n }
n
n SPSecurity.RunWithElevatedPrivileges(delegate()
n
n {
n
n _socialRatingMgr.PropagateRating(new Uri(_baseUrl + “/” + _itemUrl));
n
n });
n
n }
n
n
n
n }
n
n
n
n private static SPField GetField(Guid id, SPFieldCollection fieldColl)
n
n {
n
n return fieldColl[id];
n
n }
n
n
n
nThis method is used to disable the rating on a list
n
nprivate void DisableRatingSetting(SPList _list)
n
n {
n
n SPField _field1 = GetField(FieldId.AverageRatings, _list.Fields);
n
n if (_field1 != null)
n
n {
n
n _list.Fields.Delete(_field1.InternalName);
n
n }
n
n SPField _field2 = GetField(FieldId.RatingsCount, _list.Fields);
n
n if (_field2 != null)
n
n {
n
n _list.Fields.Delete(_field2.InternalName);
n
n }
n
n _list.Update();
n
n
n
n }
n
n
n
n
n