方法一:用C# 代码,可以用于Silverlight:
public void GetCurrentUser()
{
ClientContext context = new ClientContext("http://localhost");
Web site = context.Web;
//context.Load(site);
//context.ExecuteQuery();
if (site != null)
{
User user = site.CurrentUser;
context.Load(user);
context.ExecuteQuery();
string userName = user == null ? string.Empty : user.LoginName;
Console.WriteLine(userName);
}
}
方法二:用ECMAScript Client Object Model:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(GetUser, "sp.js");
var context = null;
var site = null;
var currentUser = null;
function GetUser() {
context = new SP.ClientContext.get_current();
site = context.get_web();
currentUser = web.get_currentUser();
currentUser.retrieve();
context.load(site);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args) {
alert(site.get_currentUser());
}
function onFailureMethod(sender, args) {
alert('failed');
}
</script>