Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Contacting users

This topic describes and demonstrates how to get a user's contact info and display it in a set of UI controls.

Users can store their contact info in Live services. By using the Live Connect APIs, your app can use this info to contact a user by email, by mail, or by phone.

The code examples in this topic describe how to get the user's contact info in the languages that the Live Connect APIs support. The examples assume that your app has initialized the APIs successfully, that the user has signed in successfully, and that he or she has consented to these scopes:

  • wl.basic, to get the user's name
  • wl.emails, to get the user's email address
  • wl.postal_addresses, to get the user's postal address info
  • wl.phone_numbers, to get the user's phone number info

For details about these initialization steps, see Signing users in and Obtaining user consent.

Note  The Live Connect APIs return null for any object or structure that does not have a value. We recommend that your app check for null values before it uses a returned value, especially if you're using C#.

To make full use of the code examples in this topic, you can use them in the context of larger code reference samples that we provide in Working with the code examples.

Getting and working with user contact info in JavaScript and HTML for Windows Store apps

In this code example, the WL.api function requests info about me, which returns the signed-in user's profile info.

After the Live Connect APIs return the data to your app, the response function in the WL.api method call inserts the user's info into UI controls.


function contactingUsers_onClick() {
    WL.login({
        scope: ["wl.signin", "wl.basic", "wl.postal_addresses", "wl.phone_numbers", "wl.emails"]
    }).then(
        function (response) {
            WL.api({
                path: "me",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("first_name").innerText = response.first_name;
                    document.getElementById("last_name").innerText = response.last_name;
                    document.getElementById("email").innerText = response.emails.preferred;
                    document.getElementById("address").innerText = response.addresses.personal.street + " " +
                        response.addresses.personal.city + " " +
                        response.addresses.personal.state + " " +
                        response.addresses.personal.postal_code + " " +
                        response.addresses.personal.region
                    document.getElementById("phone").innerText = response.phones.personal;
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        }, 
        function (responseFailed)
        {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}


In this code, response.structure_name in JavaScript is used to get the info from the corresponding structure. For example, response.first_name returns the first name of the signed-in user. Also, note that in JavaScript, response.addresses.personal.postal_code accesses info in the postal_code structure.

Getting and working with user contact info in C# for Windows Store apps and C# for Windows Phone apps

In this code example, the LiveConnectClient.GetAsync method makes a request to me, which returns the signed-in user's contact info.


private async void btnContact_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult = await liveClient.GetAsync("me");
        dynamic result = operationResult.Result;
        firstNameTextBlock.Text = result.first_name;
        lastNameTextBlock.Text = result.last_name;
        emailTextBlock.Text = result.emails.preferred;
        genderTextBlock.Text = result.gender;
        birthdayTextBlock.Text = string.Join(" ", result.birth_month, result.birth_day, result.birth_year);
        addressTextBlock.Text = string.Join(" ",
            result.addresses.personal.street,
            result.addresses.personal.city,
            result.addresses.personal.state,
            result.addresses.personal.postal_code,
            result.addresses.personal.region);
        phoneTextBlock.Text = result.phones.personal;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting user info: " + exception.Message;
    }
}


Getting and working with user contact info in Objective-C for iOS

In this code example, LiveConnectClient getWithPath:delegate requests info about me, which returns the signed-in user's contact info. The liveOperationSucceeded:operation then displays the user's contact info.


-(void) contactUser
{
    [self.liveClient getWithPath:@"me" 
                        delegate:self];
}

- (void) liveOperationSucceeded: (LiveOperation *)operation
{
    NSDictionary *emails = [operation.result objectForKey:@"emails"];
    NSString *preferred = [emails objectForKey:@"preferred"];
    NSDictionary *addresses = [operation.result objectForKey:@"addresses"];
    NSDictionary *personalAddress = [addresses objectForKey:@"personal"];
    NSString *street = [personalAddress objectForKey:@"street"];
    NSString *city = [personalAddress objectForKey:@"city"];
    NSString *state = [personalAddress objectForKey:@"state"];
    NSString *postal_code = [personalAddress objectForKey:@"postal_code"];
    NSString *region = [personalAddress objectForKey:@"region"];
    NSDictionary *phones = [operation.result objectForKey:@"phones"];
    NSString *personalPhone = [phones objectForKey:@"personal"];
    [self.firstNameLabel setText:[NSString stringWithFormat: @"%@", [operation.result objectForKey:@"first_name"]]];
    [self.lastNameLabel setText:[NSString stringWithFormat: @"%@", [operation.result objectForKey:@"last_name"]]];
    [self.emailLabel setText:[NSString stringWithFormat: @"%@", preferred]];
    [self.addressLabel setText:[NSString stringWithFormat: @"%@ %@ %@ %@ %@", street, city, state, postal_code, region]];
    [self.phoneNumberLabel setText:[NSString stringWithFormat: @"%@", personalPhone]];
}


Getting and working with user contact info in Java for Android

In this code example, LiveConnectClient.getAsync requests info about me, which returns the signed-in user's contact info. The custom fillUserContactForm method then displays the user's contact info.


public void contactUser() {
    client.getAsync("me", new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
            resultTextView.setText("Error getting user info: " + exception.getMessage());
        }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            fillUserContactForm(result);
        }
    });
}

