locked
SPquery - String variable in a CAML query. IS tht possible? RRS feed

  • Question

  • Hello Frnds,

    I have been trying to pull the items from a custom list based on SPquery,,it works fine as long as I know what the specific value in that Column is.. like

    brief snippet--

    SPList tasksList = workflowProperties.Web.Lists[tListID];
    SPQuery tQuery = new SPQuery();
    tQuery.Query = "<Where><eq><FieldRef Name='Plant'/>" + "<Value Type='Text'>plant1</Value></eq></Where>";
    SPListItemCollection tCollection = tasksList.GetItems(tQuery);

    ----------
    In the above query I know what field Value i need to use (plant1) and so it works well for me,,

    But know i have a scenario where i need to fetch the value from the user and push that into the query.

    like

    String plantName = plant1; (in my case reading this value from a Form)

    Can I now pass this string variable(plantName) to the CAML query,,,Is there a way?

    I'm not much into this CAML fundas ,,any help on this would be very helpful

    or let me know if I am on the wrong edge :)

    Thnks in advance :)
    Tuesday, January 20, 2009 4:33 PM

Answers

  • Hi,

    You can include a variable where plant1 is.  For example:

    SPList tasksList = workflowProperties.Web.Lists[tListID];
    SPQuery tQuery = new SPQuery();
    tQuery.Query = "<Where><eq><FieldRef Name='Plant'/>" + "<Value Type='Text'>" + plant + "</Value></eq></Where>";
    SPListItemCollection tCollection = tasksList.GetItems(tQuery);

    Hope this helps

    Dave
    My SharePoint Blog - www.davehunter.co.uk/blog
    Tuesday, January 20, 2009 4:40 PM
  • you can simply concat your string into Query property like following

    tQuery.Query = "<Where><eq><FieldRef Name='Plant'/>" + "<Value Type='Text'>" + plantName + "</Value></eq></Where>";

    as planet name was your string variable. so you can simply concat like above.
    Tuesday, January 20, 2009 4:47 PM

All replies

  • Hi,

    You can include a variable where plant1 is.  For example:

    SPList tasksList = workflowProperties.Web.Lists[tListID];
    SPQuery tQuery = new SPQuery();
    tQuery.Query = "<Where><eq><FieldRef Name='Plant'/>" + "<Value Type='Text'>" + plant + "</Value></eq></Where>";
    SPListItemCollection tCollection = tasksList.GetItems(tQuery);

    Hope this helps

    Dave
    My SharePoint Blog - www.davehunter.co.uk/blog
    Tuesday, January 20, 2009 4:40 PM
  • you can simply concat your string into Query property like following

    tQuery.Query = "<Where><eq><FieldRef Name='Plant'/>" + "<Value Type='Text'>" + plantName + "</Value></eq></Where>";

    as planet name was your string variable. so you can simply concat like above.
    Tuesday, January 20, 2009 4:47 PM
  • hmmmm Thnks guys,,

    well at times lol :D ,, for simple things like this i have been scratching on how to call a string varaible in CAML, silly me.
    Tuesday, January 20, 2009 6:13 PM
  • Another thing you could do would be including a {0} placeholder in your query and then using String.Format to replace it with the real value like:

    string query = "<Where><eq><FieldRef Name='Plant'/><Value Type='Text'>{0}</Value></eq></Where>";

    SPList tasksList = workflowProperties.Web.Lists[tListID];
    SPQuery tQuery = new SPQuery();
    tQuery.Query = String.Format(query, plant);
    SPListItemCollection tCollection = tasksList.GetItems(tQuery);


    Such approach would allow you to store the query externally (list, config file, etc.) or as a static class property.

    -- http://blog.mastykarz.nl
    Tuesday, January 27, 2009 1:40 PM
  • Waldek Mastykarz said:

    Another thing you could do would be including a {0} placeholder in your query and then using String.Format to replace it with the real value like:


    string query = "<Where><eq><FieldRef Name='Plant'/><Value Type='Text'>{0}</Value></eq></Where>";

    SPList tasksList = workflowProperties.Web.Lists[tListID];
    SPQuery tQuery = new SPQuery();
    tQuery.Query = String.Format(query, plant);
    SPListItemCollection tCollection = tasksList.GetItems(tQuery);


    Such approach would allow you to store the query externally (list, config file, etc.) or as a static class property.

    -- http://blog.mastykarz.nl



    Not only allows it to store the query externally, it is also a LOT faster than the boxing/unboxing of strings that happens when you concat like: "test" + var1 + "text" + var2 + etc etc
    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    Tuesday, January 27, 2009 1:43 PM
  • Great :) - Thnk you guys,,
    Tuesday, January 27, 2009 7:49 PM