If you are doing Salesforce Integration work and working with the REST API, then you have probably already heard about the Force.com Toolkit for .NET. But, because the toolkit is unfortunately not very well documented, you probably had no idea that it defaults to using version 32 of the REST API. You also probably had no idea that you can change the api version (like to the latest version, which is currently 34) using the Toolkit.
Don’t worry, I had no idea either and I have worked with the Toolkit quite extensively. In fact, when I discovered that it was using version 32, I actually reported it as an issue (since the Toolkit is Open Source). However, as the projects owner (Wade Wegner) graciously pointed out to me, this is a purposeful feature of the Toolkit. As he states, “The SDK is designed to make it easy for you to change the API version yourself programmatically but defaults to the last known working API version. That’s not to say it won’t work with v34, only we haven’t tested it.”
So, now you may be asking, “Ok, so how do I change it?”
Glad you asked, since that is what this post is about. Turns out to be pretty easy (since the Toolkit is pretty slick like that).
The ApiVersion property can be set or retrieved after authenticating using either the UsernamePasswordAsync or WebServerAsync methods. For example, if I were to place a line of code like this in a console application after authentication, I would expect to get “v32.0” printed out to the console:
await auth.UsernamePasswordAsync(consumerKey, consumerSecret, username, password + token, url); Console.WriteLine("Sucessfully Logged in. You're default API Version is: " + auth.ApiVersion);
Now if I want to set it to the latest version (which is 34), I can pass that in when I get an instance of the ForceClient, which is needed to perform any operation using the Toolkit. The code to set the ApiVersion to 34 would simply look like this:
var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, "v34.0");
That’s it. Just remember that the ApiVersion is a string and that it must be formatted like this: “v34.0”. Passing in “34” or 34 will not work and will get you errors, in fact. Not sure I like that part so much, but just glad I can set it to what I want.
BTW, I tested that this would work using the Sample Console app and setting the version to 34 and it worked splendidly.
Hope that helps someone out there.