private void fillUserContactForm(JSONObject user) {
    String firstName = user.optString("first_name");    
       String lastName = user.optString("last_name");
    JSONObject emails = user.optJSONObject("emails");
    String email = "";
       if (emails != null){
           email = emails.optString("preferred");
       } 
    JSONObject addresses = user.optJSONObject("addresses");
    String address = "";
       if (addresses != null) {
           JSONObject personalAddr = addresses.optJSONObject("personal");
           if (personalAddr != null) {    
               address = personalAddr.optString("street") + " " +
                personalAddr.optString("city") + " " +
                   personalAddr.optString("state") + " " +
                personalAddr.optString("postal_code") + " " +
                personalAddr.optString("region");
           }
       }        
       JSONObject phones = user.optJSONObject("phones");
       String phone = "";
       if (phones != null) {
        phone = phones.optString("personal");
       }
       resultTextView.setText("First name = " + firstName + "\n" +
        "Last name = " + lastName + "\n" +
        "Preferred email = " + email + "\n" +
        "Address = " + address + "\n" +
        "Personal phone = " + phone);
}


Getting and working with user contact info using REST

After you make the appropriate GET call in Representational State Transfer (REST), the user's info is in the response. The response is returned in JavaScript Object Notation (JSON) format, which you can parse to get the individual structures that are stored within.

Here's how to make the GET call. (In this example, the signed-in user is represented as me and ACCESS_TOKEN is the actual access token.

GET https://apis.live.net/v5.0/me?access_token=ACCESS_TOKEN

Objects such as postal_addresses, emails, and phone_numbers contain structures. In addition, the emails object contains multiple structures that correspond to the different email addresses that a user might have in his or her Live Connect profile info. These e-mail accounts include preferred, personal, and business. Here are some email addresses, postal addresses, and phone numbers for a fictional user that could be returned by the Live Connect APIs in JSON format.

{
    ...
    "emails": {
        "preferred": "lukas.abrus@fabrikam.com", 
        "account": "lukas.abrus.account@fabrikam.com",
        "personal": "lukas.abrus.personal@fabrikam.com", 
        "business": "lukas.abrus.business@fabrikam.com" 
    }, 
    "addresses": {
        "personal": {
            "street": "1235 Main St",
            "street_2": null, 
            "city": "Buffalo",
            "postal_code": "98074",
            "state": "NY",
            "region": "USA"
        },
        "business": {
            "street": "One Fabrikam Place",
            "street_2": "Unit 5", 
            "city": "Buffalo",
            "postal_code": "98074",
            "state": "WA",
            "region": "USA"
        }     
    }, 
    "phones": {
        "personal": "(425)555-0101", 
        "business": "(425)555-0155", 
        "mobile": "(425)555-0167"
    }
    ...
}

The REST reference topic includes more info about the elements of the emails, phones, and addresses objects.

Next steps

This topic demonstrates how to use the wl.postal_addresses, wl.email_addresses, and wl.phone_numbers scopes to get a user's contact info. Learn more about:

Top

 

 

Show:
© 2014 Microsoft