[SharePoint 2010]在SharePoint中几种访问域用户profile的方法
在SharePoint2010中,我们可以选择几种不同的方式来访问域用户的profile,下面简单介绍3种方法:
1.通过客户端直接调用user profile service来访问
例如项目中有silverlight模块想显示当前用户的profile信息,可以这么作:
首先添加Service Reference,输入service url http://rootsite/sites/subsite/_vti_bin/UserProfileService.asmx?WSDL
rootsite & subsite请根据实际情况替换。然后我们为我们的Service reference取名为UserProfileServiceProxy。
其次在maipage方法中加入调用service 代码以及回调方法代码:
public MainPage()
{
InitializeComponent();
client = new UserProfileServiceProxy.UserProfileServiceSoapClient();
client.Endpoint.Behaviors.Add(new AsmxBehavior());
client.GetUserProfileByNameCompleted +=new EventHandler<UserProfileServiceProxy.GetUserProfileByNameCompletedEventArgs>(client_GetUserProfileByNameCompleted);
client.GetUserProfileByNameAsync(null);
}
private void client_GetUserProfileByNameCompleted(object sender, UserProfileServiceProxy.GetUserProfileByNameCompletedEventArgs e)
{
if (e.Error == null)
{
string s = "";
for (int i = 0; i < e.Result.Count; i++)
{
if (e.Result[i].Values.Count > 0)
{
s +="{" + i.ToString() + "}" +"-->" + e.Result[i].Values[0].Value.ToString() + "\n";
}
}
MessageBox.Show(s);
}
}
其中加入AsmxBehavior类的原因是silverlight对guid的deseriable无法识别,网上找了个别人写的类,不添加这个bahavior,service调用会报异常,AsmxBehavior类和AsmxMessageInspector类就解决了这个问题,用的时候,把这两个类加进你的项目中就可以了。AsmxBehavior类和AsmxMessageInspector类的详细代码会在附件中加上。
这样就可以得到一个当前user的profile信息了,可以看见我们主要是调用了GetUserProfileByNameAsync(null)这个方法,传入null参数返回当前用户profile,当然你可以给别的name来得到相应的profile。
2. 在server直接通过SharePoint 的 Object Model取得service,方法如下:
using (SPSite site = new SPSite("siteurl"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager pmManager = new UserProfileManager(context);
System.Collections.IEnumerator item = pmManager.GetEnumerator();
while (item.MoveNext())
{
UserProfile userProfile = item.Current as UserProfile;
object o = userProfile[PropertyConstants.Url].Value;
}
}
注意此方法需要先添加引用下面2个dll文件:
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
3.当然当你有一个可以直接从公司域环境中读取信息的帐号时,也可以直接去AD中读取用户profile,方法如下:
using System.DirectoryServices;
static void Main(string[] args)
{
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://ads.autodesk.com", @"youraccount", "password",AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(directoryEntry);
ds.Filter = "(&(objectCategory=Person)(objectClass=User))";
SearchResultCollection collection = ds.FindAll();
int count = 0;
foreach (SearchResult sr in collection)
{
System.DirectoryServices.DirectoryEntry det = sr.GetDirectoryEntry();
PropertyCollection pc = det.Properties;
if (det.Properties["mail"].Value != null && det.Properties["sAMAccountName"].Value != null)
{
Console.WriteLine(det.Properties["mail"].Value.ToString() + "--------" + det.Properties["sAMAccountName"].Value.ToString());
count++;
}
if (det.Properties["sAMAccountName"].Value.ToString() == "your account")
{
foreach (string propName in pc.PropertyNames)
{
foreach (object value in det.Properties[propName])
Console.WriteLine(" property = {0} value = {1}",
propName, value);
}
}
}
Console.WriteLine(count);
Console.Read();
}