Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 25, 2010 5:01 PM
Hello,
In Sharepoint 2010 I have a Custom List with a column of the type 'Person or Group'.
I would like to have a default name for this 'Person or group' column.
So when a user creates a new list entry and leves the 'Person or Group' column value blank the list entry should be created with the default value für the 'Person or Group' column.
Is there a way to schieve this?
Many thanks in advance.
Roberto Rocco.
All replies (9)
Monday, November 29, 2010 10:08 AM ✅Answered
Hi Roberto,
Thanks for your post.
You cannot set the default value for “Person or Group” column using the field properties. But there are some workarounds for this. For example:
1. Workflow. You can create a workflow associate to the list using SPD (SharePoint Designer 2010). And you set the column value using the workflow.
2. Event handler. You also can create an Event handler to the list. Then set it value using the ItemAdding or ItemUpdating method.
More information:
http://msdn.microsoft.com/en-us/library/ms475328.aspx
SharePoint 2010
Monday, November 29, 2010 11:22 AM ✅Answered | 2 votes
The two methods that Wayne presented are good, but the default values are set after you save the item (sort of, if you do not set anything for the columns when you save the item, the default values will be automatically set by the workflow/event handler).
If you need that the default values are set when the new item form opens, then you need javascript/jQuery on the New Item form.
Also, even if there is no browser option for it, you can set the default value using C# code, here is what you can do for a User/Group field:
using(SPSite site = new SPSite("site url"))
{
SPWeb web = site.RootWeb;
SPList list = web.Lists["My List with the field"];
SPField fld = list.Fields["My User Or Group Field"];
SPUser usr = web.EnsureUser("DOMAIN\\login");
SPFieldUserValue defValue = new SPFieldUserValue();
defValue.LookupId = usr.ID;
fld.DefaultValue = defValue.ToString();
fld.Update();
list.Update();
}
Let me know if you need more help with this.
PS: I guess you can even use SharePoint Manager to set the defualt value (you just need to correctly set the string equivalent like this user_ID#;)
Florin DUCA - MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006 - Logica Business Consulting, France
Tuesday, December 14, 2010 12:10 PM
Hi Florin,
In the example above, you state the following "If you need that the default values are set when the new item form opens, then you need javascript/jQuery on the New Item form."
I can browse to "Form Web Parts" then "Default New Form", where I can see the insert new item view that I have use a content type to customise. All I need to do is set the Assigned To to be a default value. Is there any way that I can do this from this screen using jQuery?
Thanks,
Ben Grice
Wednesday, December 15, 2010 8:51 AM
There are quite a few frameworks/samples that allow you to work with SharePoint forms.
Here are some examples:
- MSDN post that talks about javascript on list item forms (no jQuery) but it is really nice post http://blogs.msdn.com/b/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx
- samples http://www.endusersharepoint.com/2009/04/20/jquery-for-everyone-pre-populate-form-fields/ and http://efreedom.com/Question/1-1090274/Can-Set-Default-Value-SharePoint-List-Field-Based-Value-Another-Field
- frameworks http://spff.codeplex.com/ and http://jpoint.codeplex.com/
PS1: most of the samples out there are for MOSS (SP 2007) but it's basically the same for SP 2010
PS2: why don't you want to set the default value using code (or SharePoint Manager for example ... or powershell if you're really keen on that). In my opinion it's the simplest option, simple to package ina feature and does not require customisation of the list item forms.
Florin DUCA
MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
Logica Business Consulting, France
Sunday, January 2, 2011 6:52 PM
Hi Florin,
I tried to insert your code into a new created NewForm (in SPD 2010). But the part where to put it is blocked it seems. I assume I have to put the javascript under (?):
<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script type=”text/javascript”>
<!-- Your Script -->
</script>
</asp:Content>
But this placeholder is situated under a part of de formcode that cannot be changed. Only within the zonetemplate I can put this code, but then it won't work.
How should I do this?
Regards, Ronald
Tuesday, January 4, 2011 8:20 AM
Plenty2do, try doing an :
alert('inside JS')
to see if the JS is being run. If you leave it as is maybe the JS will run but before the page (so the list fields) finish loading. If I remember correctly you had to wrap your JS in a function and then push this function to be called after page linished loading (_spBodyOnLoadFunctionNames.push).
Florin DUCA
MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
Logica Business Consulting, France
Thursday, March 1, 2012 6:14 AM
You can add a Content editor web part and use your javascript using html editor.
Please make sure, you place your Content editor webpart after the List form web part where the control is available.
Thursday, June 14, 2012 3:54 PM
You can do it in InfoPath.
- From the Data tab, choose Form Load. This runs a rule/action as the form loads.
- Create a rule to Set A Field's Value
- In "Select a Field or Group", you won't see your Person field. Click Show Advanced View link, just above the OK button.
- In the Advanced view, select the AccountId field for your person field.
- I wanted to default the Value to the current username. To do this, click Insert Formula.
- Enter userName() as the formula.
Friday, July 6, 2012 4:07 PM
Why couldn't you just use the default 'created' column - it's already got the value you want and if you wanted to use it in another column just create a workflow to trigger on create and update your field with the created field value?