I am trying to optimize some code that calls the GetWOPIFrameUrl method for list items that were retrieved using a CAML query. The original code executed the CAML query to get the list of items, then later on made a separate GetWOPIFrameUrl call for each list item. I am trying to combine these to reduce the number of calls as much as possible.
My first attempt was to add the GetWOPIFrameUrl call to the original query like this:
var listItems = list.GetItems(query);
context.Load(listItems,
items => items.Include(
// ... other fields
item => item.File.GetWOPIFrameUrl(SPWOPIFrameAction.Edit)));
But this generates a "query expression is not supported" error.
The second approach I am trying is to create a second query for all of the WOPI URLs and execute it immediately after the first query. This approach works, but it ends up returning duplicate data, which I'd prefer to avoid.
The code for that looks like this:
// The list was queried elsewhere
using (var context = list.Context)
using (authContext.InitiateContext(context))
{
var query = new CamlQuery();
query.ViewXml = "..."; // CAML query
var listItems = list.GetItems(query);
context.Load(listItems,
items => items.Include(
item => item.Id,
item => item.FileSystemObjectType,
item => item.DisplayName,
item => item["Modified"],
item => item.File.ServerRelativeUrl));
context.ExecuteQuery();
var wopiUrls = new Dictionary<ListItem, ClientResult<string>>();
// The useWopi flag is set when WOPI URLs are desired
if (useWopi)
{
foreach (var listItem in listItems)
{
wopiUrls[listItem] = listItem.GetWOPIFrameUrl(SPWOPIFrameAction.Edit);
context.Load(listItem, item => item.Id);
}
// This query includes Id, FileSystemObjectType, DisplayName, and Modified
context.ExecuteQuery();
}
// Do things with listItems and wopiUrls
}
As noted in the comment, the second query is including the FileSystemObjectType, DisplayName, and Modified columns, which are not needed in the second query. It appears that they are inheriting those columns from the original request. I assume that this is happening because I'm reusing the instances returned from the original request to make the second one.
Ideally I'd like to get the single-query approach working. If this is not possible, I'd like to get the second query working without having it return the extra columns.