I’m wondering how in SharePoint list when someone creates a new item their username automatically populates? Is there a function already built into share point list?
Thanks!
1
GetCurrentUser ($().SPServices.SPGetCurrentUser) in peoplepicker (JQuery), using Content Editor add NewForm.aspx.
<script type="text/javascript" src="/sites/test/SiteAssets/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="/sites/test/SiteAssets/jquery.SPServices-2014.02.min.js"></script>
<script src="/_layouts/15/clientpeoplepicker.js" type="text/javascript"></script>
(function () {
var ctx = {};
ctx.Templates = {};
ctx.Templates.Fields = {
'Applicant': {
'NewForm': renderAssignedTo
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();
function renderAssignedTo(ctx) {
var currentUser = $().SPServices.SPGetCurrentUser({fieldNames: ["Title", "Name"]});
var currentUserEntry = createUserEntity(currentUser.Name,currentUser.Title);
//Set user default value
ctx.CurrentFieldValue = [];
ctx.CurrentFieldValue.push(currentUserEntry);
return SPClientPeoplePickerCSRTemplate(ctx);
}
function createUserEntity(userLoginName,userDisplayName)
{
return {
Description: userLoginName,
DisplayText: userDisplayName,
EntityGroupName: "",
EntityType: "",
HierarchyIdentifier: null,
IsResolved: true,
Key: userLoginName,
MultipleMatches: [],
ProviderDisplayName: "",
ProviderName: ""
};
}
make a SharePoint FormApp (via Integrate > Power Apps > Customize Forms). If the form is a canvas app, you can modify easily the default values of the fields.
Whether you use SharePoint server or SharePoint online?
For SharePoint server, add a content editor web part in the New form, then use following codes in the content editor web part.
<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var user= $().SPServices.SPGetCurrentUser();
$("div[title='People Picker']").text(user);
});
</script>
For SharePoint online, refer this article to auto populate logged In username in the PowerApps form.