Wednesday, January 13, 2010

Switch Sharepoint Current User programmatically

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

How to Detect/Find Page is Loaded

Hi All,
You my want to know sometimes that is the page loaded completely?
This simple javascript function can let you know:

var body = document.getElementsByTagName('BODY')[0];

if (body && body.readyState == 'loaded')

{
AfterLoad();
}

else
{
if (window.addEventListener)

{
window.addEventListener('load', AfterLoad, false);
}

else
{
window.attachEvent('onload', AfterLoad);
}
}
function AfterLoad()

{
alert("page loaded !!!");
}


Thats it...

Thanks,
Nitin Sharma