使用JSOM或CSOM获取sharepoint列表中最后一项的ID [英] Get ID of last item in sharepoint list using JSOM or CSOM

查看:60
本文介绍了使用JSOM或CSOM获取sharepoint列表中最后一项的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

问题是我需要在列表中创建的最后一个ID值,无论该项目是否已删除

the problem is i need the value last ID which was created in the list, whether the item is deleted or not

无论哪个是我需要的最后一个id,如果我正在使用ID创建一个序列号

whichever is the last id i need value if that as i am creating a sequential number using the ID

下面是我的代码

<html>
<head>

<script type="text/javascript"> 
 
    ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js"); 
   
    // Get the current Site
    var  webAbsoluteUrl;
    function retrieveListItems() {
        webAbsoluteUrl=  _spPageContextInfo.webAbsoluteUrl;
      
        if (_spPageContextInfo.serverRequestPath.endsWith("NewForm.aspx"))
{       
        var clientContext = new SP.ClientContext(webAbsoluteUrl);
        // Get the liste instance
        var oList = clientContext.get_web().get_lists().getByTitle('ABC');
         
        var camlQuery = new SP.CamlQuery();

        // Get only the last element
        camlQuery.set_viewXml('<Query><OrderBy><FieldRef Name=\'Title\'  Ascending=\'False\' /></OrderBy></Query><RowLimit>1</RowLimit>');
        collListItem = oList.getItems(camlQuery);

        clientContext.load(collListItem);

        clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);        
}
    
}
    function onQuerySucceeded(sender, args) {
        $("input[id*='fa564e0f-0c70-4ab9-b863-0177e6ddd247']").attr("readonly", "true").css('background-color', '#fff', 'color', '#444');
        $("input[id*='fa564e0f-0c70-4ab9-b863-0177e6ddd247']").attr("disabled", "disabled");
        var isdata=false;
        var listItemInfo='';
        var listItemEnumerator = collListItem.getEnumerator();
        while (listItemEnumerator.moveNext()) {
			isdata=true;
            var oListItem = listItemEnumerator.get_current();
			 listItemInfo = oListItem.get_item("ID");
			 
            // Split the first
            var res = listItemInfo;

			var newID1 = res+1;
			var newID = "ABC-00" + newID1
            // create the new id
			if(isdata==true){
			    $("input[id*='fa564e0f-0c70-4ab9-b863-0177e6ddd247']").val(newID);
			}
		
        }
		if(isdata==false){
			
		    $("input[id*='fa564e0f-0c70-4ab9-b863-0177e6ddd247']").val("ABC-00" + 1);
	  
	   }
      listItemInfo.toString();
   
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
    }   
	

_spBodyOnLoadFunctionNames.push("SetReadOnly()");


</script>
</head>
</html>
	

    



Paru

推荐答案

Hi Paru,



请尝试以下代码,此处列表名称为Documents。根据您的列表名称进行更改。
Hi Paru,

Try the below code, here the list name is Documents. Change it based on your list name.
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()
    var list = web.get_lists().getByTitle("Documents")
    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();





这篇关于使用JSOM或CSOM获取sharepoint列表中最后一项的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