码迷,mamicode.com
首页 > 其他好文 > 详细

SharePoint中Rating相关的字段。

时间:2017-05-25 13:25:00      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:text   where   ext   wave   imp   bre   each   should   post   

?

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. This field stores the number of users who have rated the item its value should be incremented.

  2. 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. 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. 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. 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. 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相关的字段。

标签:text   where   ext   wave   imp   bre   each   should   post   

原文地址:http://www.cnblogs.com/time-is-life/p/6902932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!