none
SPQUERY

    Question

  • I have the following code snippet from my event handler

     string AssignedPersonCheck(SPItemEventProperties properties, string column)
            {
                string assignedUser;
                string currentUser = properties.UserDisplayName;
                string isAssigned = "False";
                SPFieldUserValue userValue;
                SPWeb web = properties.OpenWeb();
                SPList listIACA = web.Lists["Interim Area Completion Approval"];
                string iDNumber = Convert.ToString(properties.ListItem["ID Number"]);
                SPQuery query = new SPQuery();
                query.Query = "<Query><Where><Eq><FieldRef Name=\"ID_x0020_Number\"/><Value Type=\"Number\">" + iDNumber +
                                "</Value></Eq></Where></Query>";
                SPListItemCollection itemIACACollection = listIACA.GetItems(query);
                SPListItem itemIACA;
                itemIACA = properties.ListItem;

    When the handler runs the Id number is correct (the number 5) this is the only item in the list with the value of 5 in the "ID Number" column however the Enitre column is being return in the List Item Collection. This is affecting coding later in the program as I use the .count value to clarify there is no duplicates.
    Thursday, December 03, 2009 10:46 AM

Answers

  • You shouldn't need the <Query> tag in your query.  See if this works (also added the @):

    query.Query = @"<Where><Eq><FieldRef Name='ID_x0020_Number'/><Value Type='Number'>" + iDNumber +
                            "</Value></Eq></Where>";

    • Proposed as answer by Jevgeni Borozna Thursday, December 03, 2009 1:57 PM
    • Edited by Dzangger Thursday, December 03, 2009 1:59 PM Removed " for '
    • Marked as answer by Chengyi Wu Thursday, December 10, 2009 2:13 AM
    Thursday, December 03, 2009 1:56 PM

All replies

  • What are you doing here:
    SPListItem itemIACA;
    itemIACA = properties.ListItem;

    if you want to get Item from Query, then it will be SPListItem itemIACA itemIACACollection[0];
    if you are using Field with type Number without decimal places, then you can use it as Type='Integer'
    Also you can use single quotes instead of double, then you don't need to add back slashes

    And most important place is Convert.ToString(properties.ListItem["ID Number"]);
    are you sure that you will always get value here? Actually you should use here Internal name of field with _x0020_. I don't know why in method description is Display Name of a field
    Thursday, December 03, 2009 10:58 AM
  • "if you want to get Item from Query, then it will be SPListItem itemIACA itemIACACollection[0];"

    But the query is returning all the items in the list not just the one I'm after. Is my query wrong?

    I want to search the list for the ID number. the ID number is unique so only one entry should be return, I do check this later in the code but it is failing at present.
    Thursday, December 03, 2009 11:20 AM
  • Hi,

    You should use (itemIACACollection.Count ==1) to test there is a single item matching your query.

    You don't mention the Enitre column before referring to it, but I assume it is also a column in your list (or did you mean all of the columns / fields of your item are returned?). If you don't need that, use the SPQuery.ViewFields property (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewfields.aspx) to specify exactly the columns you need. But you should not check duplicates by checking the count of the item fields, but by counting the items in the SPListItemColection, as suggested above.

    Peter
    Thursday, December 03, 2009 11:21 AM
  • You shouldn't need the <Query> tag in your query.  See if this works (also added the @):

    query.Query = @"<Where><Eq><FieldRef Name='ID_x0020_Number'/><Value Type='Number'>" + iDNumber +
                            "</Value></Eq></Where>";

    • Proposed as answer by Jevgeni Borozna Thursday, December 03, 2009 1:57 PM
    • Edited by Dzangger Thursday, December 03, 2009 1:59 PM Removed " for '
    • Marked as answer by Chengyi Wu Thursday, December 10, 2009 2:13 AM
    Thursday, December 03, 2009 1:56 PM