1

I want to get the logged in user inside Sharepoint to pass by parameter in a link inside the web part "Page Viewer".

In our company we need to integrate our pages in SharePoint, and when a user logs into SharePoint, the page viewer send the link ex: http://example.com?user=loggedUser.

The client needs to only login once. If his login in SharePoint is not necessary to login again in our pages, so the link will send what user is trying to access and no need to login again. Is this possible?

What I want

1

This isn't the username but you can grab the userid by using _spPageContextInfo in the client object model.

You should be able to grab it with a call like:

var userId = _spPageContextInfo.userId

This should work for SP 2010 and 2013 and you can read about it here: http://blah.winsmarts.com/2013-2-_sppagecontextinfo_is_your_new_best_friend.aspx

1

I already found the answer. If anyone have the same question than me,here is the answer:

Fist add a script editor web part, then put this.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type="text/javascript">
  var userid= _spPageContextInfo.userId;
  var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
  var requestHeaders = { "accept" : "application/json;odata=verbose" };
  $.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
  });
 function onSuccess(data, request){
    var loginName = data.d.Title;
    var iframes = document.getElementsByTagName('iframe');
    for(var i =0; i < iframes.length; i++) {
        iframes[0].src = iframes[0].src + "?user=" + data.d.Title;
    }
 }

  function onError(error) {
    alert("error");
  }
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.