Getting and setting SharePoint form field values
Forms Designer provides JavaScript-framework that allows to manipulate fields in a simple JQuery manner. You can find how to set or get current field values in the official website.
But what if the field is more complex than a simple input, what if it has multiple parts of values, like lookup or date and time? How do you get lookup id and lookup text? Below I placed the table that illustrates how you get or set different value types for the most SharePoint types of fields. To create this table I used the current last version of Forms Designer: 2.7.9.
Field Type | Get / Set | Script |
Single Line of Text | Get | fd.field('Title').value(); |
Set | fd.field('Title').value('Default title'); | |
OnChange | fd.field('Title').change(function(){}); | |
Multiline Plain Text Field | Get | fd.field('MultipleLinePlain').value(); |
Set | fd.field('MultipleLinePlain') .value('MultipleLinePlain'); | |
OnChange | fd.field('MultilinePlain').change( function(){alert('Field has been changed!')} ); | |
Multiline RTF Field | Get | fd.field('MultilineRichText').value(); |
Set | fd.field('MultilineRichText') .value('MultilineRichText'); | |
OnChange | fd.field('MultilineRichText').change( function(){alert('Field has been changed!')} ); | |
Multiline Enhanced Rich Text Field | Get | fd.field('MultilineEnhanced').value(); |
Set | fd.field('MultilineEnhanced') .value('MultilineEnhanced'); | |
OnChange | fd.field('MultilineEnhanced').change( function(){alert('Field has been changed!')} ); | |
Choice Single | Get | fd.field('ChoiceSingle').value(); |
Set | fd.field('ChoiceSingle').value('B'); | |
OnChange | fd.field('Choice').change(function(){ alert('Field changed!'); }); | |
Choice Multiple | Get | var checkboxIndex = 2; fd.field('MultiChoice').control()._el() .find('input[type="checkbox"]').eq(checkboxIndex) .is(':checked'); |
Set | var checkboxIndex = 2; fd.field('MultiChoice').control()._el() .find('input[type="checkbox"]').eq(checkboxIndex) .prop('checked', true); | |
OnChange | fd.field('MultiChoice').change(function(){ alert('Field changed!'); }); | |
Number | Get | fd.field('Number').value(); |
Set | fd.field('Number').value('13'); | |
OnChange | fd.field('Number').change(function(){ alert('Field changed!'); }); | |
Currency | Get | fd.field('Currency').value(); |
Set | fd.field('Currency').value('23'); | |
OnChange | fd.field('Currency').change(function(){ alert('Field changed!'); }); | |
Date | Get | fd.field('Date').value(); |
Get Date-object | field('Date').control('getDate') // returns Date-object | |
Set | fd.field('Date').value('4/21/2012'); // by String fd.field('DateTime') .value(new Date()); // by Date-object | |
OnChange | fd.field('Date').change(function(){ alert('Field changed!'); }); | |
DateTime | Get | fd.field('DateTime').value()[0]; // date fd.field('DateTime').value()[1]; // hours fd.field('DateTime').value()[2]; // minutes |
Get Date-object | field('DateTime').control('getDate') // returns Date-object | |
Set | fd.field('DateTime') .value(['4/21/2012', '11 PM', '35']); // by Array fd.field('DateTime') .value(new Date()); // by Date-object | |
OnChange | fd.field('DateTime').change(function(){ alert('Field changed!'); }); | |
Hyperlink / Image | Get | fd.field('Hyperlink').value()[0]; // link fd.field('Hyperlink').value()[1]; // descr |
Set | fd.field('Hyperlink') .value(['http://someurl', 'description']); | |
OnChange | fd.field('Hyperlink').change(function() { alert('Changed!'); }); | |
Single Lookup Value | Get Text | fd.field('Lookup').control('getSelectedText'); |
Get ID | fd.field('Lookup').value(); | |
Set Text | fd.field('Lookup').value('Text'); | |
Set ID | var ID = 4; fd.field('Lookup').value(ID); | |
OnChange | fd.field('Lookup') .change(function(){}); | |
Multi Lookup Values | Get All | var selectedItems = fd.field('LookupMultiple') .control()._el().find("select:eq(1) option"); var s = ''; for (var i = 0; i < selectedItems.length; i++) { s += selectedItems.eq(i).text() + ';'; } alert(s); |
Get First | fd.field('MultiLookup').control()._el() .find("select:eq(1) option").eq(0).text() | |
Get Second | fd.field('MultiLookup').control()._el() .find("select:eq(1) option").eq(1).text() | |
Get ID | // first selected: fd.field('MultiLookup').control()._el() .find('select:eq(1) option').eq(0).val(); // second selected: fd.field('MultiLookup').control()._el() .find('select:eq(1) option').eq(1).val(); | |
Set | var ID = 3; // select element by ID: fd.field('MultiLookup').control()._el() .find('select:eq(0)').val(ID); // add button click: fd.field('MultiLookup').control()._el() .find('button:eq(0)').click(); | |
OnChange | fd.field('MultiLookup').change(function(){ alert('Field changed!'); }); | |
Boolean | Get | fd.field('Boolean').value(); |
Set | fd.field('Boolean').value('1'); | |
OnChange | fd.field('Boolean').change(function(){ alert('Field changed!'); }); | |
Client People Picker | Get All Logins | fd.field('User').control('ready', function() { var selectedUsers = fd.field('User').value(); for (var i = 0; i < selectedUsers.length; i++) { alert('Login: ' + selectedUsers[i].Key); } }); |
Set | // assign value by a display name fd.field('User').value('Tim Watts'); // by a login fd.field('User').value('DEV\\ann'); // or by an e-mail: fd.field('User').value('AGibson@contoso.com'); | |
OnChange | fd.field('User').change(function(){ alert('Field changed!'); }); | |
Server People Picker with multiple choice | Get All Logins | var logins = ''; $.each(fd.field('People').value() .dictionaryEntries, function() { logins += this.AccountName + '; '; }); |
Set | fd.field('People').value('DOMAIN\\login'); | |
OnChange | fd.field('People').change(function(){ alert('Field changed!'); }); | |
Server People Picker with single choice | Get | fd.field('PersonGroupSingle') .value().dictionaryEntries[0].AccountName |
Set | fd.field('Person').value('DOMAIN\\login'); | |
OnChange | fd.field('Person').change(function(){ alert('Field changed!'); }); | |
External Data | Get | Returns field value of External List item:fd.field('ExternalData').value() .dictionaryEntries[0]['FieldOfExternalList'] ‘ExternalData’ is an internal name of the lookup column. If you include additional columns to the target list in lookup settings you will be able to get them dynamically as well: fd.field('ExternalData').value() .dictionaryEntries[0]['AdditionalFieldOfExternalList'] ‘AdditionalFieldOfExternalList’ is an internal name of the external list column which has been added as additional field in lookup settings. |
Set | fd.field('ExternalData') .value({key: '__bg40003300', text: 'Item 1'}); key is BDC Identity of the selected item. | |
OnChange | fd.field('ExternalData').change(function(){ alert('Field changed!'); }); | |
Plumsail Cross-site Lookup with single value | Get ID | fd.field('CrossSiteLookup').value() |
Get title | fd.field('CrossSiteLookup').control('data')['Title'] | |
Set | Set ID:fd.field('CrossSiteLookup').value(1); Set ID and display text: fd.field('CrossSiteLookup') .value({Id: 1, Title: 'Item 1'}); | |
OnChange | fd.field('CrossSiteLookup').change(function(){ alert('Field changed!'); }); | |
Plumsail Cross-site Lookup with multiple values | Get IDs | Returns an array if selected ids:fd.field('CrossSiteLookup').value() |
Get titles | First item:fd.field('CrossSiteLookup').control('data')[0]['Title'] Second item: fd.field('CrossSiteLookup').control('data')[1]['Title'] | |
Set | fd.field('CrossSiteLookup') .value([{Id: 1, Title: 'Item 1'}, {Id: 2, Title: 'Item 2'}]) | |
OnChange | fd.field('CrossSiteLookup').change(function(){ alert('Field changed!'); }); |