8

I have implemented Odata query syntax for my web api. I am successfully able to return only the first 10 results and the link for the further results. However I am unable to extract this link from the JSON object that is returned by the server using my angularjs front end.

Say the server is responding as follows:

{
  "odata.metadata":"http://localhost:60497/odata/$metadata#tables","value":[
    {
      "id":001,"name":"abc"
    },{
      "id":002,"name":"pqr"
    },{
      "id":003,"name":"xyz"
    },{
      .
      .
      .
  ],"odata.nextLink":"http://localhost:60497/odata/tables?$skip=10"
}

Now I am displaying the data by using the success method of $http by assigning the returned data to a variable and using ng-repeat. I am assigning it as follows:

.success(function(data)){
  $scope.foo = data.value;
}

However when I try to access the next link using:

$scope.link = data.odata.nextLink;

within the success method it gives me an error. What am I missing over here? How else can I access the link returned? Is there any other method to implement server side paging?

5 Answers 5

12

I had the same problem, more or less. I guess it has got to do with JavaScript objects and how their properties are referred to. The reference

data.odata.nextLink

would mean there is a property "odata" with a sub-property/field "nextLink". This is not the case, "odata.nextLink" is the name of the property. I don't know why OData is like this.

I got the contents of this property by using a string reference i.e.

data['odata.nextLink']

Don't know if there is some drawback, but seems to work...

Sign up to request clarification or add additional context in comments.

Comments

4
    using Newtonsoft.Json;     
    [JsonProperty("@odata.nextLink")]
    public string nextPage { get; set; }

Comments

3

I got it to work using

theReturnedObject['@odata.nextLink']

Comments

0

Don't forget that the odata values should have an @ in front of them. I'm not sure how your implementation handles that character but it should be a valid prefix.

The issue I am running into is that some providers use the full path and some only use a relative path from the service endpoint.

Comments

0

The C# method that I devised for use with the Microsoft Graph API is as follows.

private static string GetNextLink ( JObject pjobjResult )
{
    JProperty jtLastToken = ( JProperty ) pjobjResult.Last;

    if ( jtLastToken.Name.Equals ( @"@odata.nextLink" ) )
    {
        return jtLastToken.Value.ToString ( );
    }   // TRUE (There is at least one more page.) block, if ( jtLastToken.Name.Equals ( @"@odata.nextLink" ) )
    else
    {
        return SpecialStrings.EMPTY_STRING;
    }   // FALSE (The current page is the last one.) block, if ( jtLastToken.Name.Equals ( @"@odata.nextLink" ) )
}   // private static string GetNextLink

In the above method, SpecialStrings.EMPTY_STRING is the name of a true constant that I use in lieu of string.Empty.

A similar method should work for PowerShell, too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.