1

I need to highlight a row in a list view, if current user is selected in a people or group field in this view, directly, or using SharePoint group. I really would like to do this using JS. Is this possible?

I know how to highlight a row, I only need the checking part.

I found this topic:

How to see if current user exists in people and groups field, using ecma/client-object-model

But I really didn't get how to implement this query in a script.

UPDATE: It seems that the fastest way to get current user for on-premises 2013 is _spPageContextInfo.userId. Now i need to somehow compare this to the view field, which is: Multi-value people or group field. Is it possible with CSR?

2 Answers 2

0

You can check JSLINK for same. Using JSLINK we can change rendering of listview.

For more detail check below reference link.

https://www.codeproject.com/Articles/620110/SharePoint-Client-Side-Rendering-List-Views

5
  • I know how to change rendering, i dunno how to get current user and compare him to the people or group field value. Feb 15, 2017 at 16:00
  • You can get current user ID by _spPagecontextinfo.Userid and compare it people picker column value. Feb 15, 2017 at 16:02
  • 1
    document.querySelectorAll("[href*='userdisp.aspx?ID="+_spPageContextInfo.userId+"']") gets you the A tags, you can walk up the DOM from there Feb 15, 2017 at 16:07
  • Thanks Sam, _spPageContextInfo.userId is working great, though it seems case-sensitive. Feb 15, 2017 at 18:40
  • Yes..It's case sensitive and please make answer as rihht. Feb 16, 2017 at 1:42
0

I ended up with the below script. I didn't really like the criteria i used to separate users form groups, but i didn't find any other simple option.

UPDATE
Updated the script and corrected errors. You can read further in my blog

//BELOW CODE SETS ROW COLOR IF USER IS SELECTED IN A int_x002e_lrn_x002e_members DIRECTLY OR THROUGH SPGROUP//
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  OnPostRender: function(ctx) {
    var SPfield = "int_x002e_lrn_x002e_members" //YOU CAN SET PEOPLE AND GROUPS FIELD HARE
    var RowColor = "#ada" //YOU CAN SET ROW COLOR HERE
    var requestUri = _spPageContextInfo.webAbsoluteUrl + '/_api/web/CurrentUser/Groups?$select=Id,Title';
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    var rows = ctx.ListData.Row;
    var usergroupsids=[];
    var result=null;
    result = $.ajax({url : requestUri,contentType : "application/json;odata=verbose",headers : requestHeaders,global: false,async:false,success : function(data) {return data;}})
    for (var i = 0; i < result.responseJSON.d.results.length; i++){usergroupsids.push(result.responseJSON.d.results[i].Id);}
    //alert(usergroupsids); //FOR TESTING
    for (var i=0;i<rows.length;i++)
    {
      for (var e=0;e<rows[i][SPfield].length;e++)
      {
        var isGroup = rows[i][SPfield][e].department == "";
        if (isGroup)
        {
            if (($.inArray(rows[i][SPfield][e].id, usergroupsids)) != -1)
            {
                var rowElementId = GenerateIIDForListItem(ctx, rows[i]);
                var tr = document.getElementById(rowElementId);
                tr.style.backgroundColor = RowColor; 
                //alert('Current User is in a group' + rows[i][SPfield][e].title) //FOR TESTING
            }
        //alert('Element ' + e + ' ' + rows[i][SPfield][e].title + ' is a Group') //FOR TESTING
        }
        else
        {
            if (_spPageContextInfo.userId == rows[i][SPfield][e].id) 
        {
            var rowElementId = GenerateIIDForListItem(ctx, rows[i]);
            var tr = document.getElementById(rowElementId);
            tr.style.backgroundColor = RowColor; //YOU CAN SET ROW COLOR HERE
            //alert('Element ' + e + ' ' + rows[i][SPfield][e].title + ' is a User') //FOR TESTING      
        }
        }
      }
    }
  }
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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