none
SharePoint App. query.set_viewXml is retrieving all list items. . RRS feed

  • Question

  • Hi All,

    I am making a SharePoint app and able to select SharePoint list items using CSOM with 5 total list count.  

    I am updating list items, use a dynamic CAML query and CAML query works fine with selected 2 IDs as shown:


    However, when I set the query.set_viewXml(finalQuery) I am getting all the 5 items results although I have selected only selected list items as shown:




    Similar post was there on http://sharepoint.stackexchange.com/questions/97435/query-set-viewxml-retrieving-everything

    Any suggestions?

    Thank you.

    Sandy

    function makeCAML()
    {
        var queryMainFirst = "";
        var queryFilter = "";
        var queryMainLast = "";
    
    
        var listItemIds = decodeURIComponent(getQueryStringParameter("SPListItemId"));
        var delimiterlistItemIds = listItemIds.split(',');
    
    
        for (var i = 0; i < delimiterlistItemIds.length ; i++)
        {
            queryMainFirst = "<View><Where><Or>";
            queryMainLast = "</Or></Where></View>";
            listItemIds = decodeURIComponent(getQueryStringParameter("SPListItemId"));
    
            queryFilter += "<Eq><FieldRef Name='ID' /><Value Type='Counter'>" + delimiterlistItemIds[i] + "</Value></Eq>";
    
        }
    
        var finalQuery = queryMainFirst.concat(queryFilter, queryMainLast);
    
    
        try {
    
            //Get the Host Web Objects and update Workflow Tasks items. 
    
            var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    
            var ctx = SP.ClientContext.get_current();
            var hostContext = new SP.AppContextSite(ctx, hostUrl);
            var web = hostContext.get_web();
            var listWF = web.get_lists().getByTitle("Workflow Tasks"); 
            var items = null;
    
            //Create CAML Object
    
            var query = new SP.CamlQuery();
            query.set_viewXml(finalQuery);
            //query.set_viewXml("<Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>5</Value></Eq><Eq><FieldRef Name='ID' /><Value Type='Counter'>8</Value></Eq></Or></Where>");
            
    
            var qitems = listWF.getItems(query);
            items = ctx.loadQuery(qitems);
            alert(items.length);
    
            ctx.executeQueryAsync(success1, fail);
    
    
        }
        catch (ex)
        { ex.message}
    
        function success1()
        {
            if(items.length > 0)
            {
                var item = items[0];
                item.set_item("Status", "Completed");
                item.set_item("PercentComlete", 1);
                item.set_item("WorkflowOutcome", "Approved");
                item.update();
    
            }
    
            //ctx.executeQueryAsync(success2, fail);
        }
        
        function fail(sender, args) {
            alert("Call failed. Error: " +
                args.get_message());
        }
    
        function success2() {
            var message = jQuery("#message");
            message.text("Item updated");
        }
    
    }


    • Edited by Sandy 791 Tuesday, December 20, 2016 10:00 AM Added source code
    Tuesday, December 20, 2016 9:53 AM

Answers

