How to retrieve the maximum date and minimum date from Sharepoint server 2010 using client object model?
-
15-03-2021 - |
Вопрос
I am developing silverlight web part. I have a one List Entry log. In this list I am storing all the entries of basic information. All the entries are saved successfully. Now I want to retrieve the maximum date and minimum date on which these entries are created in the Entry log list. So I am using the following code
public void PopulateTimeLog(String paymentStatus, String currentUser, timeLogDelegate populateCombo)
{
timeLogDelegateClient = populateCombo;
ClientContext clientContext = ClientContext.Current;
List list = clientContext.Web.Lists.GetByTitle("Time Log");
string query = "<View>";
query += "<Query>";
query += "<Where>";
query += "<And>";
query += "<Eq>";
query += "<FieldRef Name='Author'></FieldRef>";
query += "<Value Type='Lookup'>" + currentUser + "</Value>";
query += "</Eq>";
query += "<Eq>";
query += "<FieldRef Name='Payment_x0020_Status'></FieldRef>";
query += "<Value Type='Text'>" + paymentStatus + "</Value>";
query += "</Eq>";
query += "</And>";
query += "</Where>";
query += "<OrderBy>";
query += "<FieldRef Name='Created' Ascending='False'></FieldRef>";
query += "</OrderBy>";
query += "</Query>";
query += "</View>";
CamlQuery Query = new CamlQuery();
Query.ViewXml = query;
listItemsTimeLog = list.GetItems(Query);
//clientContext.Load(list);
//clientContext.Load(listItemsTimeLog);
clientContext.Load(listItemsTimeLog, itms => itms.Include(
itm => itm["Created"]));
clientContext.ExecuteQueryAsync(HandleTimeLogRequestSucceeded, HandleTimeLogRequestFailed);
}
private void HandleTimeLogRequestSucceeded(object sender, ClientRequestSucceededEventArgs e)
{
////call back on the UI thread
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
timeLogDelegateClient(listItemsTimeLog);
});
}
In the above code I will get the the maximum date in listItemsTimeLog at index 0 and minimum date in listItemsTimeLog at last index. In the above query i dont want to retrieve all the dates in the listItemsTimeLog. How Can I simplify the above query so that I will get only two string values - One string will contain maximum date and other string will contain mimimum date . If I can do it through web service then it is also fine for me ? How can I do this ? Can you please provide me any code so that I can solve the above issue ?
Решение
You can use RowLimit for this, e.g.
Max
<View>
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='Author'></FieldRef>
<Value Type='Lookup'>[your use name]</Value>
</Eq>
<Eq>
<FieldRef Name='Payment_x0020_Status'></FieldRef>
<Value Type='Text'>[ your payment status]</Value>
</Eq>
</And>
</Where>
<OrderBy>
<FieldRef Name='Created' Ascending='False'></FieldRef>
</OrderBy>
</Query>
<RowLimit>1</RowLimit>
</View>
Min
<View>
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='Author'></FieldRef>
<Value Type='Lookup'>[your use name]</Value>
</Eq>
<Eq>
<FieldRef Name='Payment_x0020_Status'></FieldRef>
<Value Type='Text'>[ your payment status]</Value>
</Eq>
</And>
</Where>
<OrderBy>
<FieldRef Name='Created' Ascending='True'></FieldRef>
</OrderBy>
</Query>
<RowLimit>1</RowLimit>
</View>