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.

  • Do you want to get internal name of list or name of list columns? – Viraj Gorajia Nov 30 '16 at 11:19
  • I want the name of list colum to use it as axis on my grap^h – Marilyn Nov 30 '16 at 13:49
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)

  • 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 Nov 30 '16 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

  • 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 Dec 1 '16 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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