SharePoint : Check if item exists in a list, minimum overhead

后端 未结 1 1683
猫巷女王i
猫巷女王i 2021-02-10 02:28

How can I check if a list contains an item... really only interested in checking 1 field, not every single field in the list.

How can this be done in the most efficient

1条回答
  •  名媛妹妹
    2021-02-10 02:38

    Here's a good comparison of techniques from Waldek Mastykarz.

    The general rule is to use SPQuery. See SharePointDevWiki for more details. Here's a basic example:

    SPList list = SPContext.Current.Web.Lists["Some List"];
    SPQuery query = new SPQuery();
    query.Query = @"
        
            
                
                Value To Match
            
        ";
    SPListItemCollection found = list.GetItems(query);
    if (found.Count > 0)
    {
        // Do something
    }
    

    A few notes about SPQuery:

    • If you get your query wrong, it can return all answers instead of giving an error
    • If you get your query wrong, it can sometimes give an unhelpful/misleading error
    • Make sure you get the Value Type correct

    Save yourself a lot of trouble by using a tool such as U2U CAML Builder (Windows or Web versions - Web is better IMHO) or Stramit CAML Viewer to build and test your queries.

    0 讨论(0)
提交回复
热议问题