Freigeben über


Getting Current User in InfoPath 2007 via C#

Folks,

Got a little code sample from a fellow watcher of Pashman's Goldmine (thanks Huseyin) on how to retreive the username of the current user via C# code.  Since all code samples are useful, I've posted it below:

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

string xpath = "/my:oneriForm/my:nameTxt";

string userName1;

XPathNavigator field1 = MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);

//string oldValue = field1.Value; // Read

string strUserName = System.Environment.UserName;

//userName1 = System.Environment.

field1.SetValue(strUserName);

// Write

In this case the user wanted the full user name too, and I advised him to try and replace the System.Environment.UserName with System.Environment.FriendlyName.   I'll let you know if that worked.

Comments

  • Anonymous
    September 28, 2006
    System.Environment.UserName requires Full trust, or more precisely a
    permission that only Full trust forms would get.



    Note that System.Environment.UserName will not return the real WSS user
    name, which I assume your code actually needs. Instead, we recommend using
    the InfoPath OM to get the user name.
  • Anonymous
    January 12, 2007
    The comment has been removed