Connecting Windows Apps to SalesForce Chatter

Are you using SalesForce for CRM? Did you know SalesForce also offers an industry-leading enterprise social network called Chatter? Are your users starting to bring Windows devices in the BYOD fold? Would you not like to custom apps on web/desktop/mobile that allows your users to interact with SalesForce applications more easily? If you answered yes to any, read on ..

Now, you probably already heard that SalesForce recently announced SalesForce Toolkits for .NET developers. This comes courtesy of my good friend, the brilliant Wade Wegner. Details about the SalesForce Toolkit announcement can be found HERE, which has links to the Force.Com & Chatter Toolkits for .NET.

When Wade asked me to take a look, my little world of Windows 8 apps was the obvious choice to try things out. So, let’s try connecting to SalesForce Chatter from a Windows Store app, to give the new APIs a spin. Here’s my step by step guide:

SFDashBoard

  • Before our 3rd party app can talk to SalesForce, we need a “Connected App”, sort of like a proxy which allows us to talk to SalesForce APIs on behalf of the user. From the dashboard, click Build->Create->Apps from the left menu; at the bottom, you’ll see Connected Apps, as below .. click New.

CreateNewApps

  • Create a new App, by specifying App name, Callback URL & OAuth permissions. The end result should be something like my SFChatterApp, as below. Let’s make note of the app’s Consumer Key & Secret; we’ll need them in our client app.

AppCreated

  • While you are just done creating your SalesForce Connected App, let’s take care of one more setting, unless you want to be left scratching your head like me later down the line. Click on your app to go to Settings and make sure you relax IP firewall settings, or allow your specific IP ranges to have access, like shown below.

AppSettings

  • That’s it .. we’re all set on the SalesForce side with our connected app. Time to now build our Windows 8 Store app to connect to SalesForce through the connected app we just created. So, back in Visual Studio, we create a new Windows 8 app from default template. Next step, let’s grab the Chatter Toolkit for .NET, which is available as a NuGet package. We could add this visually, as below or using the NuGet Package Manager Console and “Install-Package DeveloperForce.Chatter”.

SFNuGet

  • With the added DLL references, now let’s write some code to hook up our client Windows 8 Store app to the SalesForce Chatter APIs. I’m going to use the Username-Password flow to quickly get an OAuth access token from Chatter; but for anything real-world, you may want to use the WebServer or OAuth flows, so that each user can get his/her own access token. Wade has these flows nicely documented HERE and HERE. Here’s some code in my App.xaml.cs:

using Salesforce.Common;
using Salesforce.Chatter;
using Salesforce.Chatter.Models;

public AuthenticationClient SFAuth = new AuthenticationClient();
public ChatterClient SFChatter;
public Me SFMe;

 public static new App Current
 {
    get { return Application.Current as App; }
 }

private async void DoSalesForceHandshake()
{
    await SFAuth.UsernamePassword("ConsumerKey", "ConsumerSecret", "UserName", "Password");
    SFChatter = new ChatterClient(SFAuth.InstanceUrl, SFAuth.AccessToken, SFAuth.ApiVersion);
    SFMe = await SFChatter.Me(); 
}
  • You’ll notice that I have a few global variables that I want to hydrate & use elsewhere in my app. I invoke the DoSalesForceHandshake() method from my app’s OnLaunched event handler, to do the SaleForce Chatter handshaking upfront. If your AuthenticationClient object is hydrated correctly, you’re all set. Notice how we create the ChatterClient using details from our authentication call to Chatter APIs. Lastly, we grab info about our personal Chatter feed using the Me() method call. This is sounding like fun, isn’t it?
  • What we want to do is try posting a Chatter message from our Windows 8 app onto SalesForce Chatter, through our client handshaking that we just did. Here’s my over-simplified Windows 8 UI in MainPage.xaml for posting a Chatter message:

ChatterWin8

  • Here’s my code on the send button’s click event handler, so we can post to Chatter:

private async void sendChatBtn_Click(object sender, RoutedEventArgs e)
{            
    var messageSegment = new MessageSegment()
    {
       text = chatTextbox.Text,
       type = "Text"
    };

    var body = new FeedItemBody { messageSegments = new List { messageSegment } };
    var feedItemInput = new FeedItemInput()
    {
        attachment = null,
        body = body
    };

    var feedItem = await App.Current.SFChatter.PostFeedItem(feedItemInput, App.Current.SFMe.id);
}
  • Notice how we are identifying the user (through id property of Me object) as we post the FeedItemInput containing the message to Chatter. The result on button click .. sure enough, our user’s message gets posted to Chatter, as shown below:

SFChatter

  • We only scratched the surface of Chatter APIs here .. you can do many more fancy things like liking/commenting on a Chatter feed item etc. And all of this helps you make interactive mobile apps for your users.
  • Now, here’s the best part – you’ll notice that the Chatter toolkit uses the Async-Await C# pattern for asynchronous access, which allows from easy consumption from any UI client you want to build and it also uses PCLs under the covers. So, as long as you are building any modern .NET based applications, the same code/techniques apply .. this extends coverage to Windows 8, Windows Phone, Silverlight, WPF & even Console apps. Fun!

Hope this was helpful.
Adios!

Advertisement

2 thoughts on “Connecting Windows Apps to SalesForce Chatter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s