Here is the code I’m using to retrieve the current username from SharePoint 2010 via Silverlight:
ClientContext clientContext = ClientContext.Current;
if (clientContext == null)
{
SharepointUser = "[Unknown]";
}
else
{
MessageBox.Show("Beginning server query now...");
clientContext.Load(clientContext.Web, s => s.CurrentUser);
clientContext.ExecuteQueryAsync((s, e) =>
{
MessageBox.Show("RESPONSE!");
SharepointUser = clientContext.Web.CurrentUser.LoginName;
MessageBox.Show("Hello, " + SharepointUser + "!");
},
(s, e) =>
{
MessageBox.Show("An error occurred: " + e.ToString());
});
}
The problem is that the request never comes back! The success/error events don’t trigger at all, and the request just seems like it’s going into the void.
Does anyone have any ideas?
MessageBox.Show is probably the cause of the issue. I was having the same issue and by debugging I found that the calls the MessageBox.Show within the ExecuteQueryAsync callbacks were throwing an exception “Invalid cross-thread access”. I needed to wrap the calls like this to get it to work: