SPServices

SPServices it is javascript framework that able to accessing SharePoint object and it more simplify to you on coding it.

Here is the source code:

1. Get user info then auto fill to controls

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var displayName = "";
function GetUser()
{
    displayName = $().SPServices.SPGetCurrentUser({
    fieldName: "Title"
    });    
    var ctx = new SP.ClientContext.get_current();
    this.currentUser = ctx.get_web().get_currentUser();
    ctx.load(this.currentUser);
    ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceededGetUser),
    Function.createDelegate(this, this.onQueryFailed));
}
function onSucceededGetUser(sender, args)
{
    //alert("The users " + this.currentUser.get_loginName());  
    $("input[Title='Title']").val(displayName);
    $("input[Title='Account']").val(this.currentUser.get_loginName()); 
    $("input[Title='Email']").val(this.currentUser.get_email());       
}
function onQueryFailed(sender, args)
{
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}  

2. Update user email info

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function UpdateUserEmail()
{          
    var ctx = new SP.ClientContext.get_current();
    this.currentUser = ctx.get_web().get_currentUser();
    var email = $("input[Title='Email']").val();
    this.currentUser.set_email(email);
    this.currentUser.update(); 
    ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceededUpdateUserEmail),
    Function.createDelegate(this, this.onQueryFailed));
}
function onSucceededUpdateUserEmail(sender, args)
{
    alert("Updated! " + this.currentUser.get_email());     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.