2

I am trying to convert c# code to javascript. I am able to get list item using javascript but dont know how to filter. I am aware about caml query but cannot use it here as i need to filter same list records with different filter logic. Here is my c# code to fetch list and filter record

c#

using (var site = new SPSite(spWebUrl))
        {
            var web = site.RootWeb;
            var list = web.Lists.TryGetList("mylist");
            listItems = GetListItemsAsList(list.Items);
        }

  var topLevelNodes = listItems.Where(i => ((i["Name"] == null) ? "" : i["Name"].ToString()) == "").ToList();

I want equivalent linq query in javascript.

In javascript i am just able to get list data like below

context = new SP.ClientContext.get_current();
web = context.get_web();

myList = web.get_lists().getByTitle("myList");
3
  • What's your different logic? Oct 20, 2015 at 6:59
  • @AtishDipongkor i meant to say i will have few other filters on same list items i retrieved Oct 20, 2015 at 7:05
  • I mean you want to get items where Name is null or empty? Oct 20, 2015 at 7:17

1 Answer 1

0

Use CAML builder to generate your query and write your function like following

    context = new SP.ClientContext.get_current();
    web = context.get_web();
    myList = web.get_lists().getByTitle("myList");

    var query = "<View><Query><Where><Eq><FieldRef Name='Name' /><Value Type='Text'>''</Value></Eq></Where></Query></View>";
    camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(query);
    items = myList.getItems(camlQuery);
    context.load(items);

You can see this article to get CAML query from view also.

Your LINQ seems to me that your are trying to find items where Name is null or Empty. So in that case your CAML should look like following

<View><Query><Where><Or><IsNull><FieldRef Name='Title' /></IsNull><Eq><FieldRef Name='Title' /><Value Type='Text'>''</Value></Eq></Or></Where></Query></View>
16
  • this will give me filtered list already. What if i wanted to filter on some other things in same list then i have to again get all list with another caml query Oct 20, 2015 at 7:19
  • Nope! single query can contains multiple conditions. Please pass some time with CAML building tools Oct 20, 2015 at 7:24
  • Please see updated question Oct 20, 2015 at 7:26
  • So you have to write two CAML query. One for topLevelNodes and another one for empReportees. So build it using CAML builder Oct 20, 2015 at 7:31
  • but my filter changes as per top level node. How can i build caml query for it. Oct 20, 2015 at 7:36

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.