I'm retrieving drive items from the default document library of multiple SharePoint Online sites. I need, among other things, the user information in the CreatedBy.User
property.
For some sites, I can just access that property without any issue, but for one site the code crashes because the property is null. Coincidentally, this happens on the site with the most drive items.
These are the relevant code sections.
var items = await GetChildren(_client.Sites[siteId].Drive.Root.Children).ToListAsync();
private static async IAsyncEnumerable<DriveItem> GetChildren(IDriveItemChildrenCollectionRequestBuilder builder)
{
var page = builder.Request()
.Expand(d => d.ListItem)
.GetAsync().Result;
foreach (var driveItem in page)
{
yield return driveItem;
}
while (page.NextPageRequest is not null)
{
page = await page.NextPageRequest.GetAsync();
foreach (var driveItem in page)
{
yield return driveItem;
}
}
}
How can I force the request to always include the CreatedBy.User
information? When I additionally request that specific property it does work, but doing so many separate calls is not really an option. There are just too many items in that document library.
driveItem
resource has relationship createdByUser
which is identity of the user who created the item.
You can expand createdByUser
to avoid additional request.
...
var page = builder.Request()
.Expand("listItem,createdByUser")
.GetAsync().Result;
...
Got it to work without sending a request for every single CreatedBy
, though I'm not thrilled with the solution.
It turns out, that at least for that particular site, CreatedBy.User
was null only if I expanded the ListItem
. If I left out the Expand
, CreatedBy.User
was returned just fine.
So I decided to collect all ListItems
, in addition to the DriveItems
.
private async IAsyncEnumerable<ListItem> GetListItems(IListItemsCollectionRequestBuilder builder)
{
var page = await builder
.Request()
.Expand("fields")
.GetAsync();
foreach (var listItem in page)
{
yield return listItem;
}
while (page.NextPageRequest is not null)
{
page = await page.NextPageRequest.GetAsync();
foreach (var listItem in page)
{
yield return listItem;
}
}
}
When looking at a DriveItem
, I pull the matching ListItem
from the list. This way I get all the information I need. It works, but I'd prefer the option of expanding the ListItem
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.