Category Archives: Linq

EWS & .NET how to get started

Getting started with Exchange Web Service (EWS)

Firstly you might want to configure your security groups as per my article titled “Exchange Web Service (EWS) configuration” then return and continue.

EWS is a powerful way to access Microsoft Exchange over https. Doing this via .NET is a doddle.

I am going to assume you know how to add a web reference to your .net project.

your exchange server will be publishing EWS on the following URL

https://<DomainName|IPAddress|Servaername>/EWS/Services.wsdl

All you need to do is add a webreference to your .NET project by pointing to the above url. The important thing to remember is that you will actually be using a different URL when it comes to binding in code. The url shape will be like this

https://<DomainaName|IPAddress|Servaername>/EWS/exchange.asmx

Once you have your web reference in place, and in this example we will be calling the reference EWS, the first thing you are going to need to do is declare a new instance like so

The GetNewEWSBinding() returns a binding. The binding is on one of the mailboxes within the saEWSImpersonatable security group mentioned in the article previously mentioned at the begining of this one.
The following version of the function has a default binding mailbox defined in a variable at the package level of the SSIS package this script task resides in; it also checks for an InTest switch to swap between two mailboxes when in test or production. Binding credentials can be configured manually for a specific user, this user must be a member of the security group sgEWSImpersonate as described in the article mentioned at the beginning of this one. Finally there is an override optional parameter to provide if you wish to specify a specific mailbox to bind to instead of the default.
You can amend this to your requirements

As soon as you have your binding you are ready to start working with the mailbox.

In my next article on EWS, I will discuss how to declare your EWS binding objects using object initialisers, you will be able to declare and run requests against EWS using less code.
ie Dim x as object With {.parameter = value, .parameter = value}
Example

VAT Information Exchange System (VIES)

What is the VIES ?
It is an electronic means of transmitting information relating to VAT-registration (= validity of VAT-numbers) of companies registered in EU. Furthermore, information relating to (tax exempt) intra-Community supplies between Member States’ administrations is also transmitted via VIES

The Taxation and Custom Union has provided us developers a nice little webservice to use for validating VAT numbers realtime.

This is their public facing page for one off manual validation
http://ec.europa.eu/taxation_customs/vies/
The service to be used by your application can be found here
http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

The FAQ is here http://ec.europa.eu/taxation_customs/vies/faqvies.do

Ok so now i will demonstrate how to use this in your Visual Studio based application

If you have Visual Studio 2010, and your building a web app, in solution explorer right click the web app root node and you should see “Add Web Reference”, this will present you with the “Add Web Reference” dialog box
Paste the above web reference link into the “URL” and hit the “GO” green arrow

VIES Visual Studio Web Reference

Now Hit “Add Reference” and Visual Studio creates the Web Server Proxy and Wrapper Class.
Ok so now your project has the reference lets see how to use it

On your page that you wish to validate a vat number you should do the following
1. Collect a valid 2 character country code from the User
2. Collect a valid vat number from the User
3. Perform the needed filter on both of these values prior to envoking the webservice
4. Verify the return from the service and take action

First off you will need to import the reference into your page. For ease I use asp.net vb, C# will be similar and there are plenty of converters out there to see the code in c#

now your ready to use the reference
For this example we will be supplying country code and VAT number only. There are various other methods available but this suited me so its the only demo you will be getting.
The rules
1.The country code must be valid
2. The VAT number must contain no spaces and only be numerical numbers
3. We pass the validation boolean as a byref, if you also wanted to see what the company name and address the EU have on file for the company the webservice verified, you can pass two strings “Name” and “address” byref and view the content upon return.

This is how i do it

Most of the above code is preparation, there are very few ines tha perform the webservice method.
The actual call is made in the try catch

My particular app verifies the value of IsValid in the calling function, how you validate the values is up to you, on asuccessful return the value will be true and the byref Name and Add will contain the registered company name and address.

For your reference there is also another way to reference the web service described here http://softwareobjects.net/technology/other/vies-vat-number-checker/

LINQ to Entities does not recognize the method ‘System.String ToString()’ method

If you use the entity framework from microsoft, latest version at the date of this post is framework 4.0
You might run into this little angel when you try to envoke the string value of an object

LINQ to Entities does not recognize the method ‘System.String ToString()’ method

This happens when applying a filter to an integer field and you want to use the

method of a string. So my code looked ike this

From the master query

etc…

The only way i Could get round this was to onvert the UID field into a string in the master select query like so

notice the cure was to force linq to convert the resulting field into a string, allowing the filter to look like this

Microsoft have a list os supported methods here