Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom list for example "MyCustomListA" I have created a custom ListForm template and overide the ListFieldIterator. I have overide the ListFieldIterator and CreateChildControls to implement some readonly logica depandant of user membership.

One of the fields is a lookup to another custom list (MyCustomListB). In this custom list I am using a custom content type with 2 fields:

(MyCustomListB)

1: Title

2: Person or Group

The lookup shows a dropdownlist with for each item in "MyCustomListB" only the title. I would like to get the "Person or Group" value, someone know how??

public class CustomListFieldIterator : ListFieldIterator
{

    protected override void CreateChildControls()
    {
    // some logic to set readonly depandant by user membership    
    }

    protected override bool IsFieldExcluded(SPField field)
    {
                  // some logic
                  if(field.InternalName.Equals("UsersAndGroups"))
                    {
                        // get users and groups from this field
                        SPUser selectedUsers = field.Some logic...
                        SPGroup selectedUserGroups = field.Some logic...
                        // some logic
                    }

    }

}
share|improve this question
    
in which moment do you want to get this value, after user pressed "Save" in new\edit form? –  Kai Feb 10 '13 at 20:41
add comment

1 Answer

This should do it for you:

SPFieldUser userField = (SPFieldUser)item.Fields.GetField("UsersAndGroups");
SPFieldUserValue userFieldValue = (SPFieldUserValue)userField.GetFieldValue(item["UsersAndGroups"].ToString());
SPUser user = userFieldValue.User;

If you want more examples (e.g. when using multi-value selections) see here http://dsen-25.blogspot.ch/2012/07/how-to-get-spuser-or-spgroup-from.html

[EDIT after Comments]: If the UserAndGroups column is actually pointing to another list having one column of type "Person or Group" you need to use something like expressed here Get related items from a parent list item through lookup

share|improve this answer
    
Thanks for your support.. what do I need to fill in as currentValue?? Is it the column name from MyCustomListB? –  KHA Feb 10 '13 at 16:19
    
updated code with a piece from the article adapted to your case –  C. Marius - MVP Feb 10 '13 at 17:27
    
I dont have an item object in my code. How can I get current item in the IsFieldExcluded method? –  KHA Feb 10 '13 at 18:06
    
but you do, using this.Item as you are using the ListFieldIterator class msdn.microsoft.com/en-us/library/… –  C. Marius - MVP Feb 10 '13 at 18:15
    
are we talking about SPItem or SPListItem? –  KHA Feb 10 '13 at 19:44
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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