简体   繁体   中英

Query not returning expected results(CAML)

I am trying to remove an attachment from a list of attachments using the CAML, however; when I do the query it always returns both of my files. I only need to get the file of the current one using the passed in value as the parameter.

SP.List list = context.Web.Lists.GetByTitle("TempAttachments");

 // Query
            SP.CamlQuery query = new SP.CamlQuery();
            query.ViewXml =
                 "<Query><Where><Or>"
                + "<BeginsWith>"
                // Job Note Matches
                + "<FieldRef Name=\"FileRef\"/>"
                + "<Value Type=\"Text\"/>" + ID + "_</Value>"
                + "</BeginsWith>"
                // OR Date Modified is older than one day.
                + "<Lt>"
                + "<FieldRef Name=\"Modified\"/>"
                + "<Value Type=\"DateTime\"/><Today OffsetDays=\"-1\" /></Value>"
                + "</Lt>"
                + "</Or>"
                + "</Where></Query>";

Could the Beginswith tag be the problem?

  1. Your CAML query needs to be wrapped in a View element when seeting the ViewXml .

  2. Your Value tags are both malformed; you're closing the element in the opening tag, meaning you have malformed XML.

  3. Your query has an underscore at the end of the value you specify for the file name, but in the examples you mentioned in comments , your actual files don't have one, so that shouldn't be there.

So your query can now become:

CamlQuery query = new CamlQuery();
query.ViewXml = string.Format(
@"<View>
  <Query>
    <Where>
      <Or>
        <BeginsWith>
          <FieldRef Name='FileRef'/>
          <Value Type='Text'>{0}</Value>
        </BeginsWith>
        <Lt>
          <FieldRef Name='Modified'/>
          <Value Type='DateTime'>
            <Today OffsetDays='-1'/>
          </Value>
        </Lt>
      </Or>
    </Where>
  </Query>
</View>
", ID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM