I’m returning the username from sharepoint site as a string. This is done successfully with the below code but I also get the domain with it. How can I only return the username and not the domain either through sharepoint or programmatically removing it? domain/username
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;
}
else
{
}
return userName;
}
Assuming the user is returned in this format
you can do the following:
If the format is like this
then the following will work
Probably you should test which format you have and extract the user name based on that. If the user name contains neither a @ nor a \ then you just return the entire string.