This one has caused me a few hours of pain….a couple of times.  It is about time I document how I fixed it.

The Error:

Cannot import the following key file: <filename>.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_ E2AEBF22AB52DD08    <Application Name>

The Fix:

  1. Open Visual Studio Command Prompt (It can be found in the Windows Start menu)
  2. Type sn -i “c:\Pathtofile\<filename>.pfx” TVS_KEY_ E2AEBF22AB52DD08
  3. Reimport the pfx file into Visual Studio

The sn.exe with the –i parameter, installs a key pair from <infile> into a key container named <container>.

This method is a modified version of Mike Borozdin method which I happen to enjoy very much. The biggest changes I made where to add using statements around the disposable objects such as the Bitmap and the Graphics object to avoid memory leaks, as well as a few minor changes.

//Image Resize Helper Method
private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
{
    using (Image originalImage = Image.FromFile(filename))
    {
        //Caluate new Size
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;

        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = (int)Math.Round(newWidth / aspectRatio);
        }
        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = (int)Math.Round(newHeight * aspectRatio);
        }

        Bitmap newImage = new Bitmap(newWidth, newHeight);

        using (Graphics g = Graphics.FromImage(newImage))
        {
            //--Quality Settings Adjust to fit your application
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
            return newImage;
        }
    }
}

The other day, one of our customers was switching over to 64-bit development machines, and encountered the following issue:

System.BadImageFormatException: Could not load file or assembly'(assembly name)' or one of its dependencies. An attempt was made to load a program with an incorrect format.

The likely reason is that you (or in this instance, our customer) are trying to load a 32-bit assembly into a 64-bit application. This can happen when you have the Any CPU platform selected.

Solution:

If your scenario sounds like the one I described, try setting your application to run in x86 mode. In Visual Studio this can be done by going to Build -> Configuration Manager…
Visual Studio Configuration Manager
…and setting the Platform to be x86.
Visual Studio Configuration Manager

This article and many others like this can be found on my SoftArtisans blog.

SharePoint 2010
[Image via Tihomir Ignatov]

Setting up a SharePoint Development Server

Recently I wanted to start some SharePoint 2010 development, and taking Microsoft’s recommendation that developing for SharePoint be conducted on a machine that already has it installed, I set out to create development-optimized SharePoint environment. One of the greatest tools I came across was the SharePoint 2010 Easy Setup Script. The script is a set ofWindows PowerShell scripts that install and configure all the pre-requisites and products to get you developing for SharePoint in no time. Running these scripts will install evaluation versions of:

  • SharePoint Server 2010 + pre-requisites (Standalone)
  • Visual Studio 2010 Ultimate Edition
  • Silverlight 4 Tools for Visual Studio
  • Expression Studio 4 Ultimate
  • Open XML SDK
  • Visual Studio SDK
  • Visual Studio SharePoint Power Tools
  • Office 2010 Professional Plus
  • SharePoint Designer 2010
  • Visio 2010

After downloading and extracting, the installation files will be installed to C:\SharePoint2010EasySetup by default. The next step is to configure the scripts:

Configuring the Installation Scripts

Navigate to C:\SharePoint2010EasySetup\Labs\EasySetup\Source and locate the config.xml file. This is the file you want to modify. Here you can set up a virtual hard disk to install applications to and comment out any applications that you do notwant installed. In my case I wanted to do a fresh install of a Windows 7 64bit operating system on my local operating system, rather than on a virtual disk (it’s recommended to do a fresh install when installing to your local os). After editing the configuration file you will want to save the configuration file and then execute the run.bat file.

Running the scripts:

In the same directory running the Run.bat file will execute the Windows PowerShell scripts in the correct order.  The scripts will download the evaluation versions of the software, install them and configure them. During this process I was asked to reboot several times.

SharePoint 2010 Development Resources:

If you are completely new to SharePoint development I found some of the following resources helpful:

  • SharePoint Server Virtual Labs: MSDN has some free virtual labs. You can use Hyper V to remote into the remote machine provided by Microsoft, which is preconfigured for you to follow the training labs. While these labs are ready to go and everything is set up for you, I did find the remote connection a little slow.
  • Share Point 2010 Training Videos: Microsoft MSDN has some great SharePoint videos that I found very helpful. The videos also link to corresponding labs for you to try your new skills, as well as provide quizzes to make sure you really understand the material.
  • SharePoint 2010 Books: I have been reading SharePoint master Tom Rizzo’s Professional SharePoint 2010 Development
  • Also, for those interested in Team-Based SP development, MVP Andrew Connell has written a very thorough and cohesive article for MSDN on the subject.http://msdn.microsoft.com/en-us/library/gg512102.aspx

This article and many others like this can be found on my SoftArtisans blog.

I was recently asked to work on a site running FMyScript. The problem was this every time a voting link was clicked the link would just disappear and the votes were not updated.

The solution was two fold:

Part 1:
The first problem was the voting links has a ‘ in the string such as “I’m really sad to hear that” So I just changed the string instead of finding all the places I might have to escape it. I figured I am is better grammar anyways.

Part 2:
The domain must the config file. That means if you set up your site with the www in the url and someone goes to your site without the www in the url the voting system will not work.

I modified the .htaccess files to force www by adding the following lines:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]

I hope this helps someone.