Compartilhar via


SP.List.getRelatedFields() Method

Applies to: SharePoint Foundation 2010

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

var value = SP.List.getRelatedFields();

Return Value

Type: SP.RelatedFieldCollection

Exceptions

  • [System.UnauthorizedAccessException]
    The current user has insufficient permissions. Error code: -2147024891.

Applies To

SP.List Class

Example

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

<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>

See Also

Reference

SP.List Methods

SP.List Properties

SP Namespace