Search code examples
c#sharepointsharepoint-2007

How to connect to SharePoint 2007 using C#?


I have a SharePoint 2007 farm and I need to connect to it using C# in order to get its data programmatically.

The purpose of all this is to get all its lists (including fields and data) and subsites in order to recreate them in SharePoint Online. I successfully connected to SPO, but to SharePoint 2007 I could not.

using (SPSite site = new SPSite(root_url + "/" + relative_site_url))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                //SPFolder myLibrary = web.Folders["testdocuments"];
                                SPWebCollection SitesList = web.Site.AllWebs;
                                int i = 0;
                                foreach (string WebSiteName in SitesList.Names)
                                {
                                    if (WebSiteName.Contains(relative_site_url))
                                    {
                                        //Console.WriteLine("Title: " + WebSite.Title);
                                        //Console.WriteLine("Description: " + WebSite.Description);
                                        Console.WriteLine(i.ToString() + " " + WebSiteName);
                                        Console.WriteLine("Lists:");
                                        GetSiteLists(root_url + "/" + relative_site_url);
                                        i++;
                                    }
                                }
                            }
                        }
        }

I am expecting a successful connection. However, I got this error message when trying to connect to a local SharePoint 2007:

Unhandled Exception: System.IO.FileNotFoundException: The Web application at http://localhost/ could not be
 found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administ
rator may need to add a new request URL mapping to the intended application.
   at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
   at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
   at ExportSolution.Program.GetSiteLists(String root_url, String relative_site_url) in D:\Users\admin\source\repos\
ExportSolution\ExportSolution\Program.cs:line 33
   at ExportSolution.Program.Main(String[] args) in D:\Users\admin\source\repos\ExportSolution\ExportSolution\Progra
m.cs:line 17


Solution

  • Objects SPSite, SPWeb and other with prefix "SP" exists only inside sharepoint farm. You must install sharepoint solution with stsadm tool.

    If you want connect to sharepoint outside you must use "SharePoint SDK" with client context to web services. Sharepoint 2007 SDK download page Sharepoint 2007 SDK documentation