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

rss

Posted by Merley72 - Oct 15 ’15 at 03:55

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.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 16 days
    #1 by rostislav Oct 15 ’15 at 06:10

    Hello,

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

  • Merley72
    Member
    Member for: 4 years 10 months 20 days
    #2 by Merley72 Oct 15 ’15 at 07:34

    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.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 16 days
    #3 by rostislav Oct 16 ’15 at 05:08

    [#2]:

    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?

  • Merley72
    Member
    Member for: 4 years 10 months 20 days
    #4 by Merley72 Oct 16 ’15 at 05:46

    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.

  • Merley72
    Member
    Member for: 4 years 10 months 20 days
    #5 by Merley72 Oct 16 ’15 at 05:49

    Hi there, thought I had included the picture in the message, lets try again.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 16 days
    #6 by rostislav Oct 16 ’15 at 09:08

    [#5]:

    Sorry for the confusion! SharePoint 2010 doesn't have that option! Give me some time, I'll post the solution here.

  • Merley72
    Member
    Member for: 4 years 10 months 20 days
    #7 by Merley72 Oct 16 ’15 at 09:18

    Thanks a lot for the hard work and for a great product!!

    Alan C.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 16 days
    #8 by rostislav Oct 16 ’15 at 09:35

    [#5]:

    Try the code below. Make sure 'Name08' is the Internal Name of the field.

    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?

  • Merley72
    Member
    Member for: 4 years 10 months 20 days
    #9 by Merley72 Oct 16 ’15 at 10:10

    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.

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 3 months 16 days
    #10 by rostislav Oct 16 ’15 at 12:22

    [#9]:

    Code below will work with display pages:

    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').control()._el().find("tr a").each(function() {
    dic.push($(this).text());
      });
          var found = false;
    
          for (var i = 0; i < dic.length; i++) {
            if (dic[i] == user.get_loginName()){
              found = true;
            }
          }
          if (found) {
            $('.Table').show();
          }
          else {
            $('.Table').hide();
          }
    
    
        }); 
    }), "SP.js");
    
Displaying 1 to 10 of 13 messages
Previous12