24

I need to write an application to update a list on a SharePoint 2010 site.

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 possibility 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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
J4N
  • 19,480
  • 39
  • 187
  • 340

1 Answers1

45

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.
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 1
    hi! what is "http://yourserver/"? can you please check my question below.http://stackoverflow.com/questions/15737059/sharepoint-foundation-2010-client-object-model-authentication.Thanks – chamara Apr 01 '13 at 03:58
  • is somehting for JSOM? – Hector Sanchez Apr 30 '14 at 17:18
  • 1
    ClientContext is not disposable. – Alex Zhukovskiy Feb 18 '16 at 11:52
  • 3
    @Alex, yes, it is. It derives from [ClientRuntimeContext](https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.clientruntimecontext.aspx), which implements `IDisposable`. – Frédéric Hamidi Feb 18 '16 at 12:06
  • @FrédéricHamidi sorry, it was a MSVS error, it shows that it doesn't implement IDisposable if `Microsoft.Sharepoint.Client.Runtime` is not referenced. – Alex Zhukovskiy Feb 18 '16 at 12:53
  • 1
    check below for the correct answer if you are using SharePoint online: https://sharepoint.stackexchange.com/questions/80961/the-remote-server-returned-an-error-403-forbidden/80964#80964 – gbdavid Apr 20 '18 at 14:25
  • 1
    How can i prompt the end user with supplying the username and password in a secret manner. i dont want to hardcode the username, password in the .cs file. – samolpp2 Aug 29 '18 at 14:35
  • 1
    @SaMolPP Use `SecureString` with the `.Append` property to take the password in as user input via some prompt or modal. – TylerH Apr 09 '19 at 21:11