Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
frread opened this issue May 8, 2024 · 3 comments
Closed

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

frread opened this issue May 8, 2024 · 3 comments

Comments

@frread
Copy link

frread commented May 8, 2024

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
Copy link
Collaborator

bcameron1231 commented May 16, 2024

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
Copy link
Collaborator

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.

Copy link

github-actions bot commented Jun 1, 2024

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.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants