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

getlistitems with post and camlquery #21

Closed
mdissel opened this issue Jan 7, 2016 · 10 comments
Closed

getlistitems with post and camlquery #21

mdissel opened this issue Jan 7, 2016 · 10 comments

Comments

@mdissel
Copy link
Contributor

mdissel commented Jan 7, 2016

Due to a bug in not expanding Taxonomy fields i have to switch to a camlquery. Currently there's no implementation. Is this planned?

@PauCodina
Copy link
Contributor

At this time there are no plans to implement this functionality in our site.
Could you fork this project and make a pull request with your final implementation.

@mdissel
Copy link
Contributor Author

mdissel commented Jan 7, 2016

i will try. I see there's a camlhelper class but no samples how to use it..

@mdissel
Copy link
Contributor Author

mdissel commented Jan 8, 2016

Hmm. now i swithed to CAMLquery taxonomyfields are working, but expanding lookup columns are not working anymore.. Do you have a tip?

@PauCodina
Copy link
Contributor

Could you show me what are you doing? How you've switched to CAMLQuery?

@PauCodina
Copy link
Contributor

The CamlHelper module provides basic functionality to convert OData sentences into CAML Query expressions.
This module are inspired in SharePoint CAML Query Helper.

Example of use:

var odata = ODataParserProvider.ODataParser(listSchema);
odata.parseExpression({
    filter: 'Country eq USA and Modified eq [Today]',
    orderBy: 'Title asc, Modified desc',
    select: 'Title, Country',
    top: 10
});
camlQueryXml = odata.getCAMLQuery();

This module is used when accessing SharePoint lists through JSOM. When you use REST API this conversion is not necessary and you can use directly OData queries.

@mdissel
Copy link
Contributor Author

mdissel commented Jan 8, 2016

This is my code to load via REST and CAML
MatterType is a taxonomyfield and Client is a lookup field. The Client field is not expanded when using Caml.

        SharePoint.getCurrentWeb().then(function(web) {
            web.getList('Matters').then(function(list) {
                var query = {
                    $filter :"",
                    $top: 10,
                    $select:"Code,Title,Client/Title,MatterType,Created",
                    $expand: "Client",
                    viewXML: "<View Scope='RecursiveAll'><Query></Query></View>"
                };
                list.getListItems(query, true).then(function(items) {
                    $scope.matters = items;
                });
            });
        });

and this is my change getListItems(..)

            if (query.viewXML){

                // Set the contents for the REST API call.
                // ----------------------------------------------------------------------------
                var body = {
                    'query':{ 
                       '__metadata': { 'type': 'SP.CamlQuery' },
                       'ViewXml': query.viewXML
                    } 
                };
                delete query.viewXML;
                urlParams = utils.parseQuery(query);

                // Set the headers for the REST API call.
                // ----------------------------------------------------------------------------
                var headers = {
                    "Accept": "application/json; odata=verbose",
                    "content-type": "application/json;odata=verbose"
                };

                var requestDigest = document.getElementById('__REQUESTDIGEST');
                // Remote apps that use OAuth can get the form digest value from the http://<site url>/_api/contextinfo endpoint.
                // SharePoint-hosted apps can get the value from the #__REQUESTDIGEST page control if it's available on the SharePoint page.

                if (requestDigest !== null) {
                    headers['X-RequestDigest'] = requestDigest.value;
                }


                // Make the call.
                // ----------------------------------------------------------------------------
                executor.executeAsync({

                    url: self.apiUrl + '/GetItems' + urlParams,
                    method: 'POST',
                    body: angular.toJson(body),
                    headers: headers,

                    success: function(data) {

                        var d = utils.parseSPResponse(data);

                        def.resolve(d);
                    },

                    error: function(data, errorCode, errorMessage) {

                        var err = utils.parseError({
                            data: data,
                            errorCode: errorCode,
                            errorMessage: errorMessage
                        });

                        def.reject(err);
                    }
                });

                return def.promise;
            }

@PauCodina
Copy link
Contributor

I think with CAML query is not possible to expand lookup fields.
May be, using SP.CamlQuery and ViewXml, the REST params are ignored.

@mdissel
Copy link
Contributor Author

mdissel commented Jan 8, 2016

the rest params are indeed ignored, but when using pure rest taxonomyfields are not supported.. and i need both, so i have to switch back to JSOM.

@mdissel
Copy link
Contributor Author

mdissel commented Jan 8, 2016

I found a solution to expand both the taxonomyfields and the lookup fields using the JSOM renderListData(..) function.

var ctx = new SP.ClientContext();
                var lst = ctx.get_web().get_lists().getByTitle('Matters');
                ctx.load(lst);
                ctx.executeQueryAsync(function(result){
                    var renderList = lst.renderListData('<View><Query></Query><RowLimit>10</RowLimit></View>');             
                    ctx.executeQueryAsync(function(result){
                        $scope.matters = angular.fromJson(renderList.get_value()).Row;
                    });
                });

@mdissel
Copy link
Contributor Author

mdissel commented Jan 9, 2016

see my pull request #23, converted into a rest call

@mdissel mdissel closed this as completed Jan 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants