Wednesday, November 24, 2010

How to get user information in InfoPath web form programmatically?

Normally we can get the user information from SPUser object. There is only one issue: we need to add that user into at least one user group of the current site.

                SPUser objSPUser = objSPWeb.EnsureUser(strAccountId);
                string strUserName = objSPUser.Name;

What should we do if we only want to add "domain\domain users" into "site members" sharepoint user group?  In that case, we need to get the user information from User Profiles store.

First, define "GetUserProfileByName" data connection based on the SharePoint web service
Then, run the data connection as showed below:

                XPathNavigator xGetUserProfileByName = this.DataSources[@"GetUserProfileByName"].CreateNavigator();
                XPathNavigator xQueryAccountName = xGetUserProfileByName.SelectSingleNode(@"/dfs:myFields/dfs:queryFields/tns:GetUserProfileByName/tns:AccountName", NamespaceManager);
                xQueryAccountName.SetValue(strAccountId);

                WebServiceConnection dcGetUserProfileByName = (WebServiceConnection)this.DataConnections[@"GetUserProfileByName"];
                dcGetUserProfileByName.Execute();

                XPathNavigator xnAcctWorkEmail = xGetUserProfileByName.SelectSingleNode(@"/dfs:myFields/dfs:dataFields/tns:GetUserProfileByNameResponse/tns:GetUserProfileByNameResult/tns:PropertyData/tns:Values[../tns:Name = ""WorkEmail""]", NamespaceManager);
                XPathNavigator xnAcctDepartment = xGetUserProfileByName.SelectSingleNode(@"/dfs:myFields/dfs:dataFields/tns:GetUserProfileByNameResponse/tns:GetUserProfileByNameResult/tns:PropertyData/tns:Values[../tns:Name = ""Department""]", NamespaceManager);
                XPathNavigator xnAcctWorkPhone = xGetUserProfileByName.SelectSingleNode(@"/dfs:myFields/dfs:dataFields/tns:GetUserProfileByNameResponse/tns:GetUserProfileByNameResult/tns:PropertyData/tns:Values[../tns:Name = ""WorkPhone""]", NamespaceManager);

string strEmail = xnAcctWorkEmail.Value;

No comments:

Post a Comment