Blog

Check whether logged in user’s name is present in any people picker column

I wrote following js using jQuery library. It checks whether logged in user is present in any of the people picker column in the sharepoint form. Then I am using this to assign custom permisson on the field. It could be hide or show field, disable edit or you can attach any events to it as needed.

Note: You need to add Spserives and jQuery references. Recommend to download these libraries and store locally.

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var str = $('.ms-entity-resolved').toArray().reduce(function (a, p) {
  return a.concat.apply(a, _toConsumableArray(p.textContent.replace(/\s/g, ' ').trim().split(',')));
}, []);
 
 //replace(/\s\s+/g, ' ').trim().split(' ')));
//convert list of users to string   
var obj2str=String(str);

var na = $().SPServices.SPGetCurrentUser({
	   fieldName: "Title",
	    });
//convert current user to string  
var nastr=String(na);

var look=obj2str.search(nastr);
//alert(nastr);
//alert(obj2str);
//alert(look);

$().SPServices({ 
        operation: "GetGroupCollectionFromUser",
        userLoginName: $().SPServices.SPGetCurrentUser(),
        async: false,
        completefunc: function(xData, Status) {
            var check= $(xData.responseXML).find("Group[Name='Risk Manager']").length
      //  }
   // })                  
 
 if (check!=1)
 {
 if (look==-1){

   //People Picker Column
 jQuery("div[class$='sp-peoplepicker-topLevel']").attr("disabled", "disabled"); 
 jQuery("div[class$='sp-peoplepicker-topLevel']").css("background-color", "lightgrey"); 
 jQuery("input[class$='sp-peoplepicker-editorInput']").attr("disabled", "disabled");
 jQuery("span[class$='sp-peoplepicker-userPresence']").attr("disabled", "disabled");
 jQuery("a[class$='sp-peoplepicker-delImage']").hide();
 jQuery("a[class$='ms-imnlink ms-spimn-presenceLink']").hide();
 
 //SINGLE LINE OF TEXT
 $("input[class$='ms-long ms-spellcheck-true']").attr("disabled", "disabled"); 
 $("input[class$='ms-long ms-spellcheck-true']").css("background-color", "lightgrey"); 
  
  //RADIO AND DROPDOWN
   $("select[class$='ms-RadioText']").attr("disabled", "disabled"); 
  $("select[class$='ms-RadioText']").css("background-color", "lightgrey"); 
  
     $("input[class$='ms-RadioText']").attr("disabled", "disabled"); 
  $("sinput[class$='ms-RadioText']").css("background-color", "lightgrey"); 
  
    //multipline of text
  $("div[class$='ms-rtestate-write ms-rteflags-0 ms-rtestate-field']").attr("disabled", "disabled"); 
  $("div[class$='ms-rtestate-field ms-rtefield ms-inputBox']").attr("disabled", "disabled"); 
  $("div[class$='ms-rtestate-field ms-rtefield ms-inputBox']").css("background-color", "lightgrey"); 
 
  $("div[class$='ms-rtestate-write ms-rteflags-0 ms-rtestate-field']").css("background-color", "lightgrey"); 
    //inpput fields
  jQuery("input[class$='ms-input']").attr("disabled", "disabled");
   jQuery("input[class$='ms-input']").css("background-color", "lightgrey"); 

  $("select[id*='$DateTime']").css("background-color", "lightgrey"); 
  alert("You are not authorized to make edits on this form");

 //closing if
}
 else
 {

 }
 
//closing 2nd if
 }
//closing group lookup
 }
    })
 
      
 }); 
 
  
  

 

Leave a Reply

Your email address will not be published. Required fields are marked *