Skip to content

Instantly share code, notes, and snippets.

@rheid
Created March 16, 2015 13:55
Show Gist options
  • Save rheid/d845826ceb106d8ae330 to your computer and use it in GitHub Desktop.
Save rheid/d845826ceb106d8ae330 to your computer and use it in GitHub Desktop.
SharePoint 2013 Set People Pick Field to CurrentUser (JQuery, SPServices)
(function () {
var ctx = {};
ctx.Templates = {};
ctx.Templates.Fields = {
'Schulungsteilnehmer': {
'NewForm': renderAssignedTo
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();
function renderAssignedTo(ctx) {
//get current user properties
var currentUser = $().SPServices.SPGetCurrentUser({fieldNames: ["Title", "Name"]}); //get current user properties
var currentUserEntry = createUserEntity(currentUser.Name,currentUser.Title);
//Set user default value
ctx.CurrentFieldValue = []; //Note: it is assumed the user field is aaa multi-valued field (!)
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: ""
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment