3
votes

I am getting what I think is a bogus "File Not Found" error when I do simple SharePoint 2010 client coding. It is not that I'm building to the wrong CPU type or the wrong .NET version. (I'm building to "any CPU" or "x64" and .NET 3.5.) I copied the Microsoft.SharePoint*.dlls from the server and put them in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI. (I've tried other folders too.) I then opened my Visual Studio 2010 project, right-clicked on References, clicked the Browse tab and added one or all of the dlls (I should only need Microsoft.SharePoint.dll.) This is the simple program I try to run:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SPListsToConsole {
    class Program {
        static void Main( string[] args ) {
            using ( SPSite sc = new SPSite( "http://orsandbox01/SitePages/Home.aspx" ) ) {
                SPWeb site = sc.RootWeb;
                foreach ( SPList list in site.Lists ) {
                    if ( !list.Hidden ) {
                        Console.WriteLine( list.Title );
                    }
                }
            }
        }
    }
}

And here is the error I get:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SharePoint.Library, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.SharePoint.Library, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
   at SPListsToConsole.Program.Main(String[] args)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

I haven't done any SharePoint coding before but I've following all the instructions I can Google. TIA for any help you can share.

2
How are you using / referencing the Microsoft.SharePoint.Librarydll's in your current project..?MethodMan
Stupid question: Does the code run on a SharePoint Server? What do you mean by "client coding" the SharePoint 2010 client object model?Stefan
By client, I mean on a non-server workstation. I'm just now installing VS2010 on the server so I can test whether the code works on the server. The Microsoft.SharePoint.dll is in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI on the server and the client. (I had to create the 14\ISAPI on the client). I added a reference in VS2010 and browsed to the dll.ksnortum

2 Answers

2
votes

It turns out you can't do what I was trying to do. You must have SharePoint installed on the machine you develop on.

MSDN Article

2
votes

Use SharePoint web services instead of the object model

If you need to work with a remote sharepoint server, you can use the web services instead of the object model.

Assuming you have added a reference to the lists webservice (/_vti_bin/lists.asmx), and called it "Lists", you can get a list of visible lists like this:

Lists.Lists proxy = new Lists.Lists();

// Use logged on users credentials to authenticate with SharePoint
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

XmlNode listCollection = proxy.GetListCollection();

foreach (XmlNode node in listCollection.ChildNodes)
{
    bool hidden = bool.Parse(node.Attributes["Hidden"].Value);

    if (!hidden)
    {
       Console.WriteLine(node.Attributes["Title"].Value);
    }
}

You can find more details on the lists web service here: http://msdn.microsoft.com/en-us/library/lists.lists_methods.aspx

Most of the things you can do with object model code can also be achieved using web services.

Adding a reference to the web service

To add a web reference in VS2010:

  1. Right click on the project node in Solution Explorer, and select Add Service Reference

  2. In the Add Service Reference dialog, click on Advanced...

  3. In the Service Reference Settings dialog, click on Add Web Reference...

  4. In the Add Web Reference dialog:

    a. For Url, enter the site collection url followed by _vti_bin/lists.asmx. For example: http://sharepoint/sites/mySiteCollection/_vti_bin/lists.asmx

    b. For Web reference name, enter Lists

    c. Click Add Reference

The reference will now be added to the project, and you can use the code above to access it.

To get lists from sub-site, change the services URL property to point to the web service for that sub-site by adding _vti_bin/lists.asmx to the sub-site's url as above.