Change Query Does Not Have Editor, EditorEmailHint, or EditorLoginName Fields Populated #1281
-
I'm attempting to generate a change report similar to the change history report that's built into Admin center but not exactly like it. I'm trying to output the list of changes occurring each week. So I need to figure out:
This is the code I have so far: ChangeTokenOptions changeToken = new("CHANGE_TOKEN_HERE");
IList? watchPath = await _sharePointContext.Web.Lists.GetByServerRelativeUrlAsync(WatchPath);
IList<IChange> allChanges = await watchPath.GetAllChangesAsync(new ChangeQueryOptions()
{
Add = true,
Move = true,
Restore = true,
DeleteObject = true,
RecursiveAll = true,
Item = true,
FetchLimit = 2000,
IgnoreStartTokenNotFoundError = true,
ChangeTokenStart = changeToken
});
List<IChange> changes = allChanges
.Where(c => c.ChangeType != ChangeType.SystemUpdate
&& c is IChangeItem changeItem
&& changeItem.IsPropertyAvailable<IChangeItem>(p => p.ListId)
&& changeItem.IsPropertyAvailable<IChangeItem>(p => p.ItemId))
.ToList(); The problem is all of the editor fields are var editors = allChanges.Select(c => ((IChangeItem)c).Editor).Distinct().Order().ToList();
var editorEmailHints = allChanges.Select(c => ((IChangeItem)c).EditorEmailHint).Distinct().Order().ToList();
var editorLoginNames = allChanges.Select(c => ((IChangeItem)c).EditorLoginName).Distinct().Order().ToList(); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I did try iterating over each item and seeing if I could just access the "Author" or "Editor" fields instead but not sure if this is ideal over just getting values back for the editor fields on the actual change item? foreach (IChange change in changes)
{
IChangeItem changeItem = (IChangeItem) change;
IList? list = await _sharePointContext.Web.Lists.GetByIdAsync(changeItem.ListId,
p => p.Fields.QueryProperties(
p => p.InternalName,
p => p.FieldTypeKind,
p => p.TypeAsString,
p => p.Title));
IListItem item = await list.Items.GetByIdAsync(changeItem.ItemId,
li => li.FieldValuesAsText,
li => li.File.QueryProperties(f => f.Name, f => f.ServerRelativeUrl),
li => li.Folder.QueryProperties(f => f.Name, f => f.ServerRelativeUrl));
if (item == null)
{
continue;
}
// These contain user models but most fields on them are null
var author = list.Fields.AsRequested().FirstOrDefault(p => p.InternalName == "Author");
var editor = list.Fields.AsRequested().FirstOrDefault(p => p.InternalName == "Editor");
} |
Beta Was this translation helpful? Give feedback.
-
I came up with a hacky approach to this that involves checking a variety of places for an editor name and then falling back to the author's (creator) name on the item I looked up (denoted by // Editor
if (changeItem.IsPropertyAvailable<IChangeItem>(p => p.Editor) && !string.IsNullOrEmpty(changeItem.Editor))
{
EditorName = changeItem.Editor;
}
else if (!isFolder && item.File.ModifiedBy != null)
{
EditorName = item.File.ModifiedBy.Title;
}
else if (item.FieldValuesAsText.Values.TryGetValue("Editor", out object? editor))
{
EditorName = editor as string;
}
else if(item.FieldValuesAsText.Values.TryGetValue("Author", out object? author))
{
EditorName = author as string;
}
// EditorEmail
if (changeItem.IsPropertyAvailable<IChangeItem>(p => p.EditorEmailHint) && !string.IsNullOrEmpty(changeItem.EditorEmailHint))
{
EditorEmail = changeItem.EditorEmailHint;
}
else if (!isFolder && item.File.ModifiedBy != null)
{
EditorEmail = item.File.ModifiedBy.Mail;
} |
Beta Was this translation helpful? Give feedback.
I came up with a hacky approach to this that involves checking a variety of places for an editor name and then falling back to the author's (creator) name on the item I looked up (denoted by
item
) if thechangeItem
did not contain an editor which it never does.