pnp / pnpjs

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

SelectProperties not bringing the Required Properties. #2925

Closed frread closed 7 months ago

frread commented 7 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

20

Target environment

SharePoint Framework

Additional environment details

SPFX Webpart to query data from sharepoint online and present it in a listView

Question/Request

I am doing a Search With SharePoint Search Query Tool v2.10.0 this seach is being redirected to
https://<tenant>.sharepoint.com/sites/<site-name>/_api/search/query

this is being send in a get request from what i can see with the following parameters :

querytext='isDocument:True AND siteId: '<siteId>' AND (ItemPath:*Bank*)
selectproperties='Title,FileExtension,LinkingUrl,FileType,UniqueId,Author,Created,LastModifiedTime,COMPANYNAME,CTYCODE,CTYNAME,REGION,SECTOR,SUBSECTOR,SOURCE,ItemPath'

And i am getting the correct results and each result is bring the correct properties.

When i do the same query in pnp sp search the seaach is send to https://<tenant>.sharepoint.com/sites/<site-name>/_api/search/postquery

with send this payload

{"request":{"Querytext":"isDocument:True AND siteId: '<site-id>' AND (ItemPath:*Bank*)",
"SelectProperties":["Title,FileExtension,LinkingUrl,FileType,UniqueId,Author,Created,LastModifiedTime,COMPANYNAME,CTYCODE,CTYNAME,REGION,SECTOR,SUBSECTOR,SOURCE,ItemPath"],
"SortList":[{"Property":"LastModifiedTime","Direction":1}],"RowLimit":10,"HitHighlightedProperties":[],"Properties":[],"RefinementFilters":[],"ReorderingRules":[]}}

I get the correct items, but the SelectedProperties that are managed Properties, i am requesting are comming as null when i know they are not.

this is my SPFX Code

      const builder = SearchQueryBuilder(searchQuery)
        .selectProperties(spFieldsToSelect.join(','))
        .sortList(sortList)
        .rowLimit(this.maxResults);

in this code searchQuery is a string with the queryText, spFieldsToSelect is a array of Strings, sortList is a string, and maxResults is a int with value 10.

Can any one please tell me why the selectedProperties are not being returned?

frread commented 7 months ago

i did a test from Sharepoint Search Query tool using the post and i got the correct results also.

Here is a extract of result i get with the tool

                                        {
                                                "__metadata": {
                                                    "type": "SP.KeyValue"
                                                },
                                                "Key": "COMPANYNAME",
                                                "Value": "GRUPO FINANCIERO BANORTE [GFNORTEO MM]",
                                                "ValueType": "Edm.String"
                                            },
                                            {
                                                "__metadata": {
                                                    "type": "SP.KeyValue"
                                                },
                                                "Key": "CTYCODE",
                                                "Value": "MX",
                                                "ValueType": "Edm.String"
                                            },
                                            {
                                                "__metadata": {
                                                    "type": "SP.KeyValue"
                                                },
                                                "Key": "CTYNAME",
                                                "Value": "Mexico",
                                                "ValueType": "Edm.String"
                                            },
                                            {
                                                "__metadata": {
                                                    "type": "SP.KeyValue"
                                                },
                                                "Key": "REGION",
                                                "Value": "Latin America",
                                                "ValueType": "Edm.String"
                                            },
                                            {
                                                "__metadata": {
                                                    "type": "SP.KeyValue"
                                                },
                                                "Key": "SECTOR",
                                                "Value": "Banking",
                                                "ValueType": "Edm.String"
                                            },
                                            {
                                                "__metadata": {
                                                    "type": "SP.KeyValue"
                                                },
                                                "Key": "SUBSECTOR",
                                                "Value": "Financial Services",
                                                "ValueType": "Edm.String"
                                            },

here is extract of the same fields for the same file result but from the pnpjs sp search

                           {
                                "Key": "COMPANYNAME",
                                "Value": null,
                                "ValueType": "Null"
                            },
                            {
                                "Key": "CTYCODE",
                                "Value": null,
                                "ValueType": "Null"
                            },
                            {
                                "Key": "CTYNAME",
                                "Value": null,
                                "ValueType": "Null"
                            },
                            {
                                "Key": "REGION",
                                "Value": null,
                                "ValueType": "Null"
                            },
                            {
                                "Key": "SECTOR",
                                "Value": null,
                                "ValueType": "Null"
                            },
                            {
                                "Key": "SUBSECTOR",
                                "Value": null,
                                "ValueType": "Null"
                            },
juliemturner commented 7 months ago

Did you check the network call, what is in the payload? If the values are null in the payload, then something about how you're creating the request is wrong. If the values are not null in the payload but you're seeing nulls after the parse happens then you've found a bug, but I can tell you I've used PnPjs search many a time for many things, so I know it works.

frread commented 7 months ago

This is how i am creating my requests

const _sp = (context?: WebPartContext): SPFI => {
    if (!!context) {
        _sp = spfi().using(spSPFx(context)).using(PnPLogging(LogLevel.Warning));
    }

    return _sp;
};

const spFieldsToSelect = ['Title',
    'FileExtension',
    'LinkingUrl',
    'FileType',
    'UniqueId',
    'Author',
    'Created',
    'LastModifiedTime',
    'COMPANYNAME',
    'CTYCODE',
    'CTYNAME',
    'REGION',
    'SECTOR',
    'SUBSECTOR',
    'SOURCE',
    'ItemPath'
];

  const searchQuery = "isDocument:True AND siteId: '" + this.props.siteId + "' AND " + this.searchInput?.value;
  const builder = SearchQueryBuilder(searchQuery)
        .selectProperties(spFieldsToSelect.join(','))
        .sortList(sortList)
        .rowLimit(this.maxResults);

      const results = await this._sp.search(builder);
      console.log(results.PrimarySearchResults);

I got this from the examples in the documentation page

juliemturner commented 7 months ago

Ok, but I was asking you what the browser's network tab showed in the results of that call when it's executed. This library is just a passthrough for the results the api sends you, so if it has null in the values then you're doing something wrong.

I will say that I'm not 100% sure that what you're passing to selectProperites is going to be correct. I think that will be one big string vs strings separated by ,(commas).

i.e. "Title,FileExtension,LinkingUrl,..." vs "Title", "FileExtension", "LinkingUrl"...

Here's an example of a query I've done:

const q: SearchQueryInit = SearchQueryBuilder(`*${searchString}*`)
        .sourceId(this.PEOPLE_SOURCE_ID)
        .selectProperties("SipAddress", "PreferredName", "AccountName")
        .trimDuplicates;
frread commented 7 months ago

this is the biggest facepalm i have had in a long time, you are correct

juliemturner commented 7 months ago

Glad you have it solved.

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