我有验证码
SPWeb web = SPContext.Current.Web;
SPTimeZone tz = web.CurrentUser.RegionalSettings.TimeZone;
它对350个用户有效。第二行只有2个显示错误:
对象引用未设置为对象的实例我们所有的用户都在Profile Service中设置了他们的时区。与2 that错误相同。有人知道为什么会这样吗?谢谢
我建议您再次运行“ 用户配置文件服务应用程序代理-用户配置文件到SharePoint语言和区域同步”,然后检查它是否有效。
并且我们可以使用下面的REST API来检查2个用户的配置文件属性,并检查“ SPS-TimeZone”是否为空。
/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='domain\user'
为了避免代码中的问题,建议您使用下面的代码。
SPWeb web = SPContext.Current.Web;
SPTimeZone tz;
if (web.CurrentUser.RegionalSettings != null)
{
tz = web.CurrentUser.RegionalSettings.TimeZone;
}
else if (web.RegionalSettings != null)
{
tz = web.RegionalSettings.TimeZone;
}
else
{
tz = SPRegionalSettings.GlobalTimeZones[web.Site.WebApplication.DefaultTimeZone];
}