Skip to content

Salesforce SOAP API Login – Minimum Pieces of Flair

I have been working with the Salesforce API lately, and have found the learning curve to be a little bit steep. I can’t say it’s been insurmountable, but often times the first day working with any API is the most frustrating.

So without further ado, I present the Minimum Pieces of Flair required to successfully login to the SOAP API.

Steps to add a Web Reference to your project in Visual Studio.

  1. Right Click Project
  2. Add
  3. Service Reference
  4. Advanced…
  5. Add Web Reference…
  6. Open your salesforce wsdl xml file
  7. Click Add Reference

C# code

var service = new SforceService.SforceService();
var result = service.login("user", "password");
service.Url = result.serverUrl;
service.SessionHeaderValue = 
    new SforceService.SessionHeader() 
    { 
        sessionId = result.sessionId 
    };

This will get you everything that you need to login and start making calls. Salesforce’s introductory example is lengthy with all kinds of exception handling and timeout setting. In my case, this was overkill, but your mileage may vary.

NSIS Select Install Directory and Backup

After quite a few times of having to update my software manually(and really one time is too many), I decided to embark on the difficult road of creating an updater where one had not existed. I turned to my trusted NSIS, which had worked well for me in the past(your mileage may vary).

My only complaint would be that not that many people use it(I know not a lot of people make windows installers anymore, but I needed to for this project.), which leads to confusing documentation, Q&A and resources kind of scattered around online. I had some difficulty finding out elementary basic examples of how to do the following with NSIS.

  • Prompt the user to choose the installation directory
  • Backup the existing directory with a datetime stamp
  • Mark the backup as readonly

These are pretty standard, and I assume pretty common requirements for an install or update. Below is my solution which is performs these simple tasks.

!include FileFunc.nsh
!insertmacro GetTime

SetCompressor /SOLID /FINAL lzma

# name the installer
OutFile "Installer.exe"

InstallDir $%HOMEDRIVE%

Page directory
Page instfiles

Section Backup
    Var /GLOBAL backupFolder
    Var /GLOBAL command

    # define the output path for this file
    setOutPath $INSTDIR

    DetailPrint "The user has chose the directory: $INSTDIR for the install."

    ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6

    StrCpy $backupFolder "$INSTDIR.backup.$1-$0-$2.$4_$5_$6"

    StrCpy $command 'xcopy "$INSTDIR" "$backupFolder" /S /E /V /Y /I'
    DetailPrint $command
    ExecWait $command
SectionEnd 

Update!
The following code should be used with caution. I had to remove it, due to the permission implications of setting file/folder attributes. Many configurations will not allow user’s to set these attributes, especially on network drives. So you have been warned.

    ExecWait 'attrib +r "$backupFolder\*.*" /S /D'

Resize and Upload

I recently came across an interesting set of requirements. I had a collection of images that needed to be re-sized and uploaded to an ftp site. I groaned on the inside remembering implementations featuring tedious height, width, resolution and other calculations. Fortunately, a cool head and swift fingertips for searching prevailed. I learned about a wonderful library called ImageResizer.Net. Did you know it abstracts the much of the ugliness away from the consumer?

I never wanted to know about any of that graphics jargon and lingo, and not because it’s not important. It’s not important to me for what I am doing. Below you can see how trivial the image re-sizing code becomes. FTP Upload in .NET – You ain’t need no libraries proved that I could get along without a library just using the .net framework for this task.

            var versions = new Dictionary();
            versions.Add("_small", "width=100&height=100");
            versions.Add("_medium", "width=300&height=300");
            versions.Add("_large", "width=700&height=700");

            foreach (var version in versions)
            {
                var request = (FtpWebRequest)WebRequest.Create("ftp://{domain}/{path}/OS_" + version.Key + ".png");
                request.Credentials = new NetworkCredential(username, password);
                request.KeepAlive = false;
                request.UseBinary = true;
                request.Method = WebRequestMethods.Ftp.UploadFile;

                var memoryStream = new MemoryStream();
                ImageBuilder.Current.Build("OS.png", memoryStream, new ResizeSettings(version.Value), false, true);
                using (var writer = new BinaryWriter(request.GetRequestStream()))
                {
                    writer.Write(memoryStream.ToArray());
                }
            }

