1

I had a long search and until I found a working script to use the logged in user id. Finally I have found the following Javascript code, put it in XSL to be used in an XMl viewer web part:

<script language="javascript" defer="true">
      <xsl:comment>
        <![CDATA[

ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

        ]]>
      </xsl:comment>
    </script>

I call the function like this:

<A class="bluelink" href = "javascript:void(0)" onclick="javascript:window.location='{$mailtolink}' + '%0D%0Auserid: ' + currentUser.get_loginName() + '%0D%0Aurl: ' +  window.location; return false;">Request additional rights</A>

The mailtolink variable represents a valid mailto: link. The problem is, that in IE11 (also in IE8 compatibility mode), the mail body ends only with

userid: i:0

However in Chrome, I get all the details I need:

userid: i:0#.w|tdomain\testuser
url: https://myserver/testsite/default.aspx

I made the mail body shorter, to make sure it is not the lenght, but I got the same results. Can someone give me a hint how to make the script work in IE?

6
  • If you just want login name of the current user then simply use _spPageContextInfo.userLoginName this variable provided by SharePoint and you can use that to get Login name.
    – Pradip R.
    Jul 6, 2017 at 9:57
  • Also your onQuerySucceeded method is empty. That's where you get currentUser.get_loginName() . If you could console this in browser developer tool. You might figure what's wrong there.
    – Pradip R.
    Jul 6, 2017 at 9:59
  • sharepoint.stackexchange.com/questions/73064/… Reference Link
    – Pradip R.
    Jul 6, 2017 at 9:59
  • 1
    The _spPageContextInfo.userLoginName is only available since Sharepoint 2013, so that's not an option for me. The onQuerySucceeded method is empty, because I update the currentUser object in the line currentUser = this.oWeb.get_currentUser(); If I put an alert in onQuerySucceeded I get the popup with the correct userid.
    – vilmarci
    Jul 6, 2017 at 10:11
  • If you are getting alert in onQuerySucceeded then your code is correct. Just need to update the flow. As far as my knowledge your execution is taking time and your HTML is already rendered by that time, So I would suggest you to have any DOM element and in your onQuerySucceeded method, you should update innerHTML of that element.
    – Pradip R.
    Jul 6, 2017 at 10:14

3 Answers 3

0

If you are getting alert of user name as per your comment then you should follow below approach:

DOM Element:

<a id="myLink" class="bluelink" href="javascript:void(0)">Request additional rights</a>

Script:

<script language="javascript" defer="true">
  <xsl:comment>
    <![CDATA[
        ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
        var currentUser;
        function init(){
            this.clientContext = new SP.ClientContext.get_current();
            this.oWeb = clientContext.get_web();
            currentUser = this.oWeb.get_currentUser();
            this.clientContext.load(currentUser);
            this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
        }

        function onQuerySucceeded() {
            $('#myLink').attr("onclick", "javascript:window.location='<..your url string..>"+ currentUser.get_loginName() + "return false;");
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
        }

    ]]>
  </xsl:comment>
</script>

Your code might be failing due to invalid flow of execution for IE.

IE has different mechanism than other browser.

3
  • Thank you for the answer, but I get this error message: The value of the property '$' is null or undefined, not a Function object There is an unclosed ' in the line starting with $, I tried to fix, but without success.
    – vilmarci
    Jul 6, 2017 at 13:09
  • 1
    Oh that's because your page doesn't have jQuery library loaded in it. Make sure your jQuery library reference is there on the page.
    – Pradip R.
    Jul 6, 2017 at 13:10
  • Ok, I added jQuery, but now I see the error 'currentUser' is undefined I think, I adapted your code 1:1 $('#myLink').attr("onclick", "javascript:window.location='mailto:[email protected]?subject=Request additional rights&amp;body=Dear Team...,"+ currentUser.get_loginName() + "return false;");
    – vilmarci
    Jul 6, 2017 at 13:35
0

I finally found the answer: It looks like that IE doesn't like the #, that was present in the userid i:0#.w|tdomain\testuser, in the mailto URL. Doesn't matter where I put the #, the rest of the url is ignored by IE.

0

If you want to get just the user ID of currently logged in user, you can use the following code.:

 var userID = _spPageContextInfo.userId;
1

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.