简体   繁体   中英

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.

我要这个代替这个领域

Thank you.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM