← Built-in features

Autocomplete

A search box with suggestions from another list, on a text field or a people picker. Built into Classic DFFS; set up in Custom JS. Applies to Autocomplete v1.6.56 in DFFS v4.4.5.56.

What it does

Autocomplete turns a single line of text field (or a multiple lines of plain text field, for several selections) into a search box: as the user types, it suggests matching items from another list, and can fill in other fields from the chosen item. It can also replace a people picker’s search. It runs in NewForm and EditForm, not in DispForm.

  • Set it up by calling spjs.ac.textField(…) or spjs.ac.peoplePicker(…) in the form’s Custom JS. The Autocomplete sub-tab under Plugins in the DFFS configuration only points here.
  • The chosen value is stored as text. Unlike a SharePoint lookup column it does not link to the source item, and it does not change if the source item changes later.

How it looks

Suggestions while typing
Suggestions while typing.
A valid selection
A valid selection.
An invalid value
A value that matches no item.
Several selections
Several selections (multiselect).

Autocomplete in a text field

spjs.ac.textField({
    "applyTo": "Project",
    "helpText": "Type a project name …",
    "loadText": "",
    "listGuid": "Projects",
    "listBaseUrl": "/sites/pmo",
    "showField": "Title",
    "searchFields": [],
    "filterCAML": "",
    "useREST": true,
    "preloadData": false,
    "filterREST": "",
    "optionDetailFields": ["ProjectNumber"],
    "optionDetailPrefix": ["Number: "],
    "enforceUniqueValues": true,
    "rowLimit": 15,
    "listOptionsOnFocus": false,
    "minLengthBeforeSearch": 3,
    "reValidateOnLoad": false,
    "allowAddNew": false,
    "addToFolder": null,
    "isLookupInSelf": false,
    "addNewAdditionalFields": [],
    "multiselect": false,
    "multiselectSeparator": "; ",
    "orderBy": [],
    "clearSetFieldsOnInvalidSelection": false,
    "setFields": [{ "fromFIN": ["ProjectNumber"], "joinBy": "", "toFIN": "ProjectNo", "parseFunction": "", "skipIfEmpty": false }],
    "debug": false
});

Text field options

OptionTypeWhat it does
applyTostringThe FieldInternalName of the field to add autocomplete to.
helpTextstringPlaceholder text in the empty field.
loadTextstringText shown while a large REST data set loads (with several searchFields).
listGuidstringGUID or display name of the source list. Use the GUID if list names are translated. A list’s GUID is _spPageContextInfo.pageListId in the browser console on one of its views.
listBaseUrlstringServer-relative URL of the source list’s site, for example "/sites/pmo"; "" for the root site (_spPageContextInfo.webServerRelativeUrl in the console).
showFieldstringThe field shown and searched. To search on two values (name and number), make a calculated column that joins them, or use searchFields.
searchFieldsarrayMore fields to search in, while still showing showField.
filterCAMLstringA CAML condition that limits the source items, for example the current user’s items: <Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'><UserID/></Value></Eq>. {function:myFilter} calls a function in Custom JS for each search; it must return the CAML.
useRESTbooleanQuery the list with REST, without blocking the page.
preloadDatabooleanWith useREST: load all items once for faster searching. The search then also understands "exact text" in double quotes, and OR (upper case) between words; otherwise words separated by spaces must all match.
filterRESTstringA REST filter that limits the source items, for example startswith(Title,'t'). {function:myFilter} works as for filterCAML.
optionDetailFieldsarrayExtra fields shown with each suggestion to help pick the right item. They are not saved; use setFields to copy them into the form.
optionDetailPrefixarrayLabels for optionDetailFields, one per field, for example ["Number: ", "Owner: "].
enforceUniqueValuesbooleanShow each value only once.
rowLimitnumberThe most suggestions to fetch; keep it low for speed.
listOptionsOnFocusbooleanShow suggestions as soon as the field gets focus, like a dropdown with an arrow.
minLengthBeforeSearchnumberHow many characters before the search starts.
reValidateOnLoadbooleanIn EditForm, check that the saved value still matches exactly one source item (not with multiselect). A value whose item was deleted, or that matches several items, shows as invalid.
allowAddNewbooleanOffer to add a typed value that matches nothing to the source list. When isLookupInSelf is false, listGuid must be the list’s GUID.
addToFolderobject or nullWith allowAddNew (not in self): add new items to a folder, created if missing: {"rootFolder": "/sites/pmo/Lists/Projects", "folderName": "Folder A"}.
isLookupInSelfbooleantrue when the source list is the form’s own list (used with allowAddNew).
addNewAdditionalFieldsarrayWith allowAddNew: copy form fields into the new source item, [{"fromFIN": "ProjectNumber", "toFIN": "PNumber"}].
multiselectbooleanLet users choose several items (use a multiple lines of plain text field). setFields cannot be used with it.
multiselectSeparatorstringWhat separates the choices in the stored text; "; " by default.
orderByarraySort order, for example [{"fin": "Created", "ascending": true, "numeric": false, "parseFunction": ""}]. numeric sorts numbers as numbers. parseFunction names a function in Custom JS that turns each value into something sortable; it receives the value.
clearSetFieldsOnInvalidSelectionbooleanClear the setFields fields when the user leaves the field without a valid choice.
setFieldsarrayFill in other form fields from the chosen item (below).
debugbooleanShow debug information while you set it up.

