Article ID: 944106 - Last Review: October 29, 2007 - Revision: 1.0

When you programmatically use the SPUser.RegionalSettings object on a server that is running Windows SharePoint Services 3.0, the value of the returned object is NULL

Expand all | Collapse all

SYMPTOMS

When you programmatically use the SPUser.RegionalSettings object on a server that is running Microsoft Windows SharePoint Services 3.0, the value of the returned object is NULL.

CAUSE

This problem occurs if the user who is defined in the object is not the same user who is logged on to the server. By design, the SPUser.RegionalSettings object is for the current user. Therefore, the current user's regional settings are reported even when you are retrieving the settings of other users.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

To work around this problem, use the following code. This code uses a new SPSite object to create a separate user context.
String GetUserTimeZone(String strUserLogin)
{
        String strResult = String.Empty;
        SPSite site = new SPSite(<Sharepoint Site>);
        SPWeb web = site.OpenWeb();
        SPUser user = web.SiteUsers[strUserLogin];
        site.Dispose();

        using (SPSite siteUser = new SPSite(<Sharepoint Site>, user.UserToken))
        {
		SPWeb webUser = siteUser.OpenWeb();
		SPUser userImp = webUser.SiteUsers[strUserLogin];
		SPRegionalSettings rgnUser = userImp.RegionalSettings;
		strResult = rgnUser == null ? "none" : rgnUser.TimeZone.Description;
         }
        return strResult;
}
Notes
  • This code correctly reports the user's regional settings, regardless of the browsing user's context.
  • After you exit the "using" statement, you must be careful when you work with any objects that are retrieved while the objects are inside the "using" statement. The SPSite object and the underlying request that is used to collect the per-user regional settings will have been discarded. Performing the desired work inside the "using" statement is safest. However, other approaches are possible.

APPLIES TO
  • Microsoft Windows SharePoint Services 3.0
Keywords: 
kbtshoot kbprogramming kbautomation kbprb kbcode kbpubtypekc kbexpertiseinter KB944106
 

Article Translations