Use credentials to interact with the Client Object

2019-01-16 20:04发布

问题:

I need to write a small software which has to update some list on a Sharepoint 2010.

I found the "SPSite" which I can create with the url, but I can't figure out how to specify with which user I want to connect.

The user isn't the current windows user, and the program isn't executed on the server.

I saw the possiblity to give a "SPUserToken", but in my method I only have the user, the domain and his password, so how can I generate this user(and I think that this user is unknown on the system executing the code, but known on the server)

Where can I specify that?

Thank you!

回答1:

Since you're using the client object model, you won't be working with the SPSite class (which is part of the server object model).

Instead, you should create an instance of the ClientContext class and supply your authentication credentials through its aptly-named Credentials property. Then you can use it to fetch the List object you want to update:

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.
}