Skullcrusher's Blog

Andy Bonner's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

Congratulations! You've managed to stumble across the mad rantings of Andy Bonner aka Skullcrusher

Here you'll find all sorts of hopefully useful info on MOSS (Microsoft Office SharePoint Server) and things .NET related. You might even find other useful gems if I get around to it.


You can contact me on the address below, but I won't guarantee a response 8-}

AprMay 2008Jun
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Articles

Archives

Topics

Image Galleries

MOSS/SharePoint Links

Although you can easily get hold of the logged in users login name with

this.Application.User.LoginName

it's doesn't seem to be so easy to get hold of the users full name. It is however releatively easy to accomplish by making a call to one of the many webservices provided by MOSS, specifically the GetUserInfo method of the UserGroup webservice, which I'll guide you through now.

You'll need to add the following class that I've created using the xsd.exe tool (it represents the details returned in the User node of the GetUserInfo Method) to your project.

Code Copy HideScrollFull
//------------------------------------------------------------------------------
//
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.42
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//

//------------------------------------------------------------------------------

using
System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//


///

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.microsoft.com/sharepoint/soap/directory/", IsNullable = false)]
public
partial class User {

    private string idField;

    private string sidField;

    private string nameField;

    private string loginNameField;

    private string emailField;

    private string notesField;

    private string isSiteAdminField;

    private string isDomainGroupField;

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ID {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Sid {
        get {
            return this.sidField;
        }
        set {
            this.sidField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string LoginName {
        get {
            return this.loginNameField;
        }
        set {
            this.loginNameField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Email {
        get {
            return this.emailField;
        }
        set {
            this.emailField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Notes {
        get {
            return this.notesField;
        }
        set {
            this.notesField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsSiteAdmin {
        get {
            return this.isSiteAdminField;
        }
        set {
            this.isSiteAdminField = value;
        }
    }

    ///
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsDomainGroup {
        get {
            return this.isDomainGroupField;
        }
        set {
            this.isDomainGroupField = value;
        }
    }
}
. . .

You'll also need to set the forms security level to Full Trust.

Now we need to create the data connection to the web service.

Click Data -> Data Connections

 

Click Add

 

Select Create a new connection to Receive data then click Next

 

Select Web service then click Next

 

Enter http://YourServerName/_vti_bin/UserGroup.asmx for the webservice location making sure you put in your moss servers name then click next

 

Select GetUserInfo from the list of available operations then click Next

 

Infopath needs to get more info from the webservice operation which means you'll have to provide a valid Login name for it to query the webservice with, so click on the Set Sample Value button & input a user in the following format Domain\Login

 

Click OK then click Next

 

Click Next

 

Click Next

 

Enter a meaningful name for the data connection & ensure you remove the tick from Automatically retrieve data when the form is opened. Then click Finish & close the data connections window. Within your form code you can now add the using directive to System.Xml.Serialization & the following to the formloading event to retrieve the UserInfo details

Code Copy HideScrollFull
public void InternalStartup()
        {
            this.EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
        }

        void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            //get hold of the datasource for the webservice operation
            DataSource ds = this.DataSources["GetUserInfo"];
            //Create an XPathNavigator to the node we have to set to query the method
            XPathNavigator userField = ds.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:queryFields/tns:GetUserInfo/tns:userLoginName", this.NamespaceManager);
            //Set the query value to the current users login name
            userField.SetValue(this.Application.User.LoginName);
            //execute the webservice operation
            ds.QueryConnection.Execute();

            //the data will now be in the datasource
            XPathNavigator userInfo = this.DataSources["GetUserInfo"].CreateNavigator();
            //add the namespaces so we can query the datasource
            XmlNamespaceManager umanager = new XmlNamespaceManager(userInfo.NameTable);
            umanager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
            umanager.AddNamespace("tns", "http://schemas.microsoft.com/sharepoint/soap/directory/");
            //get hold of the node we're after
            XPathNavigator usernode = userInfo.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserInfoResponse/tns:GetUserInfoResult/tns:GetUserInfo/tns:User", umanager);
            //deserialize the contents of the node into our custom class
            User user;
            XmlSerializer serializer = new XmlSerializer(typeof(User));
            user = (User)serializer.Deserialize(usernode.ReadSubtree());

            //set the value of a field within out main data source i.e. the InfoPath form
            //to the users full name
            XPathNavigator field = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);
            field.SetValue(user.Name);
        }
. . .

 and voila!

 

 

If you take a look at the User class you'll notice that there's other information available to you from the GetUserInfo method.

 

Hope this information is useful to someone & saves them some time trying to figure this one out.

posted on Wednesday, December 20, 2006 11:59 AM

Feedback

# re: Get the Full user name in web hosted InfoPath Form 4/23/2007 11:29 PM Google排名
Good ! thank the author.

# re: Get the Full user name in web hosted InfoPath Form 5/23/2007 2:31 AM Wosiu
Is it possible to do this without using code?

# re: Get the Full user name in web hosted InfoPath Form 5/24/2007 9:24 AM Andy Bonner
Wosiu
Unfortunately not

# re: Get the Full user name in web hosted InfoPath Form 5/26/2007 12:40 AM Andreas
Hi Andy,

I reproduced your great step-by-step guidance (the form works perfectly in the InfoPath preview) and uploaded the template to our MOSS2007 server (and activitated it for the resp. site collection). Everything works fine till here and the template is visible in the form-template-list. When I try to open the form I get errors (in browser as well as in InfoPath):

Browser: Some data access error with "System.Net.WebException: Unable to connect to the remote server" and Log ID:5337

InfoPath: Concerning permission, trust and certificates.

Those errors seem to appear in any project where I have worked on the code.

I also found a post that describes how to get User-Info without writing any code (also web-enabled). What do you think of that? I receive the same error message as described in the first comment.

Any suggestions?

Have a nice Weekend!

Andreas



# re: Get the Full user name in web hosted InfoPath Form 5/26/2007 12:42 AM Andreas
Me again,

forgot the link:

http://blogs.microsoft.co.il/blogs/itaysk/archive/2007/04/05/InfoPath-_2D00_-Get-the-current-user-without-writing-code.aspx

# re: Get the Full user name in web hosted InfoPath Form 5/28/2007 8:15 AM Andy Bonner
Hi Andreas

Thanks for the link, just goes to show there's always another way of doing things if you know where to look. I'll have to give it a try when I get a spare minute, but from a quick look at the post it looks like it should do the trick nicely.

As for your problem I think it's going to be a permissions issue, what security level have you set for your form?

Try setting it to Full Trust & let me know how you get on.



# re: Get the Full user name in web hosted InfoPath Form 5/29/2007 9:05 AM Andy Bonner
Andreas

I got chance to try the method from the link you posted & it works like a charm

# re: Get the Full user name in web hosted InfoPath Form 5/30/2007 12:16 AM Andreas
Hi Andy,

the form was set to Full Trust all the time. I found out, that these issues arise as soon as I add a Webservice to its Data Connections (Log says: Query failed, Error-Type: DataAdapterException, Exception Message: Unable to connect to the remote server Unable to connect to the remote server).

At the moment I'm trying to find out if UDC and DCL could help me out of this. But I still wonder why your and itaysk's method work perfeclty on other systems.

Thanks a lot for your effort!

# InfoPath 2007 e Forms Services 9/1/2007 2:45 PM Il blog di Luca Mauri
InfoPath 2007 e Forms Services

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: