0

I have a List Tickets with a field Client. This is a user from our AD and displays the name of the user.

We now need to display the country and the location the user resides in to additional fields. Is it possible to add those fields in the form of calculated fields based on the user in the Client field? If yes - how?

If not: Could you guide me to the appropriate documentation or resources on how to implement this in the form of code. There are 2 eventhandlers active on this list, handling incoming mails and itemchanges, so I could place the logic there if need be, but right now I desperatly need a starting point, since I am totally lost.

This is a prototype of where I am so far:

SPWeb web = new SPSite("http://myserver/path/sitecollection").OpenWeb();
SPUser user = web.EnsureUser(@"domain\Servdev");
SPListItem item = web.SiteUserInfoList.Items.GetItemById(user.ID);

Console.WriteLine("{0} -- {1}",item.Fields[SPBuiltInFieldId.Department], item.Fields[SPBuiltInFieldId.Office]);

Console.ReadLine();

This displays the name of the fields I want to have, but not the values stored in it and I can't access them. Kind regards

//EDIT: Apparently, SPBuiltInFieldId.Office Guid is not valid across the board. So instead you'd have to use item["Office"] or the localized version of your site. (in my case item["Büro"] does work too.

But all in all, the answer given by Naim is totally correct. Cheers.

1

Try

Console.WriteLine("{0} -- {1}",item[SPBuiltInFieldId.Department] == null ? "N/A" : item[SPBuiltInFieldId.Department].ToString(), item[SPBuiltInFieldId.Office] == null ? "N/A" : item[SPBuiltInFieldId.Office].ToString());

In the event receivers you can set those two columns.

With calculated columns would be great but as far as I know that's not possible for your case.

  • This is the error I am getting: Invalid field name. {26169ab2-4bd2-4870-b077-10f49c8a5822} /project/sitecollection/_catalogs/users – Marco Nov 11 '13 at 10:50
  • then better go with UserProfileManager – Naim Murati Nov 11 '13 at 11:00
  • Office field doesn't have the same id on all webs and it's not equal to SPBuiltInFieldId.Office. You can take the value of this field just by it's name: item["Office"].ToString() – Evgenii Vilkov Nov 11 '13 at 11:03
  • I found the error, I am marking you as correct and edit my original post to clarify, where the excact problem was. – Marco Nov 11 '13 at 11:04
  • Evgeny, that was exactly what I just discovered myself. – Marco Nov 11 '13 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.