From: https://sharepoint.stackexchange.com/questions/194197/how-to-manipulate-likeby-nooflikes-ratedby-averagerating-userrating-using-cs

Copy below:

 

Gone through the problem an got the relevant answer also made blog on it so visit the blog post

Some of the important aspects covered are described breifly as follows.

To rate an Item for the first time by a user the following fields should be modified with the respective field values:

  1. item["RatingCount"]:

    This field stores the number of users who have rated the item its value should be incremented.

  2. item["Ratings"]:

    A field which stores the user ratings in CSV string format should add the new users rating like.

    int
    NewRating = 4; //any desired number from 1 to 5.

    item["Ratings"] += NewRating + ",";

  3. item["AverageRating"]:

    This field stores the average of the ratings given by all the rated users. Simple weighted average calculation formulas can be implemented which is

    float
    OldAverage = item["AverageRating"] == null ? 0 : float.Parse(item["AverageRating"].ToString());

    int
    OldNumberOfRatings = item["RatingCount"] == null ? 0 : int.Parse(item["RatingCount"].ToString());

    int
    NewNumberOfRatings = OldNumberOfRatings + 1;

     

    float
    NewAverage = OldAverage + ( ( NewRating - OldAverage ) / NewNumberOfRatings);

    //Or.

    float
    NewAverage = ( ( OldAverage * OldNumberOfRatings ) + NewRating ) / NewNumberOfRatings;

     

    item["AverageRating"] = NewAverage;

  4. item["RatedBy"]:

    This field stores the user information in form of array of FieldUserValue which can be modified using following code

    FieldUserValue[] ratedUsers = item["RatedBy"] as
    FieldUserValue[];

    //where ratedUsers becomes an array of users who have already rated.

    List<FieldUserValue> newUsersRated = new
    List<FieldUserValue>();

    if (ratedUsers != null)

    foreach (FieldUserValue ratedUser in ratedUsers)

    newUsersRated.Add(ratedUser);

    newUsersRated.Add(FieldUserValue.FromUser(user.LoginName));

    item["RatedBy"] = newUsersRated;

To Rerate the item for a user who has already rated it before you need to first find the user index (say int userIndex) in the item["RatedBy"] field then change the following fields:

  1. item["Ratings"]:

    Change the user rating in this CSV string by use of userIndex.

    FieldUserValue[] ratedUsers = item["RatedBy"] as
    FieldUserValue[];

    for (int i = 0; i < ratedUsers.Length; i++)

    if (ratedUsers[i].LookupValue == user.Title)

    int userIndex = i;

    string[] userRatings = item["Ratings"].split(',');

    int
    OldRating = int.Parse(userRatings[userIndex]);

    int
    NewRating = 3; //any desired number from 1 to 5.

    userRatings[userIndex] = NewRating.ToString();

    string ratings = userRatings.Join(',', userRatings);

    item["Ratings"] = ratings;

  2. item["AverageRating"]:

    change the average value using formula

    float
    OldAverage = item["AverageRating"];

    int
    NumberOfRatings = item["RatingCount"];

    float
    NewAverage = OldAverage + ( ( NewRating - OldRating ) / NumberOfRatings );

    item["AverageRating"] = NewAverage;

SharePoint中Rating相关的字段。的更多相关文章

  1. sharepoint中的YesNo字段

    sharepoint中的YesNo字段实际上是一个Boolean字段,性格有点特别,如果IsShow是一个YesNo字段,使用caml查询的时候值为”1“(Yes)”0“(No),Item[IsSho ...

  2. Oracle中的自连接(self join)-当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自连接。

    http://blog.163.com/wkyuyang_001/blog/static/10802122820091751049479/ 当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自 ...

  3. 6月20日 Django中ORM介绍和字段、字段参数、相关操作

    一.Django中ORM介绍和字段及字段参数 二.Django ORM 常用字段和参数 三.Django ORM执行原生SQL.在Python脚本中调用Django环境.Django终端打印SQL语句 ...

  4. Sharepoint 2013列表视图和字段权限扩展插件(免费下载)!

    记得2014年春节期间,有博客园的网友通过QQ向我咨询Sharepoint 2013列表视图和字段权限扩展,因为之前他看到我博客介绍Sharepoint 2010列表视图和字段的权限控制扩展使用,问有 ...

  5. Sharepoint中有关文件夹的操作

    1.GetItemsWithUniquePermissions根据返回数量和是否返回文件夹获取唯一权限的列表项集合 对于SharePoint对象模型中SPList的GetItemsWithUnique ...

  6. SharePoint中使用Visio Service展示业务数据

    SharePoint中可以通过Visio Service可以在浏览器中查看Visio图,功能部署到系统中,一切安好. 而现实总是很折磨人,使用该功能后,相关使用者随后提出,Visio图能否与我的业务数 ...

  7. SharePoint中新创建的Web Application在浏览器中报404错误

    问题描述:在安装完成SharePoint 2010后,进入Central Administration,创建一个新的Web Application,可以正常创建,但访问时却返回404. 平台环境:Wi ...

  8. 理解CSV文件以及ABAP中的相关操作

    在很多ABAP开发中,我们使用CSV文件,有时候,关于CSV文件本身的一些问题使人迷惑.它仅仅是一种被逗号分割的文本文档吗? 让我们先来看看接下来可能要处理的几个相关组件的词汇的语义. Separat ...

  9. 在SharePoint中创建可自定义属性的文件夹

    概况 阅读时间:约5分钟 适用版本:SharePoint Server 2010及以上 面向用户:普通用户.管理员.开发人员 难度指数:★★★☆☆ SharePoint中的文件夹分为2种,一种是文档库 ...

随机推荐

  1. [Go] panic 和 recover

    通常情况下,函数向其调用方报告错误的方式都是返回一个 error 类型的值.但是,当遇到致命错误的时候,很可能会使程序无法继续运行.这时,上述错误处理方式就太不适合了,Go 推荐通过调用 panic ...

  2. C++11 function

    #include <iostream> #include <functional> #include <vector> using namespace std; / ...

  3. 自己动手写Vue插件Toast

    <style> .vue-toast { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; backgrou ...

  4. MyEclipse10安装Log4E插件

    一. Log4E插件下载 下载地址:http://log4e.jayefem.de/content/view/3/2/ 二.安装Log4E插件 将下载下来的压缩包解压缩,如下图所示: 解压缩生成的[d ...

  5. .Net Discovery系列之十一-深入理解平台机制与性能影响 (中)

    上一篇文章中Aicken为大家介绍了.Net平台的垃圾回收机制与其对性能的影响,这一篇中将继续为大家介绍.Net平台的另一批黑马—JIT.   有关JIT的机制分析   ● 机制分析   以C#为例, ...

  6. canvas使用1

    画直线: ? 1 2 3 4 5 6 7 8 9 10 11 var c = document.getElementById("myCanvas"); //不要忘写document ...

  7. SpringBoot打jar包问题

    原文:https://jingyan.baidu.com/article/6f2f55a11d6e09b5b93e6c9e.html 当你使用springBoot进行打包的时候,这篇经验会帮助到你的. ...

  8. Android按返回键退出程序但不销毁,程序后台运行,同QQ退出处理方式

    @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BA ...

  9. Web安全测试漏洞场景

    HTTP.sys 远程代码执行   测试类型: 基础结构测试   威胁分类: 操作系统命令   原因: 未安装第三方产品的最新补丁或最新修订程序   安全性风险: 可能会在 Web 服务器上运行远程命 ...

  10. ibatis.net:第五天,QueryForObject

    xml <statement id="LoadOrder" parameterClass="int" resultClass="Order&qu ...