pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
742 stars 302 forks source link

Get Version History of a List Item in one request. #3030

Closed frread closed 1 month ago

frread commented 2 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

20

Target environment

SharePoint Framework

Additional environment details

I am creating a SPFX WebPart that runs on Sharepoint Online.

Question/Request

I am doing a Search for files and was working correctly, For the files its important that i reflect the last update date that the file was modify by anyone but an app we have thar runs in the backend.

So what i am doing is, i am getting the ModifiedBy field, but i have run into somes Problems with this.

  1. For some files this does not show me the correct modify by name, it shows me modifier on the file itself and not sharepoint.
  2. And sometimes some files the version does not seem to be correct.
  3. And sometimes because of the volume of files y get a error that seems that the connection has been dropped. so far i working with max of 4000 files in a search result

Here are some examples of my code

async function mapResultToDoc(item: IExtendedSearchResult, rowNum: number, _sp: SPFI): Promise {

const resultDoc: ISPDocument = {
    rowNum: rowNum,
    Title: item?.Title || "",
    FileExtension: item?.FileExtension || "",
    Path: getLink(item),
    AppPath: getLinkApp(item),
    OfficePath: item?.OriginalPath || "",
    FileType: item?.FileType || "",
    id: item?.UniqueId || "",
    ModifiedBy: item?.ModifiedBy || "",
    LastUpdate: item?.LastModifiedTime || undefined,
    siteId: item?.SiteId || "",
    ItemPath: item?.ItemPath || "",
    FileName: item?.FileName || "",

    Ticker: item?.TICKER || "",
    CompanyName: item?.COMPANYNAME || "",
    CtyCode: item?.CTYCODE || "",
    CtyName: item?.CTYNAME || "",
    PmName: item?.PMNAME || "",
    Region: item?.REGION || "",
    Sector: item?.SECTOR || "",
    SubSector: item?.SUBSECTOR || "",
    Source: item?.SOURCE || ""
}

const ModifiedBy = resultDoc.ModifiedBy;
const SPName: string = "SharePoint App".toLocaleLowerCase();

const file = _sp.web.getFileById(item?.UniqueId || "");

if (ModifiedBy.toLocaleLowerCase().indexOf(SPName) !== -1) {
    resultDoc.ModifiedBy = item?.Author || "";
    resultDoc.LastUpdate = item?.Created;

    const versions = await file.versions
        .select("*").orderBy("Created", false)
        .expand("VersionLabel", "CreatedBy", "Created")();

    if (versions.length > 0) {
        let idx = 0;
        let version = undefined;
        let tmpModifiedBy = undefined;

        do {
            version = versions[idx];
            tmpModifiedBy = version?.CreatedBy?.Title || "";
            idx++;
        } while ((tmpModifiedBy.trim().toLowerCase().indexOf(SPName) !== -1) || (idx < versions.length));

        if (version !== undefined) {
            resultDoc.ModifiedBy = tmpModifiedBy;
            resultDoc.LastUpdate = version.Created instanceof Date ? version.Created.toISOString : version.Created;
        }
    }
}

return resultDoc;

}

is there a way i could bring the version history with one request for each file, as maybe a subobject?

bcameron1231 commented 2 months ago

Hi, after reviewing your code that seems to be the correct way to do it.

With that said, your solution could be optimized. Currently you are executing are single request for each file, which is likely the cause of all your performance/timeouts you mentioned in your issue. This would be a great place to do a batch call instead.

You can do batch calls of up to 100 calls at a time through a single request. This would reduce your overall calls from 4,000, down to 40.

Please see our documentation on batching. https://pnp.github.io/pnpjs/concepts/batching/

juliemturner commented 1 month ago

I'm going to close this issue as answered. If you have further issues, please feel free to create a new issue and reference this one.

github-actions[bot] commented 1 month ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.