Skip to content

Instantly share code, notes, and snippets.

@madcatgith
Created May 13, 2016 12:54
Show Gist options
  • Save madcatgith/99e586e1fdae1250d484b14a3c5b7a7f to your computer and use it in GitHub Desktop.
Save madcatgith/99e586e1fdae1250d484b14a3c5b7a7f to your computer and use it in GitHub Desktop.
List filter by current user (+in group)
<script type="text/javascript">
var el_id = [];
var state = [];
(function(){
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.OnPostRender = [get_group];
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function IsInGroup(element_id,groupId,userId,usersList) {
try {
console.log("in group id " + groupId);
console.log("in user id " + userId);
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);
if (!!groupId || groupId!="")
{
var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);
var group = allGroups.getById(groupId);
currentContext.load(group);
var groupUsers = group.get_users();
currentContext.load(groupUsers);
}
document.getElementById(element_id).style.backgroundColor="#CD5C5C";
currentContext.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(sender, args) {
if ((currentUser.get_id() == userId)&&!!userId&&userId!="")
{
document.getElementById(element_id).style.backgroundColor="#FFFE59";
}
if (!!groupId && groupId!="")
{
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
if (userInGroup)
{
document.getElementById(element_id).style.backgroundColor="#ada";
}
}
if (!!usersList)
{
for (var i=0;i<usersList.length;i++)
{
if (usersList[i]["id"]==currentUser.get_id())
document.getElementById(element_id).style.backgroundColor="#add8e6";
}
}
console.log("Successfull transmission");
}
function OnFailure(sender, args)
{
console.log("failed query sending");
}
}
catch(e){console.log("error in IsInGroup "+e);}
}
/*function IsUser (element_id,user_id){
try{
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);
currentContext.executeQueryAsync(OnSuccess,OnFailure);
//}
function OnSuccess(sender, args) {
if (currentUser.get_id()==user_id){
document.getElementById(element_id).style.backgroundColor="#FFFE59";
}
else
{
document.getElementById(element_id).style.backgroundColor="#CD5C5C";
}
console.log("Successfull transmission");
}
function OnFailure(sender, args) {
console.log("failed query sending");
}
}
catch (e) {
console.log("error in IsUser " + e);
}
}
*/
function get_group (inctx){
try {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
var rows = ctx.ListData.Row;
for (var i=0;i<rows.length;i++){
if (!!rows[i]){
var n= GenerateIIDForListItem(ctx, rows[i]);
var gp=rows[i]["group"];
var usr=rows[i]["user1"];
var gp_id="";
var usr_id="";
var usrs_lst=rows[i]["users"];
if (!!gp)
{
gp_id=gp[0]["id"];
}
if (!!usr)
{
usr_id=usr[0]["id"];
}
if (!!n && (!!usr_id || !!gp_id || !!users_lst))
{
IsInGroup(n,gp_id,usr_id,usrs_lst);
}
}
}
});
}
catch(e){
console.log("error in get_group "+ e);
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment