0

I'm tring to programatically update various AD properties from a c# webservice.

I can update phone / office / title etc no problem, but when I try to update the ThumbnailPhoto I get the error: The specified directory service attribute or value already exists

I am updating via an extended class (as per Get job title using System.DirectoryServices.AccountManagement)

The code I'm using to update the AD record is:

public ADRecord UpdateADRecord(string UserName)
    {
        DeltekRecord dr = GetDeltekRecord(UserName);
        UserPrincipalEx upx = GetUser(UserName);

        upx.Office = GetFriendlyFieldName(dr.Office);
        upx.MobilePhone = dr.MobilePhone;
        upx.TelephoneNumber = dr.WorkPhone;
        upx.Department = GetFriendlyFieldName(dr.BusinessUnit);
        upx.Title = dr.Title;

        // Handle Company 
        string Company = "";
        if ((dr.Org ?? "").Contains(":FOO:"))
            Company = "Foo";
        else
            Company = "Bar";
        upx.Company = Company;

        // Handle Home Phone
        string HomeNo = "";
        if ((dr.WorkPhone.Length > 0) && (dr.Office.Length > 0))
        {
            switch (dr.Office.ToLower()) {
                case "washington":
                    HomeNo = "8" + dr.WorkPhone.Substring(dr.WorkPhone.Length - 3, 3);
                    break;
                case "ohio":
                     HomeNo = "9" + dr.WorkPhone.Substring(dr.WorkPhone.Length - 3, 3);
                     break;
                case "nooyoik":
                    HomeNo = "2" + dr.WorkPhone.Substring(dr.WorkPhone.Length - 3, 3);
                    break;
                default: 
                    HomeNo = "";
                    break;
            }

        }

        upx.Thumbnail = null;
        upx.Thumbnail = GetThumbnail(dr.Employee);

        upx.Save();

        ADRecord adr = GetADRecord(UserName);
        return adr;
    }

Some Googling suggests I need to clear the thumbnail attribute first. Have tried setting it to null, as above, but no joy.

EDIT: ADDING METHODS I MISSED BEFORE

  // Get the relevant details from AD
    [WebMethod]
    public ADRecord GetADRecord(string userName)
    {
        ADRecord adr = new ADRecord();

        PrincipalContext oPrincipalContext = GetPrincipalContext();
        // Search the directory for the new object. 
        UserPrincipalEx myUser = UserPrincipalEx.FindByIdentity(oPrincipalContext, userName);

        if (myUser != null)
        {
            adr.Company = myUser.Company;
            adr.Department = myUser.Department;
            adr.HomePhone = myUser.HomePhone;
            adr.MobilePhone = myUser.MobilePhone;
            adr.Office = myUser.Office;
            adr.TelephoneNumber = myUser.TelephoneNumber;
            adr.ThumbNail = myUser.Thumbnail;
            adr.Title = myUser.Title;

        }
        return adr;

    }


     // Get the user's thumbnail
    [WebMethod]
    public byte[] GetThumbnail(int EmpNo)
    {
        System.Net.WebClient wclient = new System.Net.WebClient();
        wclient.Credentials = new System.Net.NetworkCredential("user","pass", "domain");

        string url = "http://myurl.mycompany.com/PeopleEmployeePhoto.ashx?Employee=" + EmpNo;
        byte[] imageData;
        using (wclient)
        {
            imageData = wclient.DownloadData(new Uri(url));
        }

        return imageData;

    }
Community
  • 1
  • 1
Ben
  • 4,281
  • 8
  • 62
  • 103
  • you have 2 methods below that you are not showing what those methods do ..which leads one to become a mind reader ... please show how the images are getting their values and or attributes from AD also can you wrap some of the code for example GetUserName perhaps there is an error that you are not trapping – MethodMan Dec 20 '12 at 15:49

1 Answers1

0

I'm not familiar with UserPrincipalEx, but I'm guessing that you have to call the Save function after you set the thumbnail to null to actually clear it at the server.

Sean Hall
  • 7,629
  • 2
  • 29
  • 44