如何在Sharepoint Web部件中获取域的用户名?

时间:2010-07-29 10:28:51

标签: sharepoint visual-studio-2010

我希望使用sharepoint webpart在Active Directory中获取用户名。 任何方式我可以使用ASP获取用户名。 net但我无法将该代码转换为sharepoint web part ....

这是代码.........

          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;

2 个答案:

答案 0 :(得分:0)

这里有一些让你入门的东西。它是包含数据绑定RadioButtonList的.NET Web部件类的框架。要将其部署到SharePoint,您还需要将适当的.webpart清单汇总在一起。 Web上有很多这样的示例,SharePoint 2010的Visual Studio 2010项目使Visual Web Part模板变得非常简单。 (请注意,如果您使用的是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.
    }
}

答案 1 :(得分: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(“String”),所以无论你想要什么就改变它....

注意 - 如果用户已登录,则此代码会显示Avtive Directory中的所有用户....

干杯!!!!!

Chinthaka