AD Web Service Solution - InfoPath Dev
in

InfoPath Dev

AD Web Service Solution

Last post 04-21-2008 04:15 AM by zeroecho. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 06-25-2007 07:38 AM

    AD Web Service Solution

    So here goes the Web Service

    public string[] getUserInfo(string account, string whatToPass, string whatToGet)
    {
     string delimStr = "-";
     char[] delimiter =  delimStr.ToCharArray();
     string[] credentials = whatToGet.Split(delimiter);      
     string[] infoResult = new string[credentials.Length];
     int count = credentials.Length;
     try
     {
      // Create a directory searcher on the path provided when created the instance
      DirectoryEntry entry  = new DirectoryEntry("LDAP://OU= Users,DC=,DC=,DC=");
      DirectorySearcher searcher = new DirectorySearcher(entry);
      string ToFind    = "(" + whatToPass +"=" + account + ")";
      searcher.Filter    = ToFind;
      // Try to find the employee
      SearchResult result   = searcher.FindOne();
      if (result != null)
      {
       // Get the 'DirectoryEntry' that corresponds to 'result'.
       DirectoryEntry myDirectoryEntry =  result.GetDirectoryEntry();
       // Get the properties of the 'mySearchResult'.
       ResultPropertyCollection myResultPropColl;
       myResultPropColl = result.Properties;
       
       for(int i = 0; i < count; i++)
       {
        //important !!! check if the property you want to get the information of exists in the user properties.
        //if this check is missing an error will be thrown "Object reference not set to an instance of an object"
        if(myResultPropColl.Contains(credentials[i].ToString()))
        {
         //get all information entered for the current property
         foreach(Object myCollection in myResultPropColl[credentials[i].ToString()])
         {
          infoResult[i] = myCollection.ToString();
         }     
        }
        else
        {
         infoResult[i] = "-";
        }
       }
      } 
      return infoResult;
     }
     catch (Exception ex)
     {    
      infoResult[0] = ex.Message.ToString();
      return infoResult;
     }
    }

    Listen to Motörhead cos they play Rock'n Roll :)
  • 06-26-2007 07:23 AM In reply to

    Re: AD Web Service Solution

    Hi nukkumatti,

    This looks interesting - could you tell us a little about situations where it is useful and, also, how to use it?

    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 06-26-2007 11:03 PM In reply to

    Re: AD Web Service Solution

    Yes of course. I usually use it to receive all necessary information regarding a user when he/she opens the form (e.g. name, manager, department...) I usually call the Web Service in the OnLoad event using code and pass the credentials I want to receive as a string delimeted with "-". Actually it was planned to pass a complex type (string array) to the Web Service but somehow I couldn't manage this. May be someone knows how to do this. But anyway below is sample code you could use in your InfoPath template to get the users name and department. var obj = new ActiveXObject("WScript.NetWork"); var login = obj.UserName; var whatToGet = "name-department"; var objWSDataObject = XDocument.DataObjects("User Service"); objWSDataObject.DOM.setProperty("SelectionNamespaces", 'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:tns="http://tempuri.org/"'); objWSDataObject.DOM.selectSingleNode("dfs:myFields/dfs:queryFields/tns:getUserInfo/tns:account").text = login; objWSDataObject.DOM.selectSingleNode("dfs:myFields/dfs:queryFields/tns:getUserInfo/tns:whatToPass").text = "samaccountname"; objWSDataObject.DOM.selectSingleNode("dfs:myFields/dfs:queryFields/tns:getUserInfo/tns:whatToGet").text = whatToGet; objWSDataObject.Query(); XDocument.DOM.selectSingleNode("my:myFields/my:Name").text = objWSDataObject.DOM.selectSingleNode("dfs:myFields/dfs:dataFields/tns:getUserInfoResponse/tns:getUserInfoResult/tns:string[1]").text; XDocument.DOM.selectSingleNode("my:myFields/my:department").text = objWSDataObject.DOM.selectSingleNode("dfs:myFields/dfs:dataFields/tns:getUserInfoResponse/tns:getUserInfoResult/tns:string[2]").text;
    Listen to Motörhead cos they play Rock'n Roll :)
  • 06-26-2007 11:08 PM In reply to

    Re: AD Web Service Solution

    Ahhhhhhh I am sorry for the formatting but I saw that there have been user users facing the / similar problems already. Sometimes every line gets duplicated many times and sometimes the above happens. Hope you can still ready it.
    Listen to Motörhead cos they play Rock'n Roll :)
  • 02-25-2008 05:52 AM In reply to

    • zeroecho
    • Top 500 Contributor
      Male
    • Joined on 10-23-2007
    • Yorkshire, UK
    • Posts 39

    Re: AD Web Service Solution

    I am interested in how to get the Manager's email address from AD to add to my form so that when I send an email from the form it goes to the rigth person for approval.

    I am currently using the Context and ResolvePrinciples technique to populate the user's name and department fields (though that seems to have broken when I moved from WSS to MOSS...)

    I keep reading you need to use Webservices to access AD directly from c# code... I already have my project set up to do custom submitting and don't really want to change my project type. Do you know the c# "using..." reference I need to talk directly to AD or to query a user's profile from MOSS?

    Thanks,

    John

  • 02-26-2008 06:05 AM In reply to

    Re: AD Web Service Solution

    the using statement/reference you need to add should be System.DirectoryService.

    Listen to Motörhead cos they play Rock'n Roll :)
  • 04-21-2008 04:15 AM In reply to

    • zeroecho
    • Top 500 Contributor
      Male
    • Joined on 10-23-2007
    • Yorkshire, UK
    • Posts 39

    Re: AD Web Service Solution

    Will this method find a manager's email address? If I pass it the AD value "manager" and am expecting to get that user's email back, and set the "returnedvalue" to be "mail"  I just get error of "There is no such object on the server" so either my LPAD string is wrong, or I'm using your method wrong :)

Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.