开发者

Sharepoint retrieving username without domain or programmatically removing domain

I'm returning the username from sharepoint site as a string. This is done successfully with the below code but I also get th开发者_运维百科e 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

domain\username

you can do the following:

string userWithoutDomain = userName.Substring(userName.IndexOf('\\') + 1);

If the format is like this

username@domain

then the following will work

string userWithoutDomain = user.Substring(0, user.IndexOf('@'));

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.


Change name column in user information list.and u get SPContext.Current.Web.CurrentUser.Name because login name can not change.


Please use below code

 site = new SPSite(SPContext.Current.Site.ID);
             web = site.OpenWeb();
            userName = web.CurrentUser.Name.ToString() ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