Name of currently logged in user in sharepoint


/ Published in: C#
Save to your folder(s)

Returns the username (domain\username) of the user that is currently logged in sharepoint. Can be used inside features / custom pages


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Returns the username of the currently logged in user
  3. /// </summary>
  4. private string CurrentUserName()
  5. {
  6. string userName = "NA";
  7. SPContext currentContext;
  8. try
  9. {
  10. //Getting the current context
  11. currentContext = SPContext.Current;
  12. }
  13. catch (InvalidOperationException)
  14. {
  15. currentContext = null;
  16. }
  17. if (currentContext != null && currentContext.Web.CurrentUser != null)
  18. {
  19. userName = SPContext.Current.Web.CurrentUser.LoginName;
  20. }
  21.  
  22. return userName;
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.