Get Current Logged in user using  CSOM

Get Current Logged in user using CSOM

Below is the sample code, need to copy paste in Content Editor webpart – HTML and it will help you fetch current logged in user details.

One needs to have basic knowledge of how to use Content Editor Webpart.

//Code Starts

<script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript">   </script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.6.2/jquery.SPServices-0.6.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

   var currentUser = "";

   $(document).ready(function() {

   alert("Document ready");

       AddItem();

   });


   function AddItem()

   {

      var context = new SP.ClientContext.get_current();

      var web = context.get_web();

      user = context.get_web().get_currentUser();

      context.load(this.user);        

       context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),

       Function.createDelegate(this, this.onFailureMethod));

   }

   function onSuccessMethod(sender, args) {

       currentUser = this.user.get_title();

       var isSiteAdmin = user.get_isSiteAdmin();

          

       alert(' Name:' + this.user.get_title() + '\n Login:' + this.user.get_loginName());

       var tbl = $(this).closest('table');       

       {

         var myTr = [];

         $('.ms-listviewtable tr').each(function () {

                  var IsCurrentUser = false;

                  $(this).find('td').each(function () {

             if($(this).text() == currentUser && !IsCurrentUser)

                   IsCurrentUser = true;

            });

                  if(!IsCurrentUser)

                  $(this).attr( "disabled", "disabled");

          });

       }

     }

   function onFaiureMethod(sender, args) {

        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

   }

</script>&#160;

//Code ends.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了