Page 1 of 2

Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 15 Oct 2015
by Merley72
Hello again,

I'm wondering if I can run a check on a specific User-defined Group page to see if the current user's name is the same as that included in a field called 'Name08'. I've added the script below to the Javascript for Edit and New forms, and have tested the logic of the 'hide' by testing it against a different field altogether and that works fine. However using either the CurrentUserName element or ContainsCurrentUser in the 'IF' statement seems to kill everything. I've set the elements o f the 'else' to be the 'hide' option and still nothing happens.

What I'm looking to do is to hide part of a table if the person's name isn't included in the Name08 field. the code I'm using is:-

if(ContainsCurrentUser([Name08])) {
$('.Table').show();
} else {
$('.Table').hide();
}

Can anyone please advise how I'm going wrong?

Thanks in advance. Alan C.

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 15 Oct 2015
by rostislav
Hello,

What type of people picker field are you using? Server or client side rendering mode (check in Forms Designer), single or multiple values?

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 15 Oct 2015
by Merley72
Hi,

The people picker I'm using (called in Name08 in the system) is a field created in the SharePoint list and appended onto the relevant Group forms as normal. It is a multi-selection option but only allows people and not groups. In order to test things further I have added a second People picker to the list which is a single name field and completed tests against that as well.

I'm not certain what I would be looking for in the FormsDesigner side of things to identify the rendering mode, however I do see under the 'Field' tab for the column/field in FD the word User next to Type - is this what you are referring to ?

Thanks for your assistance, Alan C.

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by rostislav
If you look take a look at the section above called 'Layout', there is a field called 'Render' with two options: 'Client' and 'Server'. Which one are you/will you be using?

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by Merley72
Hi Rostislav,

I've looked at the two 'live' and one development instances of Forms Designer and don't see in the Layout section the 'Render' element you are referring to (see copy of element here ) -

I've just made certain by changing the field type from single choice to multi and also groups as well then looking in the Forms Designer view of the field and Render just isn't visible - sorry!

Alan C.

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by Merley72
Hi there, thought I had included the picture in the message, lets try again.

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by rostislav
Sorry for the confusion! SharePoint 2010 doesn't have that option! Give me some time, I'll post the solution here.

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by Merley72
Thanks a lot for the hard work and for a great product!!

Alan C.

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by rostislav
Try the code below. Make sure 'Name08' is the Internal Name of the field.

Code: Select all

SP.SOD.executeOrDelayUntilScriptLoaded((function () { 
    var ctx = new SP.ClientContext.get_current(); 
    var web = ctx.get_web(); 
    ctx.load(web); 
    var user = web.get_currentUser(); 
    user.retrieve(); 
    ctx.executeQueryAsync( 
    function () { 
      var dic = fd.field('Name08').value().dictionaryEntries;
      var found = false;

      for (var i = 0; i < dic.length; i++) {
        if (dic[i].AccountName == user.get_loginName()){
          found = true;
        }
      }
      if (found) {
        $('.Table').show();
      }
      else {
        $('.Table').hide();
      }


    }); 
}), "SP.js");
Oh, and this code is for when you're on the edit or new pages. Will you need this for the view page also?

Re: Using the 'ContainsCurrentUser' in the Javascript editor of a Group page

Posted: 16 Oct 2015
by Merley72
Excellent!! (Once I'd remembered to remove all the bits of script I'd used to test things!!)

Currently I'm not allowing visibility of the table in 'read' mode so that isn't an immediate need, however I can image it will be useful for another thing I'm building. If you can get that as well for the display side working that would be great.

Thanks again for this - very helpful and it saves me from doubling up the 9 stages I'm managing currently.

Alan C.