0

so I'm working on SharePoint Online integration using MS Graph. My issue is the way MS Graph present field values for hyperlinks. I'm not too sure how to handle the parsing of these dynamic fields. Please see below code snippets.

The MS Graph JSON response for SharePoint list fields:

 "fields": {
            "@odata.etag": "\"54e4e975-d364-4064-a2f0-63172bd74836,13\"",
            "FileLeafRef": "FILENAMEBOYS.jpg",
            "Title": "Trees",
            "Column_x0020_For_x0020_Text_x0020_Testing": "awdada",
            "link": {
                "Description": "https://stackoverflow.com/questions/ask",
                "Url": "https://stackoverflow.com/questions/ask"
            },
            "YES_x0020_OR_x0020_NO": true,
            "id": "1",
            "ContentType": "Document",
            "Created": "2018-06-28T16:57:34Z",
            "AuthorLookupId": "6",
            "Modified": "2018-07-19T13:59:12Z",
            "EditorLookupId": "6",
            "_CheckinComment": "",
            "LinkFilenameNoMenu": "FILENAMEBOYS.jpg",
            "LinkFilename": "FILENAMEBOYS.jpg",
            "DocIcon": "jpg",
            "FileSizeDisplay": "1048862",
            "ItemChildCount": "0",
            "FolderChildCount": "0",
            "_ComplianceFlags": "",
            "_ComplianceTag": "",
            "_ComplianceTagWrittenTime": "",
            "_ComplianceTagUserId": "",
            "_CommentCount": "",
            "_LikeCount": "",
            "Edit": "0",
            "_UIVersionString": "12.0",
            "ParentVersionStringLookupId": "1",
            "ParentLeafNameLookupId": "1"
        }

You'll see in the above snippet, the field named Link is JSON, but the result of the other field values are just normal strings. So I currently use IDictionary<string, object> to parse the data, due to the fact that I'm not sure what the field value will hold.

Now when I bind my IDictionary field collection to my data grid, I get the below result. (As expected, cause Link has not been properly parsed. Just thrown to type Object.)

Grid Result for IDictionary<string, object> binding

Q : I'm not too sure how to go about parsing the values to enable me to bind directly to the hyperlink URL. Should I be doing some kind of a TryParse to check if it's a hyperlink (or any other kind of expected result type) before parsing and storing it as a object.

Just to add to the above, the column JSON result for Hyperlink columns has no indication what type it is. Added the columns JSON below as refrence.

The MS Graph JSON response for SharePoint list column LINK:

{
      "columnGroup": "Custom Columns",
      "description": "",
      "displayName": "link",
      "enforceUniqueValues": false,
      "hidden": false,
      "id": "97a58edb-1f5c-4e46-b843-fc9827847088",
      "indexed": false,
      "name": "link",
      "readOnly": false,
      "required": false
}
2
  • Did my answer helped you solve your problem? If so, please mark it as accepted. Commented Aug 31, 2018 at 9:08
  • 1
    Ah, apologies. I thought I did do so. I did so now. Thanks. Commented Aug 31, 2018 at 9:32

1 Answer 1

1

As you already figured, you'll have to somehow parse the nested objects. There are several ways you can go about doing that. Here's one (link to fiddle):

using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq; 

public class Program
{
    public static void Main()
    {       
        string someJson =@"{
            ""FileLeafRef"": ""FILENAMEBOYS.jpg"",
            ""link"": {
                ""Description"": ""https://stackoverflow.com/questions/ask"",
                ""Url"": ""https://stackoverflow.com/questions/ask""
            }
        }";
        var jObject = JObject.Parse(someJson);
        var parentDict = jObject.ToObject<Dictionary<string, object>>();
        foreach (var parentPair in parentDict) 
        {
            Console.Write(string.Format("Key: {0}, ", parentPair.Key));
            if (parentPair.Value is JObject)
            {
                Console.WriteLine();
                var childDict = ((JObject)parentPair.Value).ToObject<Dictionary<string, string>>();
                foreach (var childPair in childDict) 
                {
                    Console.WriteLine(string.Format("\tKey: {0}, Value: {1}", childPair.Key, childPair.Value));
                }
            }
            else if (parentPair.Value is string)
            {
                Console.WriteLine(string.Format("Value: {0}", parentPair.Value));
            }
        }
    }
}

Output:

Key: FileLeafRef, Value: FILENAMEBOYS.jpg 
Key: link,
    Key: Description, Value: https://stackoverflow.com/questions/ask
    Key: Url, Value: https://stackoverflow.com/questions/ask 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.