get relationship id from layer

1927
5
Jump to solution
09-03-2013 01:25 AM
DeepikaJain
New Contributor III
Dear All

I am having multiple mapservices loaded in my map and I am working with query related records. Multiple layers have related data so the relationship id which i need to pass varied from layer to layer. I am unable to find the relationshipID of the layer dynamically. please let me know how to get it. I am using ArcGIS Javascript API 3.5

Thanks
Deepika
0 Kudos
1 Solution

Accepted Solutions
VinayBansal
Occasional Contributor II
In order to get the relation id you need to follow this :
1. Get the layer info from your map service and store its response in a variable
//Create a class called LayerInfo and add this function to it.
getLayerInfo: function () {         var requestHandle = esri.request({             "url": this.mapServiceUrl + "/layers",             "content": {                 "f": "json"             },             "callbackParamName": "callback"         });         return requestHandle.then(this.requestSucceeded, this.requestFailed);     }, requestSucceeded: function (response, io) {                 return response;     }     ,     requestFailed: function (response) {                 return null;     }


2. Once you get the response process the response to store layerwise info in a global variable.
processResponseFromDynamicLayer: function (response) {        var layerListByName = new Array();             if (response != null && response.layers != null) {                 for (var iCnt = 0; iCnt < response.layers.length; iCnt++) {                     if (response.layers[iCnt].type != "Group Layer" && response.layers[iCnt].type != "Annotation SubLayer") {                         if (layerListByName[response.layers[iCnt].name] == null) {                             layerListByName[response.layers[iCnt].name] = response.layers[iCnt];                             if (layerListByName.length == undefined)                                 layerListByName.length = 1;                             else                                 layerListByName.length = layerListByName.length + 1;                         }                     }                 }             }                  if (response != null && response.tables != null) {             for (var iCnt = 0; iCnt < response.tables.length; iCnt++) {                 if (layerListByName[response.tables[iCnt].name] == null) {                     layerListByName[response.tables[iCnt].name] = response.tables[iCnt];                     if (layerListByName.length == undefined)                         layerListByName.length = 1;                     else                         layerListByName.length = layerListByName.length + 1;                 }             }         } return layerListByName ;     }

3. From that variable get the layer relation ship Id
getLayerRelationShipId: function (layerName, relationName,layerListByName ) {         if (layerListByName != null && layerListByName.length > 0) {             if (layerListByName[layerName] != null) {                 var layerInfo = layerListByName[layerName];                 if (layerInfo.relationships != null && layerInfo.relationships.length > 0) {                     var relationsShips = layerInfo.relationships;                     for (iRelCnt = 0; iRelCnt < relationsShips.length; iRelCnt++) {                         if (relationsShips[iRelCnt].name == relationName)                             return relationsShips[iRelCnt].id;                     }                 }             }             else                 return -1;         }         return -1;     }


You can create a single class from all these functions and store the values in properties

View solution in original post

0 Kudos
5 Replies
VinayBansal
Occasional Contributor II
In order to get the relation id you need to follow this :
1. Get the layer info from your map service and store its response in a variable
//Create a class called LayerInfo and add this function to it.
getLayerInfo: function () {         var requestHandle = esri.request({             "url": this.mapServiceUrl + "/layers",             "content": {                 "f": "json"             },             "callbackParamName": "callback"         });         return requestHandle.then(this.requestSucceeded, this.requestFailed);     }, requestSucceeded: function (response, io) {                 return response;     }     ,     requestFailed: function (response) {                 return null;     }


2. Once you get the response process the response to store layerwise info in a global variable.
processResponseFromDynamicLayer: function (response) {        var layerListByName = new Array();             if (response != null && response.layers != null) {                 for (var iCnt = 0; iCnt < response.layers.length; iCnt++) {                     if (response.layers[iCnt].type != "Group Layer" && response.layers[iCnt].type != "Annotation SubLayer") {                         if (layerListByName[response.layers[iCnt].name] == null) {                             layerListByName[response.layers[iCnt].name] = response.layers[iCnt];                             if (layerListByName.length == undefined)                                 layerListByName.length = 1;                             else                                 layerListByName.length = layerListByName.length + 1;                         }                     }                 }             }                  if (response != null && response.tables != null) {             for (var iCnt = 0; iCnt < response.tables.length; iCnt++) {                 if (layerListByName[response.tables[iCnt].name] == null) {                     layerListByName[response.tables[iCnt].name] = response.tables[iCnt];                     if (layerListByName.length == undefined)                         layerListByName.length = 1;                     else                         layerListByName.length = layerListByName.length + 1;                 }             }         } return layerListByName ;     }

3. From that variable get the layer relation ship Id
getLayerRelationShipId: function (layerName, relationName,layerListByName ) {         if (layerListByName != null && layerListByName.length > 0) {             if (layerListByName[layerName] != null) {                 var layerInfo = layerListByName[layerName];                 if (layerInfo.relationships != null && layerInfo.relationships.length > 0) {                     var relationsShips = layerInfo.relationships;                     for (iRelCnt = 0; iRelCnt < relationsShips.length; iRelCnt++) {                         if (relationsShips[iRelCnt].name == relationName)                             return relationsShips[iRelCnt].id;                     }                 }             }             else                 return -1;         }         return -1;     }


You can create a single class from all these functions and store the values in properties
0 Kudos
DeepikaJain
New Contributor III
Hi Vinay

Thanks a lot. I get the Relationship Id now. But thres one more problem. Some of my layers have multiple relationship Ids, if I pass the multiple relationship ids to the relationship query, it gives error. Please let me know how should I go about it.

Thanks
Deepika
0 Kudos
VinayBansal
Occasional Contributor II
Since queryRelatedFeatures returns a defferred object you can do something like this

https://developers.arcgis.com/en/javascript/jssamples/query_deferred_list.html

Combine the deferred objects to generate single output
0 Kudos
DeepikaJain
New Contributor III
Hi Vinay

Thanks for the reply. But m using ajax call to get the response. this is how it is.
var _url = _requesturl + "/queryRelatedRecords";
    $.ajax({
        crossDomain: true,
        url: _url,
        type: "POST",
        dataType: "json",
        data: _c95,
        // async: false,
        jsonCallback: "callback",

        success: function (response) {
         designRelatdRecds(response,relatedLayername);

        },
        error: function (e) {
            alert(e.message + e.responseText);
        }
    });

how shud i combine the multiple responses together
0 Kudos
DeepikaJain
New Contributor III
Hi Vinay

Thanks for the reply. But m using ajax call to get the response. this is how it is.
var _url = _requesturl + "/queryRelatedRecords";
    $.ajax({
        crossDomain: true,
        url: _url,
        type: "POST",
        dataType: "json",
        data: _c95,
        // async: false,
        jsonCallback: "callback",

        success: function (response) {
         designRelatdRecds(response,relatedLayername);

        },
        error: function (e) {
            alert(e.message + e.responseText);
        }
    });

how shud i combine the multiple responses together



Thanks a lot Vinay for the help.Got it resolved 🙂
0 Kudos