1

I have a List called ProjectInformation and contains following Fields..

ID  ProjectName1  ProjectManager   ProjectExecution

1     EDS           PPB              Time and Material
2     CS            PQR              Fixed Price

I want to retrieve single field as we can do in SQL as :

Select ProjectExecution from ProjectInformation where ProjectName1 ='EDS'

How in CAML Query..??

**My Code **

function rtsample(){
    document.getElementById("tbl").style.visibility="hidden";
    var cc = SP.ClientContext.get_current();
    var alllist = cc.get_web().get_lists().getByTitle('ProjectInformation');
    var caml = new SP.CamlQuery();
    caml.set_viewXml('<View><ViewFields><FieldRef Name="ProjectExecution"/>ViewFields</View>');
    this.list = alllist.getItems(caml);
    cc.load(list,'Include(ProjectExecution)');
cc.executeQueryAsync(Function.createDelegate(this,this.QuerySucceeded5),Function.createDelegate(this,this.onQueryFailed));
    }
    function QuerySucceeded5(){
    var listiteminfo='';

    var listItemEnumerator = list.getEnumerator();
    while(listItemEnumerator.moveNext())
    {
      var listitem = listItemEnumerator.get_current();

     listiteminfo += '\nID:'+ listitem.get_id();
                     alert(listiteminfo.toString());     
    }
    alert(listiteminfo.toString());
    }
    function onQueryFailed(){
    alert("Failed");
    }

Thanks in advance..!!

  • 2
    You have caml.set_viewXmll , should be caml.set_viewXml . '<ViewFields>' is not closed with </viewFields>, same with 'View>' </View> – Anders Aune Sep 19 '14 at 7:33
2

The below code will help you

var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("ProjectInformation");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where>--your query goes here --   </Where></Query></View>');
var collListItem = list.getItems(camlQuery);
context.load(collListItem, 'Include(ProjectExecution)');
context.executeQueryAsync(function () { successCallback() }, function (sender, args) { failureCallback() });

The Include in context.load() is used to fetch only required fields.

P.S. There is also a <ViewField> tag in CAML query which can be used but it also fetches the mandatory columns like Created, Created By etc., so I would recommend to use Include

Source

  • i want only CAML query which i have done in SQL...Rest of the things i know.. – PB-BitWiser Sep 19 '14 at 4:54
  • 1
    Use this camlQuery.set_viewXml('<View><ViewFields><FieldRef Name="ProjectExecution"/></ViewFields></View>'); – Garima Sep 19 '14 at 4:56
  • 1
    I wish I know the reason behind a downvote. – Garima Sep 19 '14 at 5:19
  • Plz, check my code above...still its not working – PB-BitWiser Sep 19 '14 at 5:51
  • What is the error you are getting? You wont get ID column value as it is not included in Include statement. Use Include(ProjectExecution, ID). This is in addition to Anders comment above – Garima Sep 19 '14 at 8:08
0

Because someone already answered to your question, I'll suggest another solution. If you don't mind to use a thrid party library, you can look at my SharepointPlus library. With it you don't need to write the CAML Query but you use a SQL-like syntax for the WHERE clause.

In your case, all the above code will look like this:

$SP().list("ProjectInformation").get({
  fields:"ID,ProjectExecution",
  where:"ProjectName1 = 'EDS'",
  orderby:"ProjectExecution ASC"
}, function(data) {
  for (var i=0; i < data.length; i++) {
    console.log(data[i].getAttribute("ID"), data[i].getAttribute("ProjectExecution"));
  }
})

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.