Tuesday 7 July 2009

How to Access SharePoint User Information in InfoPath

In InfoPath 2007 you can get the logon name of the current user by using the built-in function username(). (http://blogs.msdn.com/infopath/archive/2006/05/26/608092.aspx)

If you want to get more information that just the user name you should use the UserprofileService Web Service of MOSS. You have two options with this service:

  1. You can pass the account information of the user who’s details you need
  2. You can pass a null value, which will cause the web service to grab the current users identify

How to do it:

Step 1: Create the form

  1. Open InfoPath and design a new, blank form
  2. Save the form

Step 2: Set the security level of the form

  1. From the Main Toolbar, select Tools | Form Options
  2. Select Security and Trust from the list box
  3. Clear the checkbox next to Automatically determine security level
  4. Select Full Trust
  5. Hit OK.

Step 3: Add code to the form

  1. From the Main Toolbar, select Tools | Programming | Loading Event
  2. Right-click on UserProfileDemo node of the Project Explorer and choose Add Web Reference
  3. Enter the following for the URL: http://<yourServerName>/_vti_bin/UserProfileService.asmx
  4. Enter the following for Web reference name: ProfileService
  5. Click Add Reference
  6. Replace the FormEvents_Loading event handler with the following code snippet:

ProfileService.UserProfileService profileService = new Gravity.IP.Activities.SignUp.ProfileService.UserProfileService();
profileService.UseDefaultCredentials = true;
ProfileService.PropertyData[] userProps = null;
// Passing null to this method causes the profile of the current user to be returned.
userProps = profileService.GetUserProfileByName(null);
int prefferedNamePropID = -1;
if((userProps != null) && (userProps.Length != 0)){
    for (int i = 0; i < userProps.Length; i++) {
        if(prefferedNamePropID != -1){
            switch (userProps[i].Name.ToLower())  {
                case "preferredname":
                    prefferedNamePropID = i;
                    break;
            }
        }else{
            break;
        }
    }
}
if (prefferedNamePropID != -1) {
    ProfileService.ValueData[] values = userProps[prefferedNamePropID].Values;
    if (values.Length > 0) {
        String currentUserDisplayName = values[0].Value.ToString();
    }
}

No comments:

Post a Comment