interrogazione MAX utilizzando CAML
-
21-08-2019 - |
Domanda
Voglio selezionare una voce di elenco di SharePoint che ha il valore massimo per una particolare colonna. Come posso farlo utilizzando query CAML?
Soluzione
<Query>
<OrderBy>
<FieldRef Name="particularcolumn" Ascending="FALSE" />
</OrderBy>
</Query>
Altri suggerimenti
Il seguente query CAML restituirebbe il valore massimo per una data colonna:
var maxValue;
try
{
using (SPSite objSite = new SPSite(sSiteUrl))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
SPList objList = objWeb.Lists[sListName];
SPQuery objQuery = new SPQuery();
objQuery.Query = "<OrderBy><FieldRef Name='ColumnName' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>";
objQuery.Folder = objList.RootFolder;
// Execute the query against the list
SPListItemCollection colItems = objList.GetItems(objQuery);
if (colItems.Count > 0)
{
maxValue = (<Insert Appropriate Cast>) colItems[0];
}
}
}
}
catch (Exception ex)
{
...
}
return maxValue;
Questo può essere fatto per ordinare da questo campo in modo decrescente e prendendo il primo elemento della collezione restituita.
Utilizzando il modello oggetto client, questo è come sto recuperando l'ID massimo da un elenco.
using (ClientContext clientContext = new ClientContext("https://sharepointed.com"))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
oWeb = clientContext.Web;
List myList= oWeb.Lists.GetByTitle("MyList");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = ("<View><Query> <OrderBy> <FieldRef Name='ID' Ascending='False' /> </OrderBy> </Query> <RowLimit>1</RowLimit> </View>");
Microsoft.SharePoint.Client.ListItemCollection listItems = myList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
<script type="text/javascript" charset="utf8" src="/jquery-3.2.1.min.js"></script>
<script type="text/javascript" charset="utf8" src="/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript">
function getLastItemId() {
var userId = _spPageContextInfo.userId;
var caml = "<View><Query><Where>"
+ "<Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>"
+ userId + "</Value></Eq></Where>"
+ "<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>"
+ "</Query><RowLimit>1</RowLimit></View>";
var ctx = SP.ClientContext.get_current()
var web = ctx.get_web()
//ENTER YOUR LIST NAME BELOW
var list = web.get_lists().getByTitle("YOUR LIST NAME")
var query = new SP.CamlQuery();
query.set_viewXml(caml);
var items = list.getItems(query);
ctx.load(items)
ctx.executeQueryAsync(function() {
// success actions
var count = items.get_count();
//should only be 1
if (count > 1) {
throw "Something is wrong. Should only be one latest list item / doc";
}
var enumerator = items.getEnumerator();
enumerator.moveNext();
var item = enumerator.get_current();
var id = item.get_id();
// do something with your result!!!!
alert(id);
}, function() {
//failure handling comes here
alert("failed");
});
}
getLastItemId();
</script>
<button id="button1" type="button" onclick="getLastItemId();">click to get last ID</button>
Sono riuscito ad ottenere il seguente lavoro ho usato una parte di script web e aggiunto il testo seguente la parte Web. Quando si fa clic sul pulsante un avviso pop-up con il più alto numero di articolo ID.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow