Get or Set value for SharePoint field in NewForm, EditForm and DispForm (Get only in DispForm) By Alexander Bautz


This article describes how to get or set the value for all standard SharePoint field types in in NewForm, EditForm and DispForm (Get only in DispForm). This solution is tested in IE8, Firefox 3.6.3.

Instructions for getFieldValue

The function “getFieldValue” takes four arguments:
  • fin: FieldInternalName of the field to set the value for.
  • dispform: true if used in DispForm. Defaults to false if omitted.
  • multiValueJoinBy: String to insert as separator between multiple returned values. If omitted, separated by a comma.
  • optionalFilter: Used for “SPFieldUser” and “SPFieldUserMulti” only. If set to “displayName” or omitted, returns the displayName for the user(s), if set to “loginName”, returns the loginName for the user(s).
getFieldValue – example code in CEWP placed below the form:
01
02
03

Instructions for setFieldValue

The function “setFieldValue” takes three arguments:
  • fin: FieldInternalName of the field to set the value for.
  • newVal: the new value.
  • onLoad: true or false to indicate whether the value is set on page load. Default value if omitted is true.
To set the value you specify the new value in the parameter “newVal”. The following fieldTypes require some extra formatting on the parameter “newVal”:
  • SPFieldMultiChoice: “newVal” is a array containing the values to select.
  • SPFieldUserMulti: “newVal” is a string with the names (LoginName, DisplayName or Email-address) separated by a semicolon (;).
  • SPFieldLookupMulti: “newVal” is a array containing the values to preSelect. Note: Setting this field is only supported onLoad.
  • SPFieldBoolean: Set to true by passing 1,”1″, or true in newVal.
  • SPFieldURL: “newVal” is a array containing the URL and the description for the hyperlink.
  • SPFieldDateTime: “newVal” is a array containing the date, the hour (with AM/PM), and the minutes. Specify only the parameters to set (for date only, add only one item to the array).
setFieldValue – example code in CEWP placed below the form:
1
2
3
As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):
IMG
The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.4.2.min. If you download another version, please be sure to update the script “src”.
Sourcecode for “spjs-Utility.js”:
Download here
Save as “spjs-utility.js”, mind the file extension, and upload to the scriptlibrary as shown above.

Popular posts from this blog

Register Event Handlers plus free Site Settings – Manage Event Handlers Add-on solution

Custom Form Base Login Web Part With Sharepoint 2010

Real World Branding with SharePoint 2010 Publishing Sites