문제

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>
도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top