Friday, January 28, 2011

SPFarm.Local is null (x86 vs. x64)

This is one of those silly little mistakes which we very often overlook…and believe it or not, I have found many forums and blog posts where developers ask for help on this.
I wanted to work with the SPFarm.Local object earlier this week and forgot that SharePoint 2010 targets the 64bit platform …duh… so when I tried the following code:
private void cmdCreateWebApp_Click(object sender, EventArgs e)
        {
            int myPort = 999;

            SPFarm farm = SPFarm.Local;
            SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder

            (SPFarm.Local);
            webAppBuilder.Port = myPort;
            SPWebApplication newApplication = webAppBuilder.Create();
            newApplication.Provision();

            SPSite mySiteCollection = newApplication.Sites.Add("/", 
           @"Mydomain\MyAdminUserName", "MyAdminUserEmail@CompanyName.co.za");
            mySiteCollection.Close();
        }

I kept getting an exception and found that SPFarm.Local remains null.
image
The problem was that my VS2010 project was set to target a x86 platform and not the x64!!
So, I opened my project properties and changed the platform target to x64.
image
Recompiled my code and ran it and it worked like a charm !
image
Always remember guys…. X64 !!!!!

0 comments:

Post a Comment