Query not returning expected results(CAML)

1.7k views Asked by At

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

There are 1 answers

6
Servy On BEST ANSWER
  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);