0
votes

I'm trying to filter a SharePoint list so that only the items with the Management field, which holds a string, as "Yes" will show up, but whenever I get to the ctx.ExecuteQuery() statement, my program blows up. I believe my CAMLQuery is structured correctly, so I'm not sure if I'm simply using it wrong or if I'm missing something. Any help would be great! thanks! The code I currently have is posted below:

Web myWeb = ctx.Web;
List myList = myWeb.Lists.GetByTitle("Company Employees");
SPClient.View view = myList.DefaultView;
CamlQuery qry = new CamlQuery();
qry.ViewXml = "<Query>" + "< Where >" + "<Eq>" + "< FieldRef Name='Management'/>" + "< Value Type='Text'>Yes</ Value >" + "</Eq>" + "</ Where >" + "</ Query >";
myList.GetItems(qry);
ListItemCollection listItems = myList.GetItems(qry);
ctx.Load(listItems);
ctx.ExecuteQuery();
2
What type of column is the Management field?Thriggle
I'm not sure if I'm understanding your question. It's just a column that holds a string. I'm just trying to test filtering with it, so I'm checking if the string inside is "Yes"Kyle
Thanks! I wanted to make sure it wasn't a Yes/No field, which would need to be queried differently from Text and Choice fields.Thriggle
Ah okay! I'll edit the question so that's more clear.Kyle
Please post solutions as answers not as updates to your question. I have rolled back your edit which you can see in the revisions. This is to avoid confusion. Thank you.Bugs

2 Answers

2
votes

Your code appears to be missing the <View> tag which would wrap around your <Query> tag in the CAML.

With the addition of the <View> root element, the correct CAML XML would be as follows:

qry.ViewXml = 
    "<View>"+
         "<Query>"+
            "<Where>"+
                 "<Eq>"+
                     "<FieldRef Name='Management'/>"+
                     "<Value Type='Text'>Yes</Value>"+
                 "</Eq>"+
            "</Where>"+
        "</Query>"+
   "</View>";

Additional Troubleshooting

To help troubleshoot, you can try running the same query through the JavaScript client object model.

  1. Visit the SharePoint site in Internet Explorer and hit F5 to open up the developer tools.
  2. On the Console tab, enter the following lines of code and execute (by pressing Enter or Ctrl+Enter) them one line at a time:

-

var ctx = new SP.ClientContext();
var list = ctx.get_web().get_lists().getByTitle("Company Employees");
var qry = new SP.CamlQuery();
qry.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\"Management\"/><Value Type=\"Text\">Yes</Value></Eq></Where></Query></View>");
var items = list.getItems(qry);
ctx.load(items);
ctx.executeQueryAsync(function(){alert("success!");},function(sender,args){alert(args.get_message());});
1
votes

POST HELP SOLUTION Thanks to your help, I was able to figure out how to create a new view with the desired filtering by using the following code. The main problem was with the Caml Query--I had to remove the and tags and then delete a few of the lines before creating the view. Below is my working solution:

Web myWeb = ctx.Web;
List myList = myWeb.Lists.GetByTitle("Company Employees");
SPClient.View view = myList.DefaultView;
CamlQuery qry = new CamlQuery();
qry.ViewXml =
"<Where><Eq><FieldRef Name=\"Management\"/><Value Type='Text'>Yes</Value></Eq></Where>";
ViewCollection viewColl = myList.Views;
string[] viewFields = { "Title", "Promoted", "Intern", "Management" };
ViewCreationInformation creationInfo = new ViewCreationInformation();
creationInfo.Title = "Management";
creationInfo.RowLimit = 50;
creationInfo.ViewFields = viewFields;
creationInfo.ViewTypeKind = ViewType.None;
creationInfo.SetAsDefaultView = false;
creationInfo.Query = qry.ViewXml;
viewColl.Add(creationInfo);
ctx.ExecuteQuery();