空sharepoint用户配置文件属性抛出异常

时间:2010-04-14 23:10:39

标签: sharepoint

我有一个用户个人资料属性。用户未分配任何属性值。如果我使用下面的代码。它是Thorwing异常“对象引用未设置为对象的实例”

userprof["OptOut"].ToString()

我尝试了所有类型 喜欢

if (userprof["OptOut"] != null)
          OR
if(userprof["OptOut"].Value != null)

对我来说没什么用。

这里userProf对象具有值。 userprof [“OptOut”]。值为空

如何处理?

1 个答案:

答案 0 :(得分:0)

你必须这样检查:

if (userprof["OptOut"][0] != null)

我使用这种方法:

    private string GetPropertyValue(UserProfile userProfile, string propertyName)
    {
        try
        {
            if (userProfile[propertyName][0] == null)
                {
                 //code like this:
                 //return "'" + propertyName + "' value is null";
                }

            return userProfile[propertyName].ToString();
        }
        catch (Exception ex)
        {
         //code like this:
         //return string.Format("Error with '{0}' UP-property: {1}", propertyName, ex.Message);
        }
        return "-";
    }
相关问题