I can’t recommend simple research like this enough. It will save you time, energy, frustration and prevent resolved bugs from appearing.

Samba Printer Sharing with Windows XP Gotcha

Make sure the there is not already a local printer with the same name as the printer that you are trying to add, because apparently Windows XP cannot tell the difference. This happened to me and I was greeted with the below dialog.

windowsprinterdialog

There should be the shared printer name under the computer name. So naturally I started to check all kinds of settings on the server, printer and windows client. Luckily I escaped from the wild goose chase. Hopefully this helps someone else do the same.

I was able to set this up in under 2 hours using the Network Printing with Ubuntu and Sharing a printer to Windows XP clients with Samba and Cups guides. The Ubuntu/Linux community has created amazing software and documentation even with something as ugly as printing. If you do not have to play with Windows clients, I highly recommend the CUPS interface.

The Campsite Rule

Sleeping Camper
In many positions, you are faced with the pile of code known as the legacy project. It is difficult to stage, understand and add additional functionality. It tempts you to transform it into the gleaming success story you want, and you believe the customer and your boss want.

However, there are a number of reasons that this project has deteriorated. In many cases there may not be the financial or political will to invest in a rewrite (even if it seems necessary). Bugs and deadlines take precedence over writing better code (as they should). Should you just throw your hands up and find a different job/project? No.

You can’t fix the code base completely, but whatever you touch should be better than when you found it. I call this the Campsite Rule. Rewriting a program from scratch inspires dreams among programmers, but doesn’t always work in practice. Maintaining software while actively making it more stable and supportable demonstrates craftsmanship.

Ask for an Install

I have been asked for the status of various projects, and I have asked other people about the status of their work. Sometimes there were discrepancies between the reported status and the actual progress.

To be clear, the inconsistency represents miscommunication and/or misunderstanding rather than dishonesty. I consider all the discussions about what “DONE” means.

This motivated me to search for a better way. How will I know if my team or myself are on schedule? Or even early? Consider how the success of the project will be measured, the customer will use it and decide for themselves. Here’s some of the subconscious questions being answered:

  1. Did it run?
  2. Does it look good?
  3. Does it do what I want?(Note the question is not “Does it do what we agreed it should do?”)
  4. Was it easy to do what I wanted?

Other possibilities exist, but you get the idea. One notably absent question is “What’s the status of this?”. The customer asks the software these questions rather than its creator.

Consider the last time you test drove a car.

Old Carriage

Did you ask the engineer, designer or mechanic how the car handled? Of course not, you knew exactly how it handled, because you drove it. How can a user “drive” software?

The user must install and use the software(yes, the install counts). The next time someone requests status, give them a build or install the latest build on a clean environment for them.

The next time you want to know the status, ask for a build. Yes, this takes more time than asking “Is it done yet?” or “Where are we on X?”, but it saves time and embarrassment with the final product.

Raspberry Pi: My Experience

I highly recommend the Raspberry Pi to anyone who is interested in learning how to use it. There are many different uses for it. In my case, I set mine up as an HTPC. Here is a picture of my setup:

IMG_0218

I used the following components:

Distros

I originally installed raspbian, which worked fine. The problem with raspbian is that I wanted it to be an HTPC. In this regard, you should definitely use one of the distros that has xbmc included (XBian, raspbmc, OpenELEC). It is much easier than trying to setup everything yourself. I am currently running XBian Alpha 5, and it is working great. Thanks to all of the people that worked very hard to make these distributions what they are today.

Patience

I had to re-install the image on the sd and start over several times. I got frustrated, but this is part of the process. Accept this fact and it will be much more enjoyable. A significant part of the process is learning how to setup and configure XBMC and your distro.

Just about every question you could ever have has already been asked. Please take the time to read the forums and take a look at Raspberry Pi Stack Exchange. If you do find yourself getting frustrated, step away for a bit. If you are anything like me, you will return to fiddling with it pretty soon.

Don’t even attempt this setup if you don’t have another machine. It is very helpful to be able to ssh into the raspberry pi.

Overall

It was a great experience that I would recommend to anyone with 1+ years of experience using a Linux distribution (including only personal use). I am very happy with my current setup and consider it money well spent. Do make sure that you order the correct compatible parts.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: