Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to show multiple properties in webservice 3

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
I have a simple web service which enables me to search for user information in active directory. Initially, I was only showing 'displayName', but now I need to show at least 4 more properties. I would like to load the properties in an array, but this does not seem to work as I want to return a the array value. Is it possible to return an array for the properties? Or should I create separate functions within the webservice for these properties? Thanks in advance.

This is what I have:

Code:
public string getUserInfo(string user) 
    {
        string adpath = "LDAP://path information here";

        try
        {
            DirectoryEntry entry = new DirectoryEntry(adpath);
            DirectorySearcher dsearch = new DirectorySearcher(entry);

            String account = user.ToString();

            user = this.User.Identity.Name.ToString();

            dsearch.Filter="(&(objectClass=User)(sAMAccountName="+ account +"))";
            
            dsearch.PropertiesToLoad.Add("displayName");            
            dsearch.PropertiesToLoad.Add("l");

            dsearch.PropertiesToLoad.Add("departmentName");

            SearchResult myresult = dsearch.FindOne();

            if (myresult != null)
            {
                return myresult.Properties["displayName"][0].ToString() + myresult.Properties["l"][1].ToString();
            }
            else
            {
                return "Unknown User";
            }
        }
        catch (Exception ex)
        {
            string debug = ex.Message;
            return "ERROR LOADING THE DATA: " + debug;
        }
    }
 
I tried to implement this and it doesn't work. There's four properties that I want to return, I can't figure out how to correctly pull them from an array as a return value.
 
Um, how didn't it work?

I would discourage using an array for this, but if you show the code you are using to try and return the array I'll do my best.

Good Luck,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Are all the properties of the array of the same type? Such as string?

Otherwise, I suggest using a Data Transfer Object (the UserInfo class that AlexCuse mentioned), as that's what it's purpose is -- to bundle up related data to transfer to or from a remote method call.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you so much. I created a class classed Employee and compiled it successfully. However, I am having problems implementing the class accordingly within my webservice. My earlier code contained a search string within my method. It seems I can not have the search method and my class simulataneously. If I add just the user string, then 'employee' does not exists. Please advise.

Code:
[WebService(Namespace = "[URL unfurl="true"]http://CORP481-GX745/ADUserInfo/",[/URL] Description = "BETUSerInfo is a service that provides the ability to search for users in the company's Active Directory")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class userInfo : System.Web.Services.WebService {

    public userInfo () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(Description = "The following method generates user information from active directory")]

    public EmployeeLibrary.Employee getInfo(string user)
    { 

            string adpath = "LDAP://domain controller info";

            DirectoryEntry entry = new DirectoryEntry(adpath);
            DirectorySearcher dsearch = new DirectorySearcher(entry);
            
            SearchResult myresult = dsearch.FindOne();

            String account = user.ToString();
            user = this.User.Identity.Name.ToString();
            dsearch.Filter = "(&(objectClass=User)(sAMAccountName=" + account + "))";

            
            employee.DisplayName = myresult.GetDirectoryEntry().Properties["displayName"].Value;
            employee.Title = myresult.GetDirectoryEntry().Properties["Title"].Value;
            employee.Location = myresult.GetDirectoryEntry().Properties["l"].Value;
            employee.Department = myresult.GetDirectoryEntry().Properties["department"].Value;
            employee.DeparmentCode = myresult.GetDirectoryEntry().Properties["departmentNumber"].Value;
            employee.Telephone = myresult.GetDirectoryEntry().Properties["telephone"].Value;

           
            if (myresult != null)
            {
                return employee;
            }
            else
            {
                return "Unknown User";
            }

    }
    
}
 
At a glance, it looks like you need to instantiate "employee".

Before assigning anything, this:

Code:
EmployeeLibrary.Employee employee = new EmployeeLibrary.Employee()

Then you will need to ALWAYS return "employee"

Just assign one of the values to contain "Unknown User" if nothing is found.

You can't return a string in one case and an object in another.

Make sense?

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
and if I try the following, I get:

ERROR

Cannot implicitly convert type 'EmployeeLibrary.Employee' to string

here's my updated code in my webservice:

Code:
public string getUserName(string user)
    {           
        EmployeeLibrary.Employee employee = new EmployeeLibrary.Employee();
        employee.DisplayName = employee.DisplayName;
        employee.Title = employee.Title;
        employee.Location = employee.Location;

            string adpath = "LDAP://PLACE DOMAIN CONTROLLER HERE";

            DirectoryEntry entry = new DirectoryEntry(adpath);
            DirectorySearcher dsearch = new DirectorySearcher(entry);
            
            SearchResult myresult = dsearch.FindOne();

            String account = user.ToString();
            user = this.User.Identity.Name.ToString();
            dsearch.Filter = "(&(objectClass=User)(sAMAccountName=" + account + "))";
  
            employee.DisplayName = myresult.GetDirectoryEntry().Properties["displayName"].Value.ToString();
            employee.Title = myresult.GetDirectoryEntry().Properties["Title"].Value.ToString();
            employee.Location = myresult.GetDirectoryEntry().Properties["l"].Value.ToString();

            if (myresult != null)
            {
                return employee;
            }
            else
            {
                return "Unknown User";
            }

    }
    
}
 
alexcuse,

you mentioned that I need to instantiate 'employee'?

<
At a glance, it looks like you need to instantiate "employee".

Before assigning anything, this:

CODE
EmployeeLibrary.Employee employee = new EmployeeLibrary.Employee()

Then you will need to ALWAYS return "employee"

Just assign one of the values to contain "Unknown User" if nothing is found.
>

so, is it best to create a string within my class class: 'Unknown user'?

if i am instantiating the employee class as


EmployeeLibrary.Employee employee = new EmployeeLibrary.Employee();

I still need to return data...
 
no, you'd just set one of your properties (like "DisplayName") to "Unknown User". Or just return the employee with everything null, and then handle nulls on the client. That would probably be easiest.

You also need to change function type (from string to EmployeeLibrary.Employee)

Something along these lines:

Code:
public EmployeeLibrary.Employee getUserName(string user)
{ 
EmployeeLibrary.Employee employee = new EmployeeLibrary.Employee();
employee.DisplayName = employee.DisplayName;
employee.Title = employee.Title;
employee.Location = employee.Location;

string adpath = "LDAP://PLACE DOMAIN CONTROLLER HERE";

DirectoryEntry entry = new DirectoryEntry(adpath);
DirectorySearcher dsearch = new DirectorySearcher(entry);

SearchResult myresult = dsearch.FindOne();

String account = user.ToString();
user = this.User.Identity.Name.ToString();
dsearch.Filter = "(&(objectClass=User)(sAMAccountName=" + account + "))";

employee.DisplayName = myresult.GetDirectoryEntry().Properties["displayName"].Value.ToString();
employee.Title = myresult.GetDirectoryEntry().Properties["Title"].Value.ToString();
employee.Location = myresult.GetDirectoryEntry().Properties["l"].Value.ToString();

return employee;

}

}


[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Thank you everyone! I am going to use classes more often. This is fabulous!
 
Makes it much easier to work with in your client app, right? Remembering those array positions would have been a big pain in the neck.

Glad you got it working :)

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
I'd think about having a null object for employee which would be inherited from the base employee class and has the properties overridden to return the 'blank' data. This is a standard technique and overcomes the needs that Alex describes to handling null. You actually return an object that has the same interface, eliminating the need to check. You ensure it does nothing with the overrides. The advantage of this is the predictability.

I enclose a link to Wikipedia, describing the technique.


Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top