问题
I'm trying to create an field definition in XML for a field of a User type and need to set its default value to a user currently logged in. How do I do that?
Thanks a lot in advance.
回答1:
Here is the closest I've ever come:
<Field ID="{99ced9dc-5715-47a7-9ede-1733f027eeb4}"
Name="MyField"
DisplayName="My Field"
Type="User"
List="UserInfo"
UserSelectionMode="PeopleOnly"
Required="TRUE">
<DefaultFormula>=";#"&Me</DefaultFormula>
</Field>
It doesn't blow up, but it doesn't give the desired result either. The problem is that Me
will give the name of the user, but since the User Field is a special type of Lookup Field, the name is ignored. All that is needed is the ID, but I haven't been able to find anything that will give me that value.
Here are some possible workarounds:
- Custom Field: Current user as default value for a SharePoint Person or Group field
- Event Receiver: Add current user to person field default value with event receiver ItemAdding–SharePoint
- Javascript: Current user as default value for a SharePoint Person or Group field
回答2:
I do not think so there is any constant for the current logged in user similar to [today]
constant for DateTime type. So you probably have to create your own field inherited from SPFieldUser and parse the default value constant introduced by you, e.g. [current]
. Then your field xml definition can look like
<Field Type="MyCurrentUser" Name="MyCurrentUser" ID="{...}">
<Default>[current]</Default>
</Field>
回答3:
This isn't XML, but you could create a workflow that operates when the item is created. It would check to see if that field was left empty and if it is, then it would add the current user as the value. I have done this and it works.
回答4:
I wrote a add-on(SharePoint Default Value Add-On), which inject a "default value" section into "Create Column" dialog.
Set "Current User" (User who is adding item) as default value.
来源:https://stackoverflow.com/questions/10343574/how-can-i-set-a-user-field-to-a-default-value-of-a-user-currently-logged-in-thro