SharePoint Rating Enable and Disable programmatically
The following code snippet can be used to SharePoint rating enable and disable programmatically.
private static void EnableRatingSetting(SPList list)
{
SPFieldCollection allfields = list.Fields;
SPFieldCollection availFields = list.ParentWeb.AvailableFields;
if (!allfields.Contains(FieldId.AverageRatings))
{
SPField field = availFields[FieldId.AverageRatings];
list.Fields.AddFieldAsXml(field.SchemaXmlWithResourceTokens, true, SPAddFieldOptions.AddFieldToDefaultView | SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
}
if (!allfields.Contains(FieldId.RatingsCount) && availFields.Contains(FieldId.RatingsCount))
{
SPField field2 = availFields[FieldId.RatingsCount];
list.Fields.AddFieldAsXml(field2.SchemaXmlWithResourceTokens, false, SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
}
list.Update();
}
private static void DisableRatingSetting(SPList list)
{
SPField field1 = GetField(FieldId.AverageRatings, list.Fields);
if (field1 != null)
{
list.Fields.Delete(field1.InternalName);
}
SPField field2 = GetField(FieldId.RatingsCount, list.Fields);
if (field2 != null)
{
list.Fields.Delete(field2.InternalName);
}
list.Update();
}
private static SPField GetField(Guid id, SPFieldCollection fieldColl)
{
return fieldColl[id];
}
No comments:
Post a Comment