3

I use a SharePoint list to display a graphics in javascript and I want to get dynamically the name of the columns of my list and not write them in my code. Is there any way to that. To get my list, I use

var list = clientContext.get_web().get_lists().getByTitle(listTitle);
var camlQuery = new SP.CamlQuery();
camlItems = list.getItems(camlQuery);

I try to find a function to get the name/Title of the column but I don't find it.

2
  • Do you want to get internal name of list or name of list columns? Commented Nov 30, 2016 at 11:19
  • I want the name of list colum to use it as axis on my grap^h
    – Marilyn
    Commented Nov 30, 2016 at 13:49

3 Answers 3

2

add to your code

clientContext.load(camlItems);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), 
                                Function.createDelegate(this, function(sender, args){}));        

function onQuerySucceeded(sender, args) {

   var listItemInfo = '';
   var listItemEnumerator = camlItems.getEnumerator();
   listItemEnumerator.moveNext()
   var oListItem = listItemEnumerator.get_current();
   oListItem.get_fieldValues()
}

get_fieldValues() return list af all columsName (internalName)

1
  • thank you for your answer, but the function return all data in the liste name of column and fielsd value. I try to find a function which return only the name of the column of my lits
    – Marilyn
    Commented Nov 30, 2016 at 13:51
1

You can query the REST Service like this:

<weburl>/_api/web/lists/GetByTitle('<listname>')/fields?$select=Name,Title

Example (excludes Hidden and ReadOnlyField fields):

var url = "/_api/web/lists/GetByTitle(' " + listTitle + " ')/fields?$select=Name,Title&$filter=Hidden eq false and ReadOnlyField eq false";
function getFieldPropertis(url) {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            console.log(data.d.results);
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
}
0

for displays the title and internal name of all fields, see https://msdn.microsoft.com/en-us/library/hh185009.aspx?f=255&mspperror=-2147217396#Anchor_4

1
  • Thank you, i try the methods but il retrieve informations for all fields of the list not only the list column. Do you know a method or property i cna use to select only the column of my list.
    – Marilyn
    Commented Dec 1, 2016 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.