SP.RelatedField Class
Applies to: SharePoint Foundation 2010
Specifies a relationship to a lookup field.
SP.RelatedField
Inherits
Example
The following example creates an input button on an application page that displays the Id of fields related to the Tasks list of the current site.
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">
var relatedFieldCollection = null;
function runCode() {
var clientContext = new SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
this.web = clientContext.get_web();
var listCollection = web.get_lists();
var item = listCollection.getByTitle("Tasks");
// Get a collection of lookup fields that use this list as a data source
// and that have FieldLookup.IsRelationship set to true.
this.relatedFieldCollection = item.getRelatedFields();
clientContext.load(this.relatedFieldCollection);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
}
function onQuerySucceeded(sender, args) {
var fieldInfo = '';
var fieldEnumerator = this.relatedFieldCollection.getEnumerator();
while (fieldEnumerator.moveNext()) {
var relatedField = fieldEnumerator.get_current();
// Get the GUID that identifies the related lookup field.
fieldInfo += 'Related Field ID: ' + relatedField.get_fieldId().toString() + '\n';
}
alert(fieldInfo);
}
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>