pnp / pnpjs

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

How to update Taxonomy field of a folder? #3072

Closed Perneel closed 2 weeks ago

Perneel commented 2 weeks ago

What version of PnPjs library you are using

3.x

Minor Version Number

3.23.0

Target environment

SharePoint Framework

Additional environment details

NodeJS

Question/Request

I'm creating document sets in sharepoint online but I struggle to update a taxonomy field. The steps I'm taking:

  1. Create a folder
  2. Update the folder item metadata (and change the contentTypeId)
export interface IMetaData {
    Label: string;
    TermGuid: string;
    WssId: number;
}

export interface IMeetingObj {
    Id: number;
    ContentTypeId?: string;
    Title: string;
    AcademicYear: IMetaData | undefined;
    Location: string;
    MeetingDate: string | Date;
}

export async function addMeeting(listId: string, listInternalName: string, contentTypeId: string | undefined, item: IMeetingObj): Promise<IMeetingObj> {
    const sp = spfi(getSP());

    // Create a new folder
    const newFolderResult = await sp.web.rootFolder.folders.getByUrl(listInternalName).folders.addUsingPath(item.Title);

    // Get the list item of the newly created folder
    const folderItem = await sp.web.rootFolder.folders.getByUrl(listInternalName).folders.getByUrl(newFolderResult.data.Name).listItemAllFields();

    // Update the metadata of the list item
    const result = await sp.web.lists.getById(listId).items.getById(folderItem.ID).update({
        ContentTypeId: contentTypeId,
        Title: item.Title,
        AcademicYear: item.AcademicYear,
        Location: item.Location,
        MeetingDate: item.MeetingDate
    } as IMeetingObj);

    return await result.item();
}

When executing addMeeting, I get the following error message:

Error making HttpClient request in queryable [400] ::> {"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected."}}}

According to the documentation, taxonomy fields for Files need to be updated differently, does this also count for Folders?

I tried updating the taxonomy field (AcademicYear) seperately like this:

    const result2 = await sp.web.lists.getById(listId).items.getById(folderItem.ID).validateUpdateListItem([
        {
            FieldName: "AcademicYear",
            FieldValue: `${item.AcademicYear?.Label}|${item.AcademicYear?.TermGuid}`,
        }
    ]);

But this seems to result in some strange behaviour, in my list view I see it appear as this:

image

If I edit the item, it seems that the taxonomy field is a string instead of IMetaData (Label, TermGuid, WssId)

Am I doing something wrong?

Perneel commented 2 weeks ago

Seems to be caused due to a wrong field type during migration... Sorry

github-actions[bot] commented 2 weeks 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.