I now understand the technique used to embed js into a form;
- upload a js file to site assets
- add content editor web part (CEWP) to form.
- then link the js file to CEWP.
The full js code used follows the asp:Content line;
<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript">
// Following js code is used to auto-populate Business Owner (ff31) with current user.
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
function init(){
this.clientContext = new SP.ClientContext.get_current();
this.oWeb = clientContext.get_web();
currentUser = this.oWeb.get_currentUser();
this.clientContext.load(currentUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}
function onQuerySucceeded() {
// alert(currentUser.get_loginName());
// populate Business Ower People picker control id 'ff31'
setPickerInputElement('ff31',currentUser.get_loginName());
}
function onQueryFailed(sender, args) {
// alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
// Marc D anderson - http://sympmarc.com/2008/06/06/set-a-people-pickers-value-on-a-form/
// setPickerInputElement: Set a People Picker's value using its identifier to find it in the page
// Arguments:
// identifier: The identifier for the instance of the fieldName (ff1, ff2, etc.)
// value: Set the fieldName to this value
//
function setPickerInputElement(identifier, value) {
var tags = document.getElementsByTagName('DIV');
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
//alert('tags[' + i + '].id = ' + tempString);
if ((tempString.indexOf(identifier) > 0) && (tempString.indexOf('UserField_upLevelDiv') > 0)){
// alert('HIT for ' + identifier + ' id=' + tags[i].id + ' value=' + tags[i].value + ' tags[i].innerHTML:' + tags[i].innerHTML );
// Only popoulate on initial load. Empty field will contain ' ', but once populated, full html is approx 130 chars long.
// so thi scaters for user changing, but script NOT overwriting during form validation failure postback.
if ( tags[i].innerHTML.length < 10 ) {
tags[i].innerHTML = value;
}
break;
}
}
}
</script>