setFields

Each entry copies from the chosen source item into a form field:

  • fromFIN: array of source fields; several are joined into one value with joinBy ("<br>" for a rich text field, "\n" for plain text).
  • toFIN: the form field to fill in.
  • parseFunction: name of a function in Custom JS that receives the value and the toFIN field name and returns the value to set.
  • skipIfEmpty: leave the form field alone when the source value is empty.
// Custom JS: turn a source date into [date, hour, minute] for a date field.
// Write the date in the form's own date format (here MM/dd/yyyy).
function toFormDate(value, fin) {
    var d = new Date(value);
    return [spjs.dffs.formatDate(d, "MM/dd/yyyy"), d.getHours(), d.getMinutes()];
}

Autocomplete in a people picker

SharePoint 2013 and later already have a searching people picker, so this is off there unless you set forceOn2013. It works on single-person fields only.

spjs.ac.peoplePicker({
    "applyTo": "Responsible",
    "forceOn2013": true,
    "helpText": "Enter name or e-mail …",
    "showField": "Title",
    "enforceUniqueValues": true,
    "chooseFromUserGroup": "",
    "showUsersOnly": true,
    "rowLimit": 5,
    "listOptionsOnFocus": false,
    "reValidateOnLoad": false,
    "setFields": []
});

People picker options

The options shared with the text field work as described above.

OptionWhat it does
forceOn2013true to use this autocomplete on SharePoint 2013 and later.
chooseFromUserGroupOnly suggest members of this SharePoint group (name, or ID as a number), or of several groups in an array; empty for all users. If the people column itself is limited to a group in the list settings, use the same group here.
showUsersOnlySuggest users only, not groups.

Change the texts

Set this in Custom JS before calling spjs.ac.textField or spjs.ac.peoplePicker:

spjs.ac.text = {
    "noItems": "No items match your search",
    "addNewItem": "Add {0} to the list?",
    "noValidItems": "No valid items match your search",
    "invalid": "Invalid value",
    "moreItemsLabel": "Showing the first {0} items",
    "moreItemsMouseover": "Continue typing to narrow your search",
    "searching": "Searching for: "
};

From code

  • spjs.ac.destroy("Project") removes the autocomplete from a field.
  • spjs.ac.setFieldValue("Project", "Project X") sets the value.

Callbacks. Define these in Custom JS to react when the user makes a valid choice, leaves an invalid value, or clears the field:

function spjs_ac_validSelectionCallback(fin, value) { }
function spjs_ac_invalidSelectionCallback(fin, value) { }
function spjs_ac_clearSelectionCallback(fin) { }

Filtering large lists

For a source list over 5,000 items that you cannot filter in the query, load everything ("useREST": true, "preloadData": true) and filter afterwards:

  • loadAdditionalFields: extra data to load for the filter, for example {"select": ["Author/Id"], "expand": ["Author"]}.
  • postPreloadFilterFn: a function that receives each item and returns true to keep it.
"postPreloadFilterFn": function (item) {
    return item.Author.Id === _spPageContextInfo.userId;
}