Microsoft.online.directoryservices.directoryvalueexistsexception

microsoft.online.directoryservices.DirectoryValueExistsException:

The DirectoryValueExistsException is an exception class in the Microsoft
Online Directory Services library. This exception is thrown when an attempt is made
to add a value to a directory attribute that already exists.

Example: Let’s say we have a user attribute called “Phone Numbers” in the directory.
Each user is allowed to have multiple phone numbers stored in this attribute. When trying to add a
new phone number for a user, we need to check if the value already exists or not. If it does, this
exception will be thrown to indicate that the value is already present.

Here’s a simple code example using Microsoft’s Online Directory Services:


using Microsoft.Online.DirectoryServices;

...

try
{
    // Assuming 'user' is an instance of User class representing a directory user
    user.PhoneNumbers.Add("1234567890");
}
catch (DirectoryValueExistsException ex)
{
    // Handling existing phone number error
    Console.WriteLine("Phone number already exists for this user.");
}
catch (Exception ex)
{
    // Handling other exceptions
    Console.WriteLine($"An error occurred: {ex.Message}");
}

  

In the above example, when trying to add a phone number to the user’s list of phone numbers,
if the number “1234567890” is already present, the DirectoryValueExistsException
will be thrown. We catch this exception separately from other exceptions for better error handling.

Read more interesting post