C# Crew - I'm getting my ass kicked here.....

Discussion in 'OT Technology' started by VD, Jan 10, 2008.

  1. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    I am not, nor do I claim to be a C# programmer, but I ended up getting a project that needs to be written in C#.

    It is a web part for windows sharepoint services 3.0 that displays active directory user information through LDAP (preferrably: full name, title, and physicalDeliveryOfficeName)

    Now, the names I've managed to get without problem - but the title and physicalDeliveryOfficeName (office location) I cannot seem to reference to save my life. This project is due on monday, and I'm seriously stuck on this part (which is vital for grouping the users)

    Code:
    using System;
    using System.Runtime.InteropServices;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;
    using System.DirectoryServices;
    using System.DirectoryServices.ActiveDirectory;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    
    
    namespace RBDirectory
    {
        [Guid("d1062d3e-3542-4668-babf-245ca230f52f")]
        public class RBDirectory : System.Web.UI.WebControls.WebParts.WebPart
        {
            public RBDirectory()
            {
                this.ExportMode = WebPartExportMode.All;
            }
    
            protected override void RenderContents(HtmlTextWriter writer)
            {
    
    
                //Setup LDAP bind
                DirectoryEntry entry = new DirectoryEntry("LDAP://ou=RB Global Group Policy,dc=domain,dc=com", "Administrator", "password");
                DirectorySearcher search = new DirectorySearcher(entry);
    
                search.CacheResults = true;  //I think this caches the data locally, so AD isn't abused too badly.  Will need to fix later.
                //search.PropertiesToLoad.Add("physicalDeliveryOfficeName");
                //search.PropertiesToLoad.Add("Title");
                search.Filter = "(&(objectCategory=person)(objectClass=user))";
    
                //Setup StringBuilder sb to dump the AD data into
                StringBuilder sb = new StringBuilder("");
    
    
                //For loop to return all the users in the objectCategory of "person"
                foreach (SearchResult result in search.FindAll())
                {
                    DirectoryEntry dirEntry = result.GetDirectoryEntry();
    
    
                    //Will eventually setup some variables to fill with data.  If I ever figure it out.
                    string name = dirEntry.Name.Remove(0, 3);
    
    /////////////////////////////////
    //This is where my problem is
    /////////////////////////////////
                    string location = result.Properties["physicalDeliveryOfficeName"].ToString();
    
                    //string location = "location goes here";
                                    
                    //Build the div containing each user                                
                    sb.Append("<div style='width: 850px; height: 140px; background-color:#eff7ff; margin: 5px; border: 2px solid #84b5ef; padding-top:12px; padding-left: 5px;' onMouseOver='this.style.backgroundColor=#F9F9F9'><img src='./noimage.gif' align=left>");
    
                    //Drop in the user info
                    sb.Append("<a href='#'><h2>" + name + " </h2></a><br /> <h4>" + location + "</h4><br /> </div> ");
    
    
                }
                base.RenderContents(writer);
    
                //Writes the data to the web part
                writer.Write(sb);
    
    
                //Closes the LDAP bind.
                entry.Close();
                entry.Dispose();
            }
    
        }
    }
    
    
    
    :squint: The Almighty had told me he's done this before and could hook me up with some sample code, put after 2 pms and an e-mail I haven't heard back from him :hsd:
     
  2. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Is it throwing an exception, or simply coming back as null? Are you positive that's the correct key for the property?
     
  3. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    you have this line commented out, you need to put it back in:

    search.PropertiesToLoad.Add ("physicalDeliveryOfficeName");


    to obtain the property you want:

    string location = result.Properties["physicalDeliveryOfficeName"][0].ToString();
     
  4. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    :hsd:
     
  5. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Code:
                    foreach (string key in dirEntry.Properties.PropertyNames)
                    {
                        foreach (object propVal in dirEntry.Properties[key])
                        {
                            if (key == "displayName") ;
                            {
                                sb.Append(key + " = " + propVal + "  ");
                            }
                            if (key == "physicalDeliveryOfficeName")
                            {
                                sb.Append(key + " = " + propVal + "<br />");
                            }
                        }
                    }
    
    other than probably being inefficient as fuck (I :dunno: ) should this work?

    For whatever reason, it seems to return all the values :wtf:

    I can get it to display just the physicalDeliveryOfficeName by itself, but as soon as I try getting it to search for another one, it just returns everything :mad::hsd:
     
    Last edited: Jan 11, 2008
  6. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    and why doesn't C# have something awesome like PHP's var_dump? :mad:
     
  7. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Code:
                        foreach (object propVal in dirEntry.Properties[key])
                       {
                           switch (key)
                           {
                               case "physicalDeliveryOfficeName":
                                   location = propVal.ToString();
                                   break;
    
                               case "name":
                                   name = propVal.ToString();
                                   break;
                               case "description":
                                   title = propVal.ToString();
                                   break;
                               case "sAMAccountName":
                                   username = propVal.ToString();
                                   break;
                           }
                     }
    
    This ended up working for me :o
     
  8. VD

    VD Active Member

    Joined:
    Aug 18, 2004
    Messages:
    71,510
    Likes Received:
    0
    Location:
    TWIN CITIES
    Jesus tapdancing christ...

    One roadblock opens to another..... ugh...

    I was originally planning on using a querystring setup to a web part on a separate page to handle the actual "viewing" of people's profile pages within the company directory and storing the information extra information about them in a ms sql database...... BUT.....

    turns out querystrings can't be used in between web part pages.... but... ya know, why the fuck would you be able to? because that would make my life easy. :mad:

    I'm fucking losing it.
     

Share This Page