简体   繁体   中英

How can I count the number of new items in SharePoint library/list?

In SharePoint Online there is a green symbol for new documents in a library. I found that the class is called 'ms-newdocument-icon'. How can I check how many items in my document library has the ms-newdocument-icons class?

<script type="text/javascript">
var clientContext = null;
var web = null;
var listItems = null;
var list = null;

ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    list = web.get_lists().getByTitle("Document");

    var camlQuery = new SP.CamlQuery();
    var q = "<View></View>";
    camlQuery.set_viewXml(q);
    listItems = list.getItems(camlQuery);
    clientContext.load(listItems);

    clientContext.executeQueryAsync(onListItemsLoadSuccess, onQueryFailed);
}

    function onListItemsLoadSuccess(sender, args) {
        var listItemEnumerator = listItems.getEnumerator();
        while(listItemEnumerator.moveNext()) {
            var newItemsCount = null;
            if() { //$(this).hasClass('ms-newdocument-icon');
            newItemsCount++;
        }
        $('#newItems').html(newItemsCount);
   }

   function onQueryFailed(sender, args) {
     alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

<div class="newItemsContent">
    <h2>New items:</h2> <h2 id="newItems"></h2>
</div>

The class you mentioned is only used to indicate new items in HTML rendered in the page. The icon is not stored within the list items data (fields). You can setup how many days the icon will be displayed for new items using this PS script:

$wa = Get-SPWebAppication -Identity:http://webappurl
$wa.DaysToShowNewIndicator = {SomeInteger}
$wa.Update()

Therefore if you want to know how many documents/items have this icon displayed you need to read this parameter and then use it in your CAML query. Reading all items and then checking if they were created during last N days is not good idea. Much better approach is to construct CAML query using current date and the DaysToShowNewIndicator value.

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