Search code examples
c#azuremicrosoft-graph-apimicrosoft-graph-sdks

How to Extract Fields from SensitivityLabelEventData within AuditData in Microsoft Graph SDK?


I'm working with the Microsoft Graph Beta SDK in a .NET project, where I'm trying to extract specific fields from SensitivityLabelEventData inside the AuditData property of an AuditLogRecord. However, I'm having trouble accessing the data within SensitivityLabelEventData, which is stored as a Microsoft.Kiota.Abstractions.Serialization.UntypedObject.

var autditLogRecords = gsc.Security.AuditLog.Queries[auditLog_Search_id].Records.GetAsync().GetAwaiter().GetResult();

foreach (var auditLogRecord in autditLogRecords.Value)
{
    var additionalData = auditLogRecord.AuditData.AdditionalData;
    var untypedObject = additionalData["SensitivityLabelEventData"] as Microsoft.Kiota.Abstractions.Serialization.UntypedObject;
    if (untypedObject != null)
    {
        // Attempt to access properties within UntypedObject
        var labelEventType = untypedObject.ToString(); // Intended to get the 'labelEventType'
        var sensitivityLabelId = untypedObject.ToString(); // Intended to get the 'SensitivityLabelId'
        var sensitivityLabelOwnerEmail = untypedObject.ToString(); // Intended to get the 'SensitivityLabelOwnerEmail'

        // Setting parameters for SQL command
        comm.Parameters.Add("@SensitivityLabelEventData_LabelEventType", SqlDbType.Int).Value = !string.IsNullOrEmpty(labelEventType) ? Convert.ToInt32(labelEventType) : DBNull.Value;
        comm.Parameters.Add("@SensitivityLabelEventData_SensitivityLabelId", SqlDbType.VarChar, 36).Value = !string.IsNullOrEmpty(sensitivityLabelId) ? sensitivityLabelId : (object)DBNull.Value;
        comm.Parameters.Add("@SensitivityLabelEventData_SensitivityLabelOwnerEmail", SqlDbType.NVarChar, 255).Value = !string.IsNullOrEmpty(sensitivityLabelOwnerEmail) ? sensitivityLabelOwnerEmail : (object)DBNull.Value;
    }
}

The issues I'm encountering:

Accessing Properties: I attempted to access properties like labelEventType and SensitivityLabelId by treating them as strings or integers after converting the UntypedObject to a string, but this doesn't seem to be the right approach.

Property Access Error: I've tried casting UntypedObject to UntypedString or UntypedInteger to access the data directly, but I'm not able to retrieve the actual values.

My question is: What is the correct way to extract the values (e.g., labelEventType, SensitivityLabelId, SensitivityLabelOwnerEmail) from the SensitivityLabelEventData object inside AuditData? How should I handle UntypedObject in this context to correctly access these nested fields?

Any guidance or examples would be greatly appreciated!


Solution

  • UntypedObject has the method GetValue which returns a IDictionary<string, UntypedNode>. It's a collection of properties. Based on the doc, auditData is a JSON object that contains the actual audit log data. The Graph API doesn't know what properties can contain and that's the reason why the SensitivityLabelEventData object is represented as UntypedObject in the SDK.

    To access the properties inside the SensitivityLabelEventData object, call GetValue to get a dictionary with properties. All properties have a generic type UntypedNode, but you can cast it to a specific type like UntypedInteger or UntypedString and call GetValue.

    if (untypedObject != null)
    {
        var properties = untypedObject.GetValue();
        var labelEventType = (properties["labelEventType"] as UntypedInteger).GetValue(); // Intended to get the 'labelEventType'
        var sensitivityLabelId = (properties["SensitivityLabelId"] as UntypedString).GetValue(); // Intended to get the 'SensitivityLabelId'
        var sensitivityLabelOwnerEmail = (properties["SensitivityLabelOwnerEmail"] as UntypedString).GetValue(); // Intended to get the 'SensitivityLabelOwnerEmail'
    
        // Setting parameters for SQL command
        comm.Parameters.Add("@SensitivityLabelEventData_LabelEventType", SqlDbType.Int).Value = labelEventType;
        comm.Parameters.Add("@SensitivityLabelEventData_SensitivityLabelId", SqlDbType.VarChar, 36).Value = sensitivityLabelId;
        comm.Parameters.Add("@SensitivityLabelEventData_SensitivityLabelOwnerEmail", SqlDbType.NVarChar, 255).Value = sensitivityLabelOwnerEmail;
    }