Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

SharePoint 2013 code works in EDIT mode but doesnt work normally - Retrieve and display first name of logged in user.

$
0
0

I am trying to retrieve the firstname and have added this as code in Site Assets and referred to it through the Content Editor Webpart.

I then output this through a HTML snippet

<p> Welcome to SharePoint </p> <div id="demo3" />

The code works fine when I am in edit mode.

However when the page is saved , the firstname is not shown. Any ideas?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/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 Logg = data.d;
         //get login name
        var loginName = Logg.LoginName.split('|')[1];
        var Reverse = Logg.Title.split(',')[1];
  document.getElementById("demo3").innerHTML = (Reverse);
    }
    function onError(error) {
        alert("error");
    }
</script>

 

Shaz



Viewing all articles
Browse latest Browse all 7589

Trending Articles