空的sharepoint用户配置文件属性引发异常

空的sharepoint用户配置文件属性引发异常,sharepoint,Sharepoint,我有一个用户配置文件属性。用户未为该属性分配任何值。如果我使用下面的代码。异常为“对象引用未设置为对象的实例” 我试了所有的类型 像 我一事无成 这里userProf对象具有值。userprof[“OptOut”]。值为空 如何处理此问题?您必须这样检查: if (userprof["OptOut"][0] != null) 我使用这种方法: private string GetPropertyValue(UserProfile userProfile, string property

我有一个用户配置文件属性。用户未为该属性分配任何值。如果我使用下面的代码。异常为“对象引用未设置为对象的实例”

我试了所有的类型 像

我一事无成

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


如何处理此问题?

您必须这样检查:

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 "-";
    }
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 "-";
    }