40

I have written the following code to fetch a record from the DocumentDB

private static void QueryDocuments1(DocumentClient client)
{

    IQueryable<SearchInput> queryable =
client.CreateDocumentQuery<SearchInput>(UriFactory.CreateDocumentCollectionUri(DocumentDBName, DocumentDBCollectionName))
        .Where(x => x.Receiver == "8907180");
    List<SearchInput> posts = queryable.ToList();
}

And it is showing the following error on the code line List<SearchInput> posts = queryable.ToList();

{"Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.\r\nActivityId: xxxxxx-xxxx-xxx-xxx-xxxxxxx"}

Please help me on it...

Jan_V
  • 4,244
  • 1
  • 40
  • 64
Prasanth V M
  • 493
  • 1
  • 5
  • 10

1 Answers1

61

You should use CreateDocumentQuery method with FeedOptions object as a parameter, this class has a property for x-ms-documentdb-query-enablecrosspartition called EnableCrossPartitionQuery.

Please follow links https://msdn.microsoft.com/library/en-us/Dn850285.aspx For REST https://learn.microsoft.com/en-us/rest/api/documentdb/querying-documentdb-resources-using-the-rest-api

Example:

you should have

 var option = new FeedOptions { EnableCrossPartitionQuery = true };
 IQueryable<SearchInput> queryable = client.CreateDocumentQuery<SearchInput>
 (UriFactory.CreateDocumentCollectionUri(DocumentDBName, 
 DocumentDBCollectionName), option ) .Where(x => x.Receiver == "8907180");
Ashkan S
  • 10,464
  • 6
  • 51
  • 80
Oleksii
  • 782
  • 7
  • 11
  • 1
    Hi, what about adding new FeedOptions { EnableCrossPartitionQuery = true }? – Prasanth V M Sep 12 '17 at 13:32
  • 3
    Exactly, you should have var option = new FeedOptions { EnableCrossPartitionQuery = true }; IQueryable queryable = client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DocumentDBName, DocumentDBCollectionName), option ) .Where(x => x.Receiver == "8907180"); – Oleksii Sep 12 '17 at 15:01