0

So far I can retrieve the list based on a string I pass and even the column names but I can't figure out how to get the values of a specific column. Here is what I have so far.

function GetFieldList()
{
  var listname = document.getElementById("ListName").value;
  var ctx = SP.ClientContext.get_current();
  this.web = ctx.get_web();
  ctx.load(this.web);
  this.list = web.get_lists().getByTitle(listname);
  ctx.load(this.list);
  this.fields = this.list.get_fields();
  ctx.load(this.fields); 
  ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

Btw I'm using SharePoint 2010.

3
  • Did you check if SP.ClientContext.get_current(); returns a Javascript Object? Commented Aug 1, 2017 at 21:06
  • I used the typeof method to check what datatype ctx is and got object as a result. Does that help? Commented Aug 2, 2017 at 14:20
  • @myEdu Are you trying to fetch data from particular field/column in sharepoint list ? Commented Aug 3, 2017 at 7:03

3 Answers 3

1

I think your code is not full the client Context must run async method to load the values If you want correct way to get the values from SharePoint read the this documentation : https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx
or you can use another library such as rest api or spservice .
Anyway the get_fields() return the fields list name not values.

Sign up to request clarification or add additional context in comments.

2 Comments

agreed! there is no method to parse the results from the query
added method to parse the query.
0

As you said SP.ClientContext.get_current(); is an Javascript Object, so first of all you need to check if the property that you want to know if is null or empty, exists.

var ctx = SP.ClientContext.get_current();
isPropertyEmptyOrNull = ctx.hasOwnProperty('myProperty') && (prop.myProperty === null or prop.myProperty === '' or prop.myProperty === undefined)

You found more details abour hasOwnProperty here

Comments

0

Refer below code. This shows how to retrieve items from SharePoint list.

function GetItems() {
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle('ListName');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("");
    collItems = list.getItems(camlQuery);
    context.load(collItems);
    context.executeQueryAsync(GetItemsSuccess, GetItemsFail);
}

function GetItemsSuccess() {

    var listItemEnumerator = collItems.getEnumerator();

    if (collItems.get_count() > 0) {

        while (listItemEnumerator.moveNext()) {

            var oListItem = listItemEnumerator.get_current();

            /*
                retrieve specific fields (e.g. Title, FirstName...)
                Here 'Title' and 'FirstName' will be the internal name of the field you want to retrieve
            */
            var title = oListItem.get_item('Title');
            var firstName = oListItem.get_item('FirstName');

        }
    }
}

function GetItemsFail(sender, args) {
    // Error handler code 
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.