How to use credentials to connect to a SharePoint list using the Client Side Object Model?

93,402

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.
}
Share:
93,402

Related videos on Youtube

J4N
Author by

J4N

Updated on July 09, 2022

Comments

  • J4N
    J4N almost 2 years

    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?

  • chamara
    chamara about 11 years
    hi! what is "yourserver"? can you please check my question below.stackoverflow.com/questions/15737059/…
  • Hector Sanchez
    Hector Sanchez almost 10 years
    is somehting for JSOM?
  • Alex Zhukovskiy
    Alex Zhukovskiy about 8 years
    ClientContext is not disposable.
  • Frédéric Hamidi
    Frédéric Hamidi about 8 years
    @Alex, yes, it is. It derives from ClientRuntimeContext, which implements IDisposable.
  • Alex Zhukovskiy
    Alex Zhukovskiy about 8 years
    @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.
  • gbdavid
    gbdavid about 6 years
    check below for the correct answer if you are using SharePoint online: sharepoint.stackexchange.com/questions/80961/…
  • userAZLogicApps
    userAZLogicApps over 5 years
    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.
  • TylerH
    TylerH about 5 years
    @SaMolPP Use SecureString with the .Append property to take the password in as user input via some prompt or modal.