I am try to develop a console application in Visual Studio which uses CAML query to get data from SharePoint List. The question is, how does the console application authenticates from the SharePoint site to get List data. Is there any specific code?



By default you will be authenticated as the current user logged in (assuming your on the server, you're probably one of the farm administrator).

You can impersonate to anyone else using this kind of code

//get this current user's user token
SPUserToken userToken = web.AllUsers[*user*].UserToken;

//create an SPSite object in the context of this user
using (SPSite s = new SPSite(siteStr, userToken))

    using (SPWeb w = s.OpenWeb())
       // perform your caml operation on that web.

Otherwise you could simply run the console application as another user (run as command) to retrieve / use the required identity.

Please ensure in all case that the user HAS access to the targeted site collection and can perform the required operation (even just "reading" is not granted for everyone !)

  • Great!! Thanks for the info.This answers my query. Just a small doubt- When using the user token to impersonate. do we need to provide the password too? If not, then isn't it a security breach. :) – Thomas Mathew Oct 31 '12 at 11:36
  • 1
    No, you don't, but you need to ensure that your code is running under the identity of someone who has the ability to obtain other user tokens. But I would run the console application with an adapted account rather than "messing" with impersonation if this is not mandatory ! Why would you need to run under a different identity ? To check the available items just for a particular audience ? – Francois Verbeeck Oct 31 '12 at 13:05
  • "someone who has the ability to obtain other user tokens"- this someone would be an admin rite? That was my question. If anyone can impersonate, then its a security breach. Anyway, thanks for the help. – Thomas Mathew Oct 31 '12 at 13:38
  • 1
    Anyone can impersonate, you don't need that much privileges to be able to grab another user identity (site visitor a.f.a.i remember). On the other side, not everyone is able to launch a compiled application on a SharePoint server and execute it :) – Francois Verbeeck Oct 31 '12 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.