Hey All,
If you guys want to switch the SharePoint User time and again in your Application(sharepoint application),then this code will hopefully is for you:
protected void Page_Load(object sender, EventArgs e)
{
SPSite spsite = new SPSite("http://YourServer/YourSite");
SPWeb web = spsite.OpenWeb();
string ObjUsr = "YourServer/YourUserName";
string SITE = http://YourServer/YourSite;
SwitchUser(web, SITE, ObjUsr);
}
private void SwitchUser(SPWeb web, string siteStr, string user)
{
SPUserToken userToken = web.AllUsers[user].UserToken;
SPSite s = new SPSite(siteStr, userToken);
SPWeb w = s.OpenWeb();
}
The key here is SPUserToken class. When a new site instance is created ,the second parameter is the UserToken which is assigned.
So here : w.CurrentUser.Name is different from web.CurrentUser.Name
Thanks,
Nitin Sharma