2010-08-16 33 views
0

我從SharePoint站返回用戶名作爲字符串。這是成功完成與下面的代碼,但我也得到它的域名。我如何才能通過Sharepoint或以編程方式刪除它來返回用戶名而不是域名?域/用戶名Sharepoint檢索用戶名沒有域或以編程方式刪除域

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; 
    } 

回答

4

假設用戶返回以這種格式

domain\username 

,你可以做到以下幾點:

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

如果格式是這樣

[email protected] 

然後下面將w ork

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

也許你應該測試你的格式並提取基於此的用戶名。如果用戶名既不包含@也不包含\,那麼您只需返回整個字符串。

0

更改用戶信息列表中的名稱列,並且獲取SPContext.Current.Web.CurrentUser.Name ,因爲登錄名不能更改。

-2

請使用下面的代碼

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

user.name是不是這個一樣LOGINNAME我的名字可以是「用戶第一」和登錄「OC \用戶01」我以爲他要「用戶01」 – 2013-12-05 10:45:53

+0

不回答他的問題根本就是錯誤的答案。 – 2016-09-24 16:20:17