找不到 SPSite 名称 space

cannot find the SPSite name space

我找不到 SPSite

的名称 space

到目前为止我已经导入了这些:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.SharePoint;
using System.Collections;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Data.SqlClient;
using SP = Microsoft.SharePoint.Client;
using System.Data;


namespace GrandPermission
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite oSPSite = new SPSite("http://spdevserver:1002/");
        }
    }
}

SPSite下面还有红线。

根据您的屏幕截图,您在项目的引用中缺少对 Microsoft.SharePoint 的引用。您只有对客户端 dll 的引用。

如果您没有找到它,请确保您正在 SharePoint 服务器上进行开发,即安装了 SharePoint,或者您的计算机上有 dll 的副本。我看过论坛帖子,其中有人将 SharePoint dll 从 SharePoint 服务器复制到本地非 SharePoint 开发机器。但是,我从来没有让它发挥作用。我一直在开发服务器上安装 SharePoint,并从中 运行 Visual Studio。

发生此错误是因为 SPSite classServer 端对象模型 API 的一部分:

Server Side Object Model API could be utilized only on machine where SharePoint Server/Foundation is installed.

由于您使用的是 客户端对象模型 API(项目中引用的程序集 Microsoft.SharePoint.Client.dllSharePoint Server 2013 Client Components SDK 的一部分)我会建议使用此 API。

因此,行:

SPSite oSPSite = new SPSite("http://spdevserver:1002/");  //SSOM

可以替换为:

using(var ctx = new ClientContext("http://spdevserver:1002/"))
{
    var site = ctx.Site;
    //...
}

参考资料

您需要将程序集 Microsoft.SharePoint.dll 添加到您的引用中才能使用它。您可以在正在使用的 SharePoint 服务器上找到此 DLL:

SharePoint 2013 它位于:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI

SharePoint 2010 上可以在以下位置找到它:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI