Return to Snippet

Revision: 45415
at April 29, 2011 19:52 by dimian12


Initial Code
/// <summary>
/// Returns the username of the currently logged in user
/// </summary>        
private string CurrentUserName()
{
    string userName = "NA";
    SPContext currentContext;
    try
    {
        //Getting the current context                
        currentContext = SPContext.Current;
    }
    catch (InvalidOperationException)
    {
       currentContext = null;
    }
    if (currentContext != null && currentContext.Web.CurrentUser != null)
    {
       userName = SPContext.Current.Web.CurrentUser.LoginName;
    }

    return userName;
}

Initial URL

                                

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

Initial Title
Name of currently logged in user in sharepoint

Initial Tags
sharepoint

Initial Language
C#