4

I have found the SO Link which discusses about downloading the binary variant using OData service by using the HTTP URL as below,

http://localhost:82/odata.svc/BinaryContents(BinaryId=159,PublicationId=4,Varia‌ntId='Y29uZmlnLWJvb3RzdHJhcA==')/$value

We would like to consume the binarycontent using C# client, I am stuck in converting the above HTTP OData Request to the equivalent C# LINQ Query and consume the BinaryVariant via C#.

Here is what I have done so far,

    BinaryContent content = (from s in cds.BinaryContents
                             where s.BinaryId == 1415 && s.PublicationId == 3 && s.VariantId == "ZGlzdC1jb21wb25lbnRzLmh0bWw="
                             select s).FirstOrDefault();

But the BinaryContent class does not have a method/property to return the byte[] or data stream.

Thanks, Rajesh

2

I have not found a way to do this using just the LINQ API, but you can download the file as a byte[] using the System.Net.WebClient object.

Try this:

string url = cds.BinaryContents.RequestUri + "(BinaryId=1415 ,PublicationId=3,VariantId='ZGlzdC1jb21wb25lbnRzLmh0bWw=')/$value";
WebClient webClient = new WebClient();
webClient.Headers.Add("Authorization", "OAuth " + OdataUtilities.GetAccessToken());
byte[] bytes = webClient.DownloadData(url);

Note that you only need the line 'webClient.Headers.Add' part if you are using a secured OData Service. OdataUtilities.GetAccessToken() is just a method I use to retrieve a token for the request.

  • Thanks Chris, I will give a try. But from the look of it, because we are able to access byte[] via HTTP Odata URL, byte[] property is available in the BinaryContent class, but currently it might not be a public property of that class. – Rajesh Sep 4 '15 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.