How to initialize the client context from another user's account

in sthink •  6 years ago 

Currently I am working on .net web api's that are called from non-SharePoint pages to run SharePoint code to perform certain actions. I have shared configured visual studio so that every one can access the page through my system's IP URL. The code in web api is working fine expect for the fact that it is always taking my account to perform every action.

So how can we run the client context from another users account if I have the domain name of the user.

ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;
User user = web.EnsureUser(@"domain\user1");
clientContext.Load(user);
clientContext.ExecuteQuery();
string title = user.Title;
string loginName = user.LoginName;

If I want to run the clientcontext under this user name. How can i do that. I found the code below that works in server side

SPUserToken userToken = web.AllUsers[user].UserToken;
SPSite s = new SPSite(siteStr, userToken);
SPWeb w = s.OpenWeb();
Console.WriteLine("Currently logged in as: " +
                  w.CurrentUser.ToString() +
                  "(" + w.CurrentUser.Name + ")"
                 );

What is the alternative for this code in CSOM.

Thanks in advance

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!