A recent project has used Silverlight to get the current login in sync mode. The asynchronous approach cannot be combined with other applications. Major sequencing issues. But Silverlight is very bad to get to the current login account. Gets even if it gets to the asynchronous way. Later thought that using JS client Object Model area acquisition is also asynchronous way can be obtained. The code is as follows:
<script type= "text/javascript" >
Sp. Sod.executefunc (' sp.js ', ' sp. ClientContext ', getwebuserdata);
var null;
var null;
var null;
function Getwebuserdata () {
Context =sp. Clientcontext.get_current ();
web = Context.get_web ();
CurrentUser = Web.get_currentuser ();
Currentuser.retrieve ();
Context.load (web);
Context.executequeryasync (Function.createdelegate (this. Onsuccessmethod), Function.createdelegate (this. onfailuremethod));
}
function onsuccessmethod (sender, args) {
var userobject = Web.get_currentuser ();
Alert (' User name: ' + userobject.get_title () + ' \ n ' Login name: ' + userobject.get_loginname ());
document.getElementById ("sl_slcurrentloginname"). Value =userobject.get_title ();
Alert (document.getElementById ("sl_slcurrentloginname"). Value);
}
function onfailuremethod (sender, args) {
Alert (' request failed ' + args.get_message () + ' \ n ' + args.get_stacktrace ());
}
</script>
It is also impossible to control the sequencing with other applications, the online information is written through the Web services can not be obtained, unsuccessful. Actually want to understand is very simple, the following is the solution:
1, self-developed a user control, in the user control with the API function to get the current login account and assign value to the hidden control, as follows:
The code for the. ascx of the user control is as follows:
<input type= "hidden" id= "Sl_slcurrentloginname" value= "<%=_currentLoginName%>"/>
Note: The ID to be unique. cannot be duplicated in master pages and layout pages, and controls for the client
With the control. The CS code is as follows:
To define a global variable:
<summary>
Currentlogin
</summary>
Public string string. Empty;
Page_Load event:
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
Try
{
//
SPUser user = SPContext.Current.Web.CurrentUser;
//
if (user =null)
{
_currentloginname =ssocredentials.getloginname (user. LoginName);
}
}
Catch (Exception ex)
{
}
}
}
2. Deploy the user control to the SP site
3. Deploy the user control to the master page or layout page (embedded in the form of SPD or VS Deployment Master). Here I deploy to master pages, such as:
4, after the deployment is complete, the Silverlight code end with the following function can be done.
string loginName = HtmlPage.Document.GetElementById ("sl_slcurrentloginname"). GetAttribute ("value");
After you deploy the Silverlight package to the SP site, you can get the current login account.
How to get the current login account for the sharepoint2013 site synchronously in Silverlight