0

I have below code, to fetch the count of sharepoint list items to auto generate next item number by incrementing it. But somehow it's only fetching count of list items per user (list is protected to show only resp user's items). i would it instead have get list of all items (irrespective of user). Any help would be appreciated.

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"type = "text/javascript" ></script>
<script type = "text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(AutoGenerate, "sp.js");

function AutoGenerate() {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    var list = web.get_lists().getById(_spPageContextInfo.pageListId);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query></Query></View>');
    this.listItems = list.getItems(camlQuery);
    clientContext.load(listItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
        Function.createDelegate(this, this.onQueryFailed));
}

function onListItemsLoadSuccess(sender, args) {

    var AutoIncremental = "AA-XCSA-" + listItems.get_count();
    $("input[title^='Request ID']").val(AutoIncremental);
    $("input[title^='Request ID']").attr('disabled', 'disabled');
}

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

2 Answers 2

0

I think we can use ItemCount property of the List to get the total item count. I've tested with a list for which item level permission was enabled and found out that regardless what items the user have permission to, the total count is returning the same.

<div>
<input type='button' value='Get item count' onclick="javascript:GetListItemCount();"/>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ItemLevelPermission')";

var myData;

function GetListItemCount(){    


        $.ajax({
            url: url,  
            method: "GET",  
            headers: {  
                "Accept": "application/json; odata=verbose"  
            },

            success: function(data){
               
               //myData=data;

               alert("Total item count in the list: " + data.d.ItemCount);
                
            },

            error: function(error){
                   
           alert('Request failed. ' + args.get_message() + 
                '\n' + args.get_stackTrace());
                  
            }
        });
    }

</script>

enter image description here

enter image description here

0
0

rahman

I tried your code but somehow when clicked on button, wasn't giving anything. I tried to amalgamate both the codes but aint working either. can you please check, i am trying to assign the count value to variable and use it for next ID generation.

<div>
<input type='button' value='Get item count' onclick="javascript:GetListItemCount();"/>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">

var AutoIncremental = null;
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Data Setup')";

var myData;

ExecuteOrDelayUntilScriptLoaded(GetListItemCount, "sp.js");

function GetListItemCount(){    


        $.ajax({
            url: url,  
            method: "GET",  
            headers: {  
                "Accept": "application/json; odata=verbose"  
            },

            success: function(data){
               
               //myData=data;

               alert("Total item count in the list: " + data.d.ItemCount);
               var AutoIncremental = "SDSR-" + data.d.ItemCount;
               $("input[title^='Request ID']").val(AutoIncremental);
               $("input[title^='Request ID']").attr('disabled', 'disabled');
                
            },

            error: function(error){
                   
           alert('Request failed. ' + args.get_message() + 
                '\n' + args.get_stackTrace());
                  
            }
        });
    }

</script>

enter image description here

9
  • Can you try using a "button" like in my example to see if you can make it work? ItemCount is a List property, so it should return a value as long as the user has access to it. Dec 8, 2020 at 13:23
  • Another question, what kind of permission does the user have at the List level? For my example, user has "Contribute" permission at the list level, and Item-Level-Permission was set according to this Article Dec 8, 2020 at 16:46
  • @matiur-rahman hi, i was finally able to make it work, there was issue with list title. Bu t i am facing another problem, the counter is stuck at 2, its not updating when added new items and still showing old value
    – Rameet
    Dec 9, 2020 at 19:12
  • @MatiurRahman, I have updated my post with the working code & screenshot, where you could see that there are 3 items but counter is still showing 2. Please guide if i am missing something. I tried with my access (Full Control) and my users would have Contribute access to the site/list
    – Rameet
    Dec 9, 2020 at 19:26
  • Can you add one more item and see if it still returns the same count 2. My guess is that List.ItemCount is being cached and that's why it's not returning the correct value in your case. Another different between your code and mine is that I have this running on the View Page, not on NewForm.aspx or EditForm.aspx Dec 9, 2020 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.