ARTICLE AD BOX
I am connecting to a Site and storing the ID
var site = System.Threading.Tasks.Task.Run(() => graphClient.Sites[siteId].GetAsync(requestConfiguration => requestConfiguration.QueryParameters.Expand = new string[] { "drives" })).Result; siteActualId = site.Id;I then iterating through the drives to find the site I am interested in.
Then I process each item and, where it is a folder, I process its items.
When I get an item that is a document, I get its list item
doc = System.Threading.Tasks.Task.Run(() => graphClient.Drives[driveId].Items[item.Id].GetAsync(requestConfiguration => requestConfiguration.QueryParameters.Expand = new string[] { "ListItem" })).Result;Then I use the fields additional data to get the field values
var fields = doc.ListItem.Fields.AdditionalData; values[APPROVAL_COMMENTS] = GetFieldValue(fields, "ApprovalComments"); private string GetFieldValue(IDictionary<string, object> fields, string key, bool date = false) { if (fields.ContainsKey(key)) { if (date) return ((DateTime)fields[key]).AddHours(offset).ToString("yyyy/MM/dd HH:mm:ss"); return fields[key].ToString(); } return string.Empty; }This all works without an issue. When I have a user field however instead of getting the name I get an ID which I then use to find the user’s name by looking in the User Information List
values[APPROVER] = GetUserFieldValue(fields, "DocumentApproverLookupId"); private string GetUserFieldValue(IDictionary<string, object> fields, string lookupId) { if (!fields.ContainsKey(lookupId)) return string.Empty; string id = (string)fields[lookupId]; if (id.Length == 0) return string.Empty; if (!UserNames.ContainsKey(id)) { if (token == null || token?.ExpiresOn < DateTime.UtcNow) token = clientSecretCredential.GetToken(new TokenRequestContext(scopes)); var client = new RestClient($"https://graph.microsoft.com/v1.0/sites/{siteActualId}/lists('User Information List')/items/{id}?$expand=fields($select=Title)"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", "Bearer " + token?.Token); var response = client.Execute(request); if (!string.IsNullOrEmpty(response.ErrorMessage)) throw new Exception("User name lookup failed: " + response.ErrorMessage); int i = response.Content.LastIndexOf(",\"Title\":\""); if (i == -1) UserNames.Add(id, "Unknown"); else { i += 10; int j = response.Content.IndexOf("\"", i); UserNames.Add(id, response.Content.Substring(i, j - i)); } } return UserNames[id]; }This works most of the time but there are some IDs which aren’t in the list.
How would I go about retrieving the user’s name if the Id is not in the site’s User Information List?
