0

Hi all i am very new to SharePoint i am working on SharePoint online. have admin rights. I am not using any SharePoint server or designer. I mapped my network drive to SharePoint and copy pasted a simple aspx page which has different layout to the site pages folder. This page is not in site definition and is a custom page. I want to display current user id on the page i searched a lot on net i was able to get the code but you are not allowed to use code behind file neither can you write code on the page so i thought of using script to get it. I ran a simple script and it runs on the page so i tried to get user id through it but it is not working.

<script> 
      (function myfunction() {
                    var propertyValue = Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName;
                    document.write(propertyValue);
                })()                    
</script>

I also tried this

<script type="text/javascript">
var loginName = _spPageContextInfo.userLoginName;
 document.write(loginName);
</script>

May be my my page is not part of SharePoint structure
Do i need to add any references on my page or is there some other way i can get the current logged in user id

2 Answers 2

1

To get User ID

var UserID = _spPageContextInfo.userId;

It will return the ID of the current User, not the Login Name.


To Get Login Name like "domain\mqassas"

var context = new SP.ClientContext.get_current();
   var web = context.get_web();  
   var currentUser = web.get_currentUser();  
   currentUser.retrieve();
   context.load(web);
   context.executeQueryAsync(
      function(){ //On success function
        var userObject = web.get_currentUser();
        var loginName = userObject.get_loginName();
        alert(loginName);

     },
     function(){ //On fail function
        alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
     }
  );

[Update]

Try to do the following to load SP.js in ASPX in the SharePoint Site

  • Open SharePoint Designer.
  • Connect to your site > Go to site pages
  • From the above ribbon > add ASPX page.
  • Edit the Page in Advanced Mode.
  • Within the <head> section add the below code.

Code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script>


   $(document).ready(function(){
        var scriptbase = "http://SiteURL" + "/_layouts/15/";
    //Do not change the order, scripts should load by order.  
     $.getScript(scriptbase + "init.js",
            function () {
                $.getScript(scriptbase + "SP.Runtime.js",
                   function () {
                $.getScript(scriptbase + "SP.js", GetLoginName);
              });
          });
           });
// **add your code**

function GetLoginName()
{
var context = new SP.ClientContext.get_current();
   var web = context.get_web();  
   var currentUser = web.get_currentUser();  
   currentUser.retrieve();
   context.load(web);
   context.executeQueryAsync(
      function(){ //On success function
        var userObject = web.get_currentUser();
        var loginName = userObject.get_loginName();
        alert(loginName);

     },
     function(){ //On fail function
        alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
     }
  );
}
</script>

At scriptbase, replace "http://SiteURL" with your site URL.

7
  • It's not working i wrote this in script tag <script></script> and simple pasted it on my page. It shows error code blocks not allowed on this page
    – Andy
    Oct 28, 2017 at 8:53
  • is it SharePoint page? Oct 28, 2017 at 8:54
  • in aspx you will not found _spPageContextInfo also SP is undefined , let me update the answer with more details Oct 28, 2017 at 9:01
  • It is a simple page in my site pages folder. I was adding runat server in script tag that is why it showed the error but even without it the page is loading but the script still doesn't work
    – Andy
    Oct 28, 2017 at 9:03
  • From where you create this page? Oct 28, 2017 at 9:05
0

What you have here is a disconnected page in SharePoint. Ideally the page should be created via Designer or Add Page via the Ribbon etc.

That being said, you can resolve the problem as below

  1. Make sure you have registered SharePoint Namespace and controls using below markup

    <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    
  2. Add SharePoint script files

    <script src="/_layouts/1033/init.js"></script>
    <script src="/_layouts/1033/core.js"></script>
    <script src="/_layouts/MicrosoftAjax.js"></script>
    <script src="/_layouts/SP.Core.js"></script>
    <script src="/_layouts/SP.Runtime.js"></script>
    <script src="/_layouts/SP.js"></script>
    <script src="/_layouts/SP.UI.Dialog.js"></script>
    <script src="/_layouts/ScriptResx.ashx?culture=en%2Dus&name=SP%2ERes"></script>
    
  3. Add Form Digest

    <form runat="server">    
      <SharePoint:FormDigest runat="server"></SharePoint:FormDigest>
      <!-- Your page data goes here -->
    </form>
    

Now you can follow the code provided by Qassas to find the current user context.

0

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.