How to insert 'Person or Group' field in list using VS2013?

I am using

string field = list.Fields.Add(StaticName, SPFieldType.User, true); SPFieldUser user = new SPFieldUser(list.Fields, field); user.AllowMultipleValues = allowMultiple; user.Required = Required; user.SelectionMode = mode; user.LookupField = "Name"; user.Update();

code sample to create SPUser type of field. It creates field perfectly fine but in default display value it gives employees' "Account" value instead of "Name" or "Name(with presence). How can I change this display value to "Name" pragmatically.

I want this Instead of this field

Thank you.

Upvotes: 0

Views: 1041

Answers (1)

Jan Vanek
Jan Vanek

Reputation: 889

You can achieve this using two ways:

Just after the creation get the field again and use it (overcast) as SPFieldLookup field because SPUserField is child of SPFieldLookup:

SPFieldLookup userfield = (SPFieldLookup)list.Fields["fieldname"];
Userfield.LookupField = "ImnName"; 
Userfield.Update();

or you can create the field using AddFieldAsXml method like this:

list.Fields.AddFieldAsXml(@"<Field Name='TypeUser' ID='{99bd898d-c181-4ca9-8397-c9fc032fcdf9}' DisplayName='TypeUser' Type='User' ShowField='ImnName' ></Field>");
list.Update();

you should also take a look at Presence property and set it to true.

Upvotes: 0

Related Questions