如何使用凭据连接到SharePoint列表使用客户端对象模型?

问题描述 投票:21回答:1

我需要编写一个应用程序来更新SharePoint 2010网站上的列表。

我找到了可以使用URL创建的“SPSite”,但我无法弄清楚如何指定我想要连接的用户。

用户不是当前的Windows用户,并且该程序未在服务器上执行。

我看到了提供“SPUserToken”的可能性,但在我的方法中我只有用户,域和他的密码,所以我怎么能生成这个用户(我认为这个用户在执行代码的系统上是未知的,但在服务器上已知)。

我在哪里可以指定?

c# .net sharepoint sharepoint-2010 sharepoint-clientobject
1个回答
37
投票

由于您使用的是客户端对象模型,因此您将无法使用SPSite类(它是服务器对象模型的一部分)。

相反,您应该创建ClientContext类的实例,并通过其恰当命名的Credentials属性提供您的身份验证凭据。然后你可以用它来获取你想要更新的List对象:

using System.Net;
using Microsoft.SharePoint.Client;

using (ClientContext context = new ClientContext("http://yourserver/")) {
    context.Credentials = new NetworkCredential("user", "password", "domain");
    List list = context.Web.Lists.GetByTitle("Some List");
    context.ExecuteQuery();

    // Now update the list.
}
© www.soinside.com 2019 - 2024. All rights reserved.