How to get the current login account for the sharepoint2013 site synchronously in Silverlight

Source: Internet
Author: User
Tags silverlight

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.