All replies

  • please use it as a method and not as a property

    http://sharepoint.stackexchange.com/questions/97435/query-set-viewxml-retrieving-everything


    Please remember to click Mark as Answer on the answer if it helps you

    Tuesday, December 20, 2016 6:17 PM
  • Hi,

    You need add <Query> in your query string. Below is sample code for your reference:

    using (var context = new ClientContext("http://sp:12001"))
                {                
                    //context.Credentials = CredentialCache.DefaultCredentials;
                    List listWF = context.Web.Lists.GetByTitle("Employee");
                    // Get selectedValues
                    string selectedValues = "2,3,5";
                    string queryMainFirst = "";
                    string queryFilter = "";
                    string queryMainLast = "";
    
                    queryMainFirst = "<View><Query><Where>";
                    queryMainLast = "</Or>";
                    string[] values = selectedValues.Split(',');
                    for (int i = values.Length - 1; i >= 0; i--)
                    {
                        values[i] = values[i].Trim();
                        if (i < 2)
                        {
                            if (i == 1)
                                queryFilter += "<Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + values[i].Trim() + "</Value></Eq>";
                            else
                                queryFilter += "<Eq><FieldRef Name='ID' /><Value Type='Counter'>" + values[i].Trim() + "</Value></Eq>";
                        }
                        else
                        {
                            queryFilter += "<Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + values[i].Trim() + "</Value></Eq>";
                            queryMainLast += "</Or>";
                        }
                    }
    
                    //TRIED TO MAKE CAMLE QUERY Dynamic 
                    string finalQuery = queryMainFirst + queryFilter + queryMainLast + "</Where></Query></View>";
    
                    // THESE below TWO Statements both the strings Hard coded and Dynamic are EXACTLY same. 
                    CamlQuery queryDynamic = new CamlQuery();
                    queryDynamic.ViewXml = finalQuery;
    
                    ListItemCollection itemsColl = listWF.GetItems(queryDynamic);
                    context.Load(itemsColl);
                    context.ExecuteQuery();
                    Console.WriteLine("Dynamic CAML, Items in list: " + itemsColl.Count);
    
                    foreach (ListItem item in itemsColl)
                    {
                        Console.WriteLine("ID is: " + item["ID"].ToString());
                    }
                    Console.ReadKey();
                }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 22, 2016 8:28 AM
  • Hi Lee and Lakshmanan, 

    Thanks for your valuable inputs. 
    @Lee: I tried your option but it did not work. 

    However, I recheck the code, I found the issue. 
    I had to supply the RowFilter option and it worked.

    Here is the code for that: 

    function updateItems()
    {
        var queryMainFirst = "";
        var queryFilter = "";
        var rowLimit = "";
        var queryMainLast = "";
    
        //Get All the Selected IDs
        var listItemIds  = decodeURIComponent(getQueryStringParameter("SPListItemId"));
        var delimiterlistItemIds = listItemIds.split(',');
    
        //Construct Dynamic CAML Query 
        for (var i = 0; i < delimiterlistItemIds.length ; i++) {
            queryMainFirst = "<View><Where><Or>";
            queryMainMiddle = "</Or></Where>"
            rowLimit = delimiterlistItemIds.length;
            queryMainLast = "</View>";
            listItemIds = decodeURIComponent(getQueryStringParameter("SPListItemId"));
            queryFilter += "<Eq><FieldRef Name='ID' /><Value Type='Counter'>" + delimiterlistItemIds[i] + "</Value></Eq>";
    
        }
        rowLimit = "<RowLimit>" + rowLimit + "</RowLimit>"
    
        var finalQuery = queryMainFirst.concat(queryFilter, queryMainMiddle, rowLimit, queryMainLast);
    
        //Get the Host Web Objects and update Workflow Tasks items. ;
        hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    
        clientContext = SP.ClientContext.get_current();
        var hostContext = new SP.AppContextSite(clientContext, hostUrl);
        var web = hostContext.get_web();
        listWF = web.get_lists().getByTitle("Workflow Tasks");
    
        //Create CAML Object
        var query = new SP.CamlQuery();
        //query.set_viewXml("<View><Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>10</Value></Eq></Or></Where><RowLimit>1</RowLimit></View>");
    
        var query = new SP.CamlQuery();
        var formattedQuery = String.format(finalQuery)
        query.set_viewXml(formattedQuery);
    
        collListItem = listWF.getItems(query);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);




    Sandy


    • Edited by Sandy 791 Sunday, December 25, 2016 5:15 AM updated the content
    Sunday, December 25, 2016 4:39 AM
  • Hi,

    You haven’t add  <Query> yet, could you try that.

    My test query:

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, December 26, 2016 1:26 AM
  • Lee, 

    You are the MAN!! 

    That was the issue that I was bothering for me for last 2 days. 
    Thanks a ton!! 

    function updateItemsNew()
    {
        var queryMainFirst = "";
        var queryFilter = "";
        var rowLimit = "";
        var queryMainLast = "";
    
        //Get All the Selected IDs
        var listItemIds  = decodeURIComponent(getQueryStringParameter("SPListItemId"));
        var delimiterlistItemIds = listItemIds.split(',');
    
        //Construct Dynamic CAML Query 
        for (var i = 0; i < delimiterlistItemIds.length ; i++) {
            queryMainFirst = "<View><Query><Where><Or>";
            queryMainMiddle = "</Or></Where></Query>"
            rowLimit = delimiterlistItemIds.length;
            queryMainLast = "</View>";
            listItemIds = decodeURIComponent(getQueryStringParameter("SPListItemId"));
            queryFilter += "<Eq><FieldRef Name='ID' /><Value Type='Counter'>" + delimiterlistItemIds[i] + "</Value></Eq>";
    
        }
        rowLimit = "<RowLimit>" + rowLimit + "</RowLimit>"
    
        var finalQuery = queryMainFirst.concat(queryFilter, queryMainMiddle, rowLimit, queryMainLast);
    
        //Get the Host Web Objects and update Workflow Tasks items. ;
        hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    
        clientContext = SP.ClientContext.get_current();
        var hostContext = new SP.AppContextSite(clientContext, hostUrl);
        var web = hostContext.get_web();
        listWF = web.get_lists().getByTitle("Workflow Tasks");
    
        //Create CAML Object
        var query = new SP.CamlQuery();
        //query.set_viewXml("<View><Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>10</Value></Eq></Or></Where><RowLimit>1</RowLimit></View>");
    
    
        var query = new SP.CamlQuery();
        var formattedQuery = String.format(finalQuery)
        query.set_viewXml(formattedQuery);
    
    
    
        collListItem = listWF.getItems(query);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    
        function onQuerySucceeded(sender, args) {
            message = jQuery("#message");
           
    
            var listItemEnumerator = collListItem.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
    
                message.append(oListItem.get_item("ID")) + message.append(" , ");
    
                message.append(oListItem.get_item("Title")) + message.append(" , ");
                message.append(oListItem.get_item("Status")) + message.append(" , ");
                oListItem.set_item("Status", "Completed");
                oListItem.set_item("PercentComplete", 1);
                oListItem.set_item("WorkflowOutcome", "Approved");
                oListItem.update();
                clientContext.executeQueryAsync(on_myUpdate_Success, on_myUpdate_Failure);
            }
        }
    
        function onQueryFailed(sender, args) {
    
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    
    
    }
    
    
    
    function on_myUpdate_Success() {
        message = jQuery("#message");
        message.text("Updated Successfully.");
        
    }
    function on_myUpdate_Failure(sender, args) {
        var message = jQuery("#message");
        message.text("Error while updating");
        alert("Call failed. Error: " +
                args.get_message());
    }
    


    Sandy

    Monday, December 26, 2016 9:41 AM