Answered by:
set Default Value for People Picker in List Template current user

Question
-
Hi,
I have field definition in my custom list
<Field ID="{26763808-64BB-4A3C-93A0-ED45AF783D45}" Type="User" Name="RequestedBy" DisplayName="Requested By" Required="FALSE" ></Field>
I need to set default current user in this.
I found few options with javascript.
Thanks in Advance.
Where I can find on set of default commands like Today
Hari
Saturday, February 4, 2012 8:56 AM
Answers
-
Hi,
Default current user can’t be got in calculated column like Today. I suggest that use SPD Workflow described above or JavaScript. Open NewForm page of the list, then add “pageview=shared&toolpaneview=2” to the page URL, click Enter to go to edit mode. Then add a Content Editor Web Part to the bottom of NewForm page, copy the code bellow into Source Editor of CEWP.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://come:100/sites/collection1/Shared Documents/jquery.SPServices-0.7.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){var CurrUser=$().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false});
$('#ctl00_m_g_dab5800a_09ac_4f35_97f6_9b5496127f68_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv').html(CurrUser);
});
</script>
Note:
download jquery.SPServices-0.7.0.js here and upload it to your own site: http://spservices.codeplex.com/SourceControl/list/changesets, and then replace the route of jquery.SPServices-0.7.0.js above with the route in your site. ctl00_m_g_dab5800a_09ac_4f35_97f6_9b5496127f68_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv
is the id of the people-picker, please find it in your own NewForm page.
Another method, you can use Event Handler. Here is the example code.
public override void ItemAdded(SPItemEventProperties properties)
{
if (properties.ListTitle == "listname")//replace listname with your list.
{
SPWebcurrentWeb = properties.OpenWeb();
SPUseruser = currentWeb.SiteUsers.GetByID(properties.CurrentUserId);
SPListItemliitem = properties.ListItem;
SPFieldUserValuefieldUser = newSPFieldUserValue(currentWeb, user.ID, user.LoginName);
liitem["currentuser"] = fieldUser;//replace currentuser with your field name.
liitem.Update();
}
//base.ItemAdded(properties);
}
If anything unclear, please feel free to ask.
Thanks.
Emir Liu
TechNet Community Support
- Marked as answer by Emir Liu Thursday, February 16, 2012 2:45 AM
Friday, February 10, 2012 8:13 AM
All replies
-
Hi Hari,
After several trials, I suggest SharePoint Designer Workflow as a most convenient method. You can create a workflow for the custom list in SharePoint Designer, and select automatically start the workflow when a new item is created. Then select Update List Item to set the Requested By field as Created By or Modified By, and then finish the workflow. Now you can create a new item and the Requested By field will be auto-populated with Created By (the current user).
Here is a similar post:
http://social.technet.microsoft.com/Forums/en-US/sharepointgeneral/thread/b6898b14-3454-4787-b8df-fa7c4d8cabde/ .Thanks.
Emir Liu
TechNet Community Support
Monday, February 6, 2012 7:00 AM -
Hi Emir
I would like to do like this.
RequestedBy = SPContext.Web.CurrentUser
Is that possible
Thanks
Hari
Monday, February 6, 2012 10:56 AM -
Hi Hariram,
please have a look at this Blog Site you may find some use full Containt over there.
People Picker Current user value
Abhijeet S. Thorat
Moderator Note: Once again - do NOT propose your own posts. The next step is to delete them.- Proposed as answer by Abhijeet Thorat Tuesday, February 7, 2012 8:14 AM
- Unproposed as answer by Mike Walsh FIN Tuesday, February 7, 2012 9:13 AM
- Edited by Mike Walsh FIN Tuesday, February 7, 2012 9:14 AM Moderator Note added about not proposing own posts (which he has done before)
Tuesday, February 7, 2012 8:14 AM -
Hi,
Default current user can’t be got in calculated column like Today. I suggest that use SPD Workflow described above or JavaScript. Open NewForm page of the list, then add “pageview=shared&toolpaneview=2” to the page URL, click Enter to go to edit mode. Then add a Content Editor Web Part to the bottom of NewForm page, copy the code bellow into Source Editor of CEWP.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://come:100/sites/collection1/Shared Documents/jquery.SPServices-0.7.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){var CurrUser=$().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false});
$('#ctl00_m_g_dab5800a_09ac_4f35_97f6_9b5496127f68_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv').html(CurrUser);
});
</script>
Note:
download jquery.SPServices-0.7.0.js here and upload it to your own site: http://spservices.codeplex.com/SourceControl/list/changesets, and then replace the route of jquery.SPServices-0.7.0.js above with the route in your site. ctl00_m_g_dab5800a_09ac_4f35_97f6_9b5496127f68_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv
is the id of the people-picker, please find it in your own NewForm page.
Another method, you can use Event Handler. Here is the example code.
public override void ItemAdded(SPItemEventProperties properties)
{
if (properties.ListTitle == "listname")//replace listname with your list.
{
SPWebcurrentWeb = properties.OpenWeb();
SPUseruser = currentWeb.SiteUsers.GetByID(properties.CurrentUserId);
SPListItemliitem = properties.ListItem;
SPFieldUserValuefieldUser = newSPFieldUserValue(currentWeb, user.ID, user.LoginName);
liitem["currentuser"] = fieldUser;//replace currentuser with your field name.
liitem.Update();
}
//base.ItemAdded(properties);
}
If anything unclear, please feel free to ask.
Thanks.
Emir Liu
TechNet Community Support
- Marked as answer by Emir Liu Thursday, February 16, 2012 2:45 AM
Friday, February 10, 2012 8:13 AM -
This may helpful:
Thanks & Regards, Santhosh
Monday, July 2, 2012 10:02 AM -
Hi,
Below script works for me:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" src="http://come:100/sites/collection1/Shared Documents/jquery.SPServices-0.7.0.js"></script> <script type="text/javascript"> $(document).ready(function(){ var CurrUser=$().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false}); $('#ctl00_m_g_dab5800a_09ac_4f35_97f6_9b5496127f68_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv').html(CurrUser); }); </script>
However, I'd like to set default user for people picker only when user checks a checkbox.
I am new to development. Could you please help me on this requirement ?
Thank you very much.
Thursday, August 28, 2014 2:35 PM -
Thanks Emir Liu!Monday, January 16, 2017 1:39 PM
-
How do I get the #ct100_m_g.... number of the field.
I checked the NewForm.aspx looking for my field 'Reported By'. Within the code it shows f18($Pos) and "Reported_x0020_By"
Are either of these the same as the #ct100_m_g.... number you provided in your code?
Thanks
TD
- Edited by qnanglfsh Wednesday, May 2, 2018 7:00 PM
Wednesday, May 2, 2018 6:59 PM