0

The following piece of sharepoint code is attempting to list all the user profile properties as well as their values

class Program
{
    static void Main(string[] args)
    {
        using (SPSite site = new SPSite(args[0], SPUserToken.SystemAccount))
        {

            var profileManager = new UserProfileManager(SPServiceContext.GetContext(site));
            UserProfile userProfile = profileManager.GetUserProfile(args[1]);

            foreach (var Property in userProfile.Properties)
            {
                Console.WriteLine("Property DisplayName = " + Property.DisplayName + "; " + "Property Name = " + Property.Name);
                if (userProfile[Property.Name] != null)
                {
                    Console.WriteLine("user profile property value " + Property.Name + " is not null");
                    Console.WriteLine("property Value = " + userProfile[Property.Name].ToString());
                }
                else
                {
                    Console.WriteLine("property Value = null");
                }
            }
        }
    }
}

this produces the following output :

output

This shows the exception is coming on the line where it is trying to check if the user profile property value is null

if (userProfile[Property.Name] != null)

But i am already comparing it to null. So why should it give the error that the object is null?

Can someone please give some clarification?

the userProfile is not null , the Property.Name is also not null and i am checking if the userProfile[Property.Name] != null. But it blows up with the error. What on earth is going on?

vivek m
  • 117
  • 1
  • 2
  • 15
  • 2
    I don't have the sharepoint references loaded, but it would see that the indexer function on userProfile is internally throwing a null exception before the comparison to the result of the function is made to null in your if statement. – Greg Bogumil May 20 '16 at 02:27
  • 1
    Are you sure that your PDB files are being rebuilt and that the stack trace is up-to-date? – Mikanikal May 20 '16 at 02:28
  • alternative, try to put in `watch` window this element: `userProfile["ADGuid"]` – Ian May 20 '16 at 02:32
  • @Ian You can tell that it's not null from the output printed before it throws. – Servy May 20 '16 at 02:32
  • I agree with @GrantWinney, It is most likely that the Property.Name is null; – hivie7510 May 20 '16 at 02:33
  • 1
    @GrantWinney We know it's not, because it's printed out as "ADGuid" on the line above. – Servy May 20 '16 at 02:33
  • 1
    @hivie7510 but `Property.Name` can be printed out... – Ian May 20 '16 at 02:33
  • I didn't see the scroll. Definitely a weird problem. – hivie7510 May 20 '16 at 02:44
  • Maybe you're enumerating on the wrong object? According to [UserProfile docs](https://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofile.aspx), it has an enumerator on it's own - maybe try `foreach (var Property in userProfile)`? – qbik May 20 '16 at 02:47
  • @qbik will try but i am wondering - if you are correct then why is it working for the first 2 properties.. it did print the values for the UserProfile_GUID and the SID – vivek m May 20 '16 at 03:08
  • @GrantWinney You don't need to do that. Either the stack trace of the error is the line the OP actually said, or the *actual* stack trace indicates that the error is somewhere else. – Servy May 20 '16 at 03:11
  • @GrantWinney i posted the exact output from the program – vivek m May 20 '16 at 13:29
  • Are you certain the userProfile[Property.Name].Value is not null? – RamblinRose May 20 '16 at 13:39
  • @RamlinRose i think you are right. I updated the code and output in my question. You can see that the error does not occur when it checks for null but it occurs when it tries to print the string output – vivek m May 20 '16 at 17:35

2 Answers2

1

You are assuming userProfile[Property.Name].Value is not null. Try this

if (userProfile[Property.Name] != null 
        && userProfile[Property.Name].Value != null)
    {
        Console.WriteLine("property Value = " + userProfile[Property.Name].Value.ToString());
    }
RamblinRose
  • 4,883
  • 2
  • 21
  • 33
  • from the updated code and output , it is clear that error occurs in this line Console.WriteLine("property Value = " + userProfile[Property.Name].ToString()); Seems like the ToString() implementation displays the Value object like you are saying – vivek m May 20 '16 at 17:37
0

In this line:

if (userProfile[Property.Name] != null)

When Property is null, attempting to get .Name from a null value will throw the NullReferenceException.

Thang Coder
  • 141
  • 1
  • 6