Get the User Profile through MOSS Web Services
2008-06-10 12:03
489 查看
Get the User Profile through MOSS Web Services
It is now easier than ever to pre-populate your InfoPath 2007 forms with the user profile information of the current user.Background:
In Microsoft SharePoint Server 2003, the UserProfileService.GetUserProfileByName(string accountName) web service method required the caller to pass the account name (e.g., domain/alias) associated with the user profile to retrieve. Though required, this argument was not needed when retrieving the profile of the current user. After all, the form was running with the network credentials of the current user and those credentials were passed to the web service.
The typical work-around was to deploy a custom WhoAmI web service as a façade for the UserProfileService. This façade would provide a method (e.g., WhoAmI() ) that would detect the user's identity and effectively forward a call to the UserProfileService, returning the eventual result to the caller.
New for Microsoft Office SharePoint Server 2007 and InfoPath 2007:
In InfoPath 2007, you can get the logon name of the current user by just using a new built-in function; but what if you need to get more data than just the username? Manager name, phone number, office location, and other data are all accessible through the UserProfileService: read on.
A form that uses the UserProfileService of MOSS to retrieve the User Profile of the current user no longer needs to explicitly pass the account information of the current user to the GetUserProfile method of the UserProfileService. Simply passing a null value to this method will cause the web service to grab the user identity from the request. The web service will then use that information as the account name and will subsequently return the User Profile of the current user.
Use this technique when you want to pre-populate the fields of an InfoPath 2007 form with user information obtained from the MOSS 2007 User Profile Database.
The following instructions detail how to build a form to demonstrate this capability.
Assumptions:
The default MOSS web application is configured to use Windows Authentication.
The MOSS server has an SSP with a User Profile Database that has been loaded from Active Directory
The end-user has a profile in the User Profile Database of the MOSS server
The end-user is logged into the client machine using his/her domain account.
Step 1: Create the form
Open InfoPath and design a new, blank form
Save the form as UserProfileDemo.xsn
Step 2: Design the form
From the Layout Task Pane, add a 2 column x 4 row table to the form
Place labels into the column 1 as follows: Name, Account, Email, and Phone
Using the Controls Task Pane, drag four text box controls to your form, placing one in each row of column 2
Using the Data Source Task Pane, rename the four "fieldX" data elements as follows: userName, userAccount, userEmail, and userPhone
Step 3: Set the security level of the form
From the Main Toolbar, select Tools | Form Options
Select Security and Trust from the list box
Clear the checkbox next to Automatically determine security level
Select Full Trust
Hit OK.
Step 4: Add code to the form
From the Main Toolbar, select Tools | Programming | Loading Event
Right-click on UserProfileDemo node of the Project Explorer and choose Add Web Reference
Enter the following for the URL: http://<yourServerName>/_vti_bin/UserProfileService.asmx
Enter the following for Web reference name: ProfileService
Click Add Reference
Replace the FormEvents_Loading event handler with the following code snippet:
public void FormEvents_Loading(object sender, LoadingEventArgs e) {
XPathNavigator myRoot = MainDataSource.CreateNavigator();
ProfileService.UserProfileService profileService = new ProfileService.UserProfileService();
profileService.UseDefaultCredentials = true;
ProfileService.PropertyData[] userProps = null;
try {
// Passing null to this method causes the profile of the current user to be returned.
userProps = profileService.GetUserProfileByName(null);
}
catch { }
if (userProps == null || userProps.Length == 0) {
return;
}
for (int i = 0; i < userProps.Length; i++) {
XPathNavigator node = null;
switch (userProps[i].Name.ToLower()) {
// these property names can be obtained by reviewing the user profile properties in the MOSS SSP.
case "preferredname":
node = myRoot.SelectSingleNode("/my:myFields/my:userName", NamespaceManager);
break;
case "accountname":
node = myRoot.SelectSingleNode("/my:myFields/my:userAccount", NamespaceManager);
break;
case "workemail":
node = myRoot.SelectSingleNode("/my:myFields/my:userEmail", NamespaceManager);
break;
case "workphone":
node = myRoot.SelectSingleNode("/my:myFields/my:userPhone", NamespaceManager);
break;
default:
continue;
}
ProfileService.ValueData[] values = userProps[i].Values;
if (values.Length > 0) {
if (node != null && string.IsNullOrEmpty(node.Value)) {
node.SetValue(values[0].Value.ToString());
}
}
}
}
Step 5: Build and Preview
Build the project and close VSTA.
From the Main Toolbar, select File | Preview| Form
When the form loads, the text boxes will be populated with your User Profile Information
This example opens the door to simplifying the form submission process for your users by reducing the time they take to complete forms, as well as ensuring the accuracy of the data that they enter.
Ronald Tielke
Microsoft Consulting Services
Desert Mountain District
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- Can't User UserProfileManager.GetUserProfile Get User Userprofile In Web Service,Error:System.ArgumentNullException: Value Cannot Be Null.(不能在Webservice中使用UserProfileManager的GetUserProfile得到用户配置,报错:System.ArgumentNullException: 值不能为空。)
- Java Web编程入门--错误信息“The method getUserById(int) of type UserServiceImpl must override a superclass”
- ?????Can't User UserProfileManager.GetUserProfile Get User Userprofile In Web Service(没解决的.只记录)
- JAVA -get-post-soap方式反问外部webservices
- Learn How TO Use SSH To Get Through the GFW
- MOSS 2007 Publishing web site definition template – onet.xml – syntax for properties and getting the right content types
- Get the parameter of webservice xml
- Getting the department_id through the user on domain filter
- Configuring the User Profile Service in SharePoint 2010
- 解决unable to get the project file from the web server
- Expert Service-Oriented Architecture in C#: Using the Web Services Enhancements 2.0
- tomcat9启动中提示 org.apache.catalina.webresources.Cache.getResource Unable to add the resource
- MOSS User Profile(四):代码创建用户配置文件
- org.apache.catalina.webresources.Cache.getResource Unable to add the resource
- jax-rs(Java API for RESTful Web Services)实践教程 之二 —— get/post/put/delete/head
- Get the SID of an Active Directory User
- Invalid root. The WEB-INF/flex folder must contain either flex-config.xml or services-config.xml 错误相关
- Get the page name in asp.net web page
- 关于安全的建议:对投入使用的 XML Web Services 禁用 HTTP-GET 和 HTTP-POST 协议
- iOS Programming 101: How To Get the User Location in iPhone App