SharePoint/PnP-JS-Core

Select and Expand for getItemsByCAMLQuery method

Closed this issue · 5 comments

Category

[X] Enhancement

[ ] Bug

[ ] Question

Version

Please specify what version of the library you are using: [ 3.0.4 ]

Expected / Desired Behavior / Question

Select and Expand methods are not available for getItemsByCAMLQuery method

Observed Behavior

Methods not available

Steps to Reproduce

Trying to use ODATA and CAML together as shown in the below article but seems like it cannot be used with sp-pnp-js.
https://piyushksingh.com/2018/03/04/odata-expand-not-working-sharepoint-rest-api/

            const web = new Web(Options.WebUrl);
            const list = web.lists.getByTitle(Options.ListTitle);
            list.items.getById(discussionId).folder.serverRelativeUrl.get()
                .then((url) => {
                    console.log(url);
                    const camlQuery: CamlQuery = {};
                    camlQuery.ViewXml = `<View Scope='Recursive'>
                            <Query>
                                <Where>
                                    <Eq>
                                        <FieldRef Name='FileDirRef' />
                                        <Value Type='Text'>${url.ServerRelativeUrl}</Value>
                                    </Eq>
                                </Where>
                            </Query>
                        </View>`;

  
         list.getItemsByCAMLQuery(camlQuery)
        // Below line doesn't work
        .select('FieldValuesAsText/Author,FieldValuesAsText/ParentItemEditor')
        .then((data) => {
                        resolve(data);
                    }).catch(error => {
                        reject(error);
                    });

Thanks!
Sri Hari

Hi @vpsrihari -

Yes, select is not available on the CAML query method as you make those specifications in the CAML itself. As for expands you can pass additional parameters to the method for expanding fields:

// as an example
list.getItemsByCAMLQuery(camlQuery, "Author/Email", "Author/Name");

HTH,

Patrick

Going to close this as answered, please reopen should you need to continue the conversation. Thanks!

has anyone had a successful test based on Patrick's response? I tried to use it to get CAML and Auth info, but got "Bad Request" Error. The code is below,

let list = web.lists.getByTitle(listName);
return list.getItemsByCAMLQuery(CamlQuery,'Author/Name').then((res) => { ... }

The rest call URL looks like this,
/sites/myside/_api/web/lists/getByTitle('Issues')/getitems?$expand=Author/Name HTTPS POST 400 application/json 1.05 KB 437 ms XMLHttpRequest 4618 218 141 78 0 0

There is also a valid post body to go along with this post request.

The library version I am using is, 3.0.2