2

I want SPFieldUserValueCollection in CSOM. How do I and manipulate it? In particular, how to manipulate LikeBy, NoOfLikes, RatedBy, AverageRating, UserRating all using strictly CSOM.

  • Can you be more descriptive about your question regarding manipulation of rating – Unnie Sep 16 '16 at 13:47
  • Manipulation here is to able to add a user into LikeBy field or increment the value of the NoOfLikes field or recalculating the value of the AverageRating field when a new rating is added using the formula NewAverage=(OldAverage*OldNoOfRatings+NewRating)/NewNoOfRatings where NewNoOfRating (is obviously)= OldNoOfRating+1. etc. @Unnie – Ashwin Kheta Sep 19 '16 at 10:00
3

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;
    

No need to change the values in item["RatingCount"] and item["RatedBy"] fields.

| improve this answer | |
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Paul Strupeikis Oct 17 '16 at 8:45
  • made changes as suggested – Ashwin Kheta Oct 28 '16 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.