如何使用 C# 连接到 SharePoint 2007?

How to connect to SharePoint 2007 using C#?

我有一个 SharePoint 2007 服务器场,我需要使用 C# 连接到它以便以编程方式获取它的数据。

所有这一切的目的是获取其所有列表(包括字段和数据)和子网站,以便在 SharePoint Online 中重新创建它们。我已成功连接到 SPO,但无法连接到 SharePoint 2007。

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++;
                                    }
                                }
                            }
                        }
        }

我期待连接成功。但是,我在尝试连接到本地 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

对象 SPSiteSPWeb 和其他前缀为 "SP" 的对象仅存在于共享点农场内。您必须使用 stsadm 工具安装 Sharepoint 解决方案。

如果您想连接到外部的共享点,您必须将 "SharePoint SDK" 与客户端上下文一起使用到 Web 服务。 Sharepoint 2007 SDK download page Sharepoint 2007 SDK documentation