In Server Side Object Model
string username;
string sspURL = "URL where Sharepoint is deployed";
site = new SPSite(sspURL);
web = site.OpenWeb();
SPUser user = web.CurrentUser;
username = user.LoginName;
site.AllowUnsafeUpdates = true;
OR
SPContext.Current.Web.CurrentUser.Name;
OR
SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
string strUserName = theUser.LoginName;
In Client Side Object Model
function CallClientOM()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded(sender, args)
{
alert(currentUser.get_loginName());
}
function onQueryFailed(sender, args)
{
alert('request failed'+args.get_message()+'\n'+args.get_stackTrace());
}
No comments:
Post a Comment