How can I set a User field to a default value of a

2019-07-11 13:03发布

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.

4条回答
何必那么认真
2楼-- · 2019-07-11 13:08

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>
查看更多
Animai°情兽
3楼-- · 2019-07-11 13:14

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.

enter image description here

查看更多
地球回转人心会变
4楼-- · 2019-07-11 13:32

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.

查看更多
We Are One
5楼-- · 2019-07-11 13:35

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>=";#"&amp;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:

查看更多
登录 后发表回答