Posted By

dimian12 on 04/29/11


Tagged

sharepoint


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jlaws0n


Name of currently logged in user in sharepoint


 / Published in: C#
 

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

  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  

You need to login to post a comment.