2010-07-29 35 views
0

我想通過使用sharepoint webpart獲取活動目錄中的用戶名。 任何方式,我可以通過使用ASP獲取用戶名。淨,但我不能轉換的代碼中的SharePoint Web部件....如何獲取用戶Sharepoint web部件中的域名?

這裏是代碼.........

  using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Web; 
      using System.Web.UI; 
      using System.Web.UI.WebControls; 
      using System.DirectoryServices; 
      using System.Data; 


     string domain = "LDAP://serverName"; 
     DirectoryEntry entry = new DirectoryEntry(domain); 

     DirectorySearcher searcher = new DirectorySearcher(entry); 

     searcher.Filter = "(&(objectClass=user))"; 

     SearchResultCollection resultCol = searcher.FindAll(); 

     //Link list for store user names 
     List<String> User_Names = new List<String>(); 

     int count = 0; 

     foreach (SearchResult result in resultCol) 
     { 
      User_Names.Add(result.Properties["CN"][0].ToString()); 
      count = count + 1; 
     } 

     //can print all user names by using for loop or while loop 

     Label2.Text = RadioButtonList1.SelectedItem.Text; 

回答

0

這裏有一個小東西,讓你開始。它是一個.NET Web部件類的框架,包含一個數據綁定的RadioButtonList。爲了將其部署到SharePoint,您還需要合併一個正確的.webpart清單。 Web上有很多這樣的示例,Visual Studio 2010的SharePoint 2010項目使Visual Web部件模板變得非常容易。 (請注意,如果您使用的是Visual Web Part模板,那麼您可以像使用其他用戶控件一樣,使用.ascx文件編寫Web部件,而不是像以下那樣使用CreateChildControls()。)

祝您好運!

using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 

/// <summary> 
/// This web part provides redirection logic to a page. 
/// </summary> 
public class RedirectWebPart : System.Web.UI.WebControls.WebParts.WebPart 
{ 
    /// <summary> 
    /// Creates the child controls. 
    /// </summary> 
    protected override void CreateChildControls() 
    { 
     base.CreateChildControls(); 
     RadioButtonList list = new RadioButtonList(); 
     Controls.Add(list); 

     list.AutoPostBack = true; 
     list.SelectedIndexChanged += new EventHandler(this.OnSelectedNameChanged); 

     // On the next line, ADUtility.GetActiveDirectoryNames() is a dummy 
     // class and method that you should replace with your AD query logic. 
     // It just needs to return an enumerable collection of strings. 
     list.DataSource = ADUtility.GetActiveDirectoryNames(); 
     list.DataBind(); 
    } 

    protected void OnSelectedNameChanged(object sender, EventArgs e) 
    { 
     // Your event-handling logic. 
    } 
} 
0

//不要忘記System.DirectoryServices.AccountManagement添加作爲參考和使用System.DirectoryServices.AccountManagement;

 PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MyDomain","DC=MyDomain,DC=com"); 
     UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext); 
     insUserPrincipal.Name = "*"; 
     PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher(); 
     insPrincipalSearcher.QueryFilter = insUserPrincipal; 
     PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll(); 
     foreach (Principal p in results) 
     { 
      Console.WriteLine(p.Name); 
     } 

上面的代碼工作對我來說.... 但我不得不做一些修改 (1)拆下, 「DC = MYDOMAIN,DC = COM」 的一部分,並將其保存和部署Web部件

(2)中的SharePoint Web部件沒有任何Console.Writeline(「字符串」),以便改變它無論你想....

注意 - 此代碼是顯示所有用戶的Avtive目錄如果用戶登錄或不...

啦啦啦s !!!!!

Chinthaka

相關問題