SharePoint / PnP-JS-Core

Code moved to https://github.com/pnp/pnpjs. This repository is archived.
Other
379 stars 231 forks source link

Select and Expand for getItemsByCAMLQuery method #777

Closed ghost closed 6 years ago

ghost commented 6 years ago

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

patrick-rodgers commented 6 years ago

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

patrick-rodgers commented 6 years ago

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

johnhe66 commented 6 years ago

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) => { ... }

johnhe66 commented 6 years ago

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.

johnhe66 commented 6 years ago

The library version I am using is, 3.0.2