Wahlin on .NET

Accessing User Profiles with ASP.NET AJAX

Using AJAX to personalize your Web sites.

Providing users with personalized pages is one of the more popular trends in Web applications today. Many Web sites track the content users want to see and how they'd like it organized, among many other miscellaneous details.

ASP.NET 2.0 provides a profile provider that can be used to easily store and retrieve user preferences without writing a lot of code. As with other ASP.NET providers, the profile provider is easily configured in web.config and can be enabled by adding a <profile> element. Various types of user preferences can be stored by configuring properties and using the profile class to access them. An example of configuring profile properties is shown in Listing 1.

More information about using ASP.NET 2.0 profile functionality can be found here.

The profile provider classes provided by ASP.NET are server-side resources that typically involve standard postback operations as users personalize a site. While you could write a custom AJAX script that calls a Web service to access profile information (see my previous articles on creating and consuming Web services with ASP.NET AJAX), the ASP.NET AJAX extensions provide a Sys.Services.ProfileService client-side class that leverages the XmlHttpRequest object to make asynchronous requests for you. It calls a built-in profile service that allows you to access profile data without writing any custom server-side code. By using the ProfileService class and the associated service, you can use the ASP.NET profile provider to store user preferences while still accessing profile data using AJAX techniques.

To use the ASP.NET AJAX profile service, you must first enable the service and define which profile properties need to be accessed on the client-side. Profile properties can have read and/or write access depending on what configuration code you define in web.config. Listing 2 shows an example of enabling the ASP.NET AJAX profile service in web.config and demonstrates how to expose profile properties. The configuration code allows the Name and ReceiveNewsletter properties to be read and written to using the ASP.NET AJAX ProfileService class.

Now that you've seen how to enable the profile service, let's take a look at properties and methods exposed by the ProfileService class. The following table shows properties of the ProfileService class and describes what they do. 

Property

Description

defaultFailedCallback

Gets or sets the default failure callback function.

defaultLoadCompletedCallback

Gets or sets the default load-completed callback function.

defaultSaveCompletedCallback

Gets or sets the name of the default save-completed callback function.

path

Gets or sets the profile service path.

timeout

Gets or sets the profile service time-out value.

In addition to the properties shown above, the ProfileService class also exposes two methods named load() and save(). The load() method performs the rather obvious task of loading profile property data while the save() method updates profile properties. All of this is done using AJAX asynchronous postback operations. 

The load() and save() methods accept several different parameters as shown next.

ProfileService load() Method Parameters

Parameter

Description

propertyNames

A string array that contains the profile properties to load.  If null, all properties defined in web.config are loaded.

loadCompletedCallback

The method that is called when loading has completed. The default is null.

failedCallback

The method that is called when loading has failed. The default is null.

userContext

User context information passed to the callback methods.

ProfileService save() Method Parameters

Parameter

Description

propertyNames

A string array that contains the profile properties to save.  If null, all properties defined in web.config are passed to the server and saved.

saveCompletedCallback

The method that is called when save() method has completed. The default is null.

failedCallback

The method that is called if the save() method fails. The default is null.

userContext

User context information passed to the callback methods.

Listing 3 shows how the ProfileService class's load() method can be used to load property data from the ASP.NET profile provider using AJAX techniques behind the scenes. Once loaded, the properties can be used to welcome a user back to a site, load user-specific reports, show requested information (such as favorite stocks) and more. The code shown in Listing 3 loads all properties defined in web.config and specifies a success callback method (OnLoadCompleted) as well as a failure callback method (OnProfileFailed).

You can access property data after the call to load() returns by going through the ProfileService class's properties field. Listing 3 shows how the Name property can be accessed and used to update a textbox in a page. It's important to note that only properties defined using the readAccessProperties attribute on the profileService element (see Listing 1) will be accessible on the client-side.

The ProfileService class's save() method can be used to persist user personalization settings back to the ASP.NET profile provider without performing a standard postback operation. Listing 4 shows an example of using the save() method to persist all of the properties defined in web.config back to the ASP.NET profile provider. If the operation completes successfully, the user is notified with an alert box.

By using the profile service capabilities built into the ASP.NET AJAX extensions, you can significantly reduce the amount of code required to access back-end profile data. In this article, you've seen how to configure the profile service and use the ProfileService client-side class to load and save profile properties. Additional details on using the profile service can be found at Microsoft's ASP.NET AJAX site.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.