Passing ViewField names to SPServices

Jun 12, 2015 at 3:00 PM
Hi All,

I have this javascript code to get me the fieldnames from a specific view which works great.
function retrieveFieldsOfListView() {
            var context = new SP.ClientContext.get_current();
            var web = context.get_web();
            // List Name
            var list = web.get_lists().getByTitle("My List");
            //List View Name 
            var view = list.get_views().getByTitle("mobileview");
            var listFields = view.get_viewFields();
            context.load(listFields);
            context.executeQueryAsync(printFieldNames, onError);
            function printFieldNames() {
                var e = listFields.getEnumerator();
                while (e.moveNext()) {
                    var fieldName = e.get_current();
                   
                    alert(fieldName);               
                   // console.log(fieldName);
                }
            }
            function onError(sender, args) {
                console.log(args.get_message());
            }
        }
        retrieveFieldsOfListView();
In this view the user most create a view for mobile only called "mobileview" with 3-4 fields only to show.

Now I want to pass "fieldName" to SPServices to use this as the fields. like so:
$j().SPServices({
            operation: 'GetListItems',
            async: false,
            listName: "My List",
            webURL: "",
            CAMLRowLimit: 100,
            CAMLViewFields: "<ViewFields>" +
                        "<FieldRef Name=' " + fieldName + " ' ></FieldRef>" +   //fieldName: is where we get it from the retrieveFieldsOfListView()                 
                    "</ViewFields>",
            completefunc: function (xData, Status) {
                $j(xData.responseXML).SPFilterNode('z:row').each(function () {
                    var ItemName_1 = $j(this).attr("ows_ " + fieldName + " "); // this should be first field
                    var ItemName_2 = $j(this).attr("ows_ " + fieldName + " "); // this should be second field
                    var ItemName_3 = $j(this).attr("ows_ " + fieldName + " "); // this should be third field
                   
                    // .... etc           
            }
        });
In summery, it must loop throw the fieldnames of that list, then pass it to spservices FieldRef"s".

This topic is also a part of this (http://spservices.codeplex.com/discussions/630693) by passing dynamic fields.

Any help is appreciated.
Thank you.

Karar