SP.List.getRelatedFields Method (sp.js)

Returns a collection of lookup fields that use this list as a data source and that have FieldLookup.IsRelationship set to true.

Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013

SP.List.getRelatedFields()

Return value

Type: SP.RelatedFieldCollection

Example

The following example creates an input button on an application page that displays the identifier of any fields related to the Tasks list on the current Web site.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

var relatedFieldCollection;
function runCode() {

    var clientContext = new SP.ClientContext.get_current()
    if (clientContext != undefined && clientContext != null) {

        var web = clientContext.get_web();

        var listCollection = web.get_lists();

        var list = listCollection.getByTitle("Tasks");

        // Get the collection of lookup fields that use this list as a data source and 
        // that have FieldLookup.IsRelationship set to true.
        this.relatedFieldCollection = list.getRelatedFields();

        clientContext.load(this.relatedFieldCollection);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onQuerySucceeded(sender, args) {
    var relatedFieldInfo = '';
    var relatedFieldEnumerator = this.relatedFieldCollection.getEnumerator();
    while (relatedFieldEnumerator.moveNext()) {
        var relatedField = relatedFieldEnumerator.get_current();
        relatedFieldInfo += 'Related Field ID: ' + relatedField.get_fieldId().toString() + '\n';
    }
    alert(relatedFieldInfo);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>

REST resource endpoint

See List resource for more information.

Endpoint URI structure

http://<sitecollection>/<site>/_api/web/lists(listid)/getRelatedFields()

HTTP requests

This resource supports the following HTTP commands:

  • POST

POST syntax

POST http://<sitecollection>/<site>/_api/web/lists(listid)/getRelatedFields()