Hello All,
i done the Count Attachment's in SPList and storing the count value in separate column. The problem is when in list we cross the 100 itmes , the list page takes more time for loading and list page is getting error .
And my actual list is contains 1000 items .
Below is the code i used ,please check and upadate the code for better page performance.Please help me.
<script type="text/javascript" src="/Style%20Library/jquery-1.12.0.min.js"></script><script type="text/javascript">
$(document).ready(function () {
getListItems();
});
function getListItems()
{
$.ajax({
url: "Https://www.example.com" + "/_api/web/lists/getbytitle('ListName')/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
for (var i = 0; i < data.d.results.length; i++) {
//console.log(data.d.results[i]);
var itemscount = getattachments(data.d.results[i].ID);
console.log(itemscount)
function getItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
var item = {
"__metadata": { "type": getItemTypeForListName("Listname") },
"AttachmentCount": itemscount
};
$.ajax({
url: data.d.results[i].__metadata.uri,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
console.log("Success");
},
error: function (data) {
console.log("Fail");
}
});
}
},
error: function (data) {
failure(data);
}
});
}
function getattachments(itemid)
{
var url = "https://www.example.com" + "/_api/web/lists/getbytitle('listname')/items(" + itemid + ")/AttachmentFiles";
var itemscount;
$.ajax({
url: url,
headers: {
"Accept": "application/json; odata=verbose"
},
method: "GET",
async: false,
success: onQuerySuccess,
error: onQueryError
});
function onQuerySuccess(data) {
if (data) {
//console.log(data.d.results.length);
var items = data.d.results;
itemscount = data.d.results.length;
for (var i = 0; i < items.length; i++) {
//console.log(items[i].FileName);
}
}
}
function onQueryError(error) {
attCount =0;
alert(error.statusText);
}
return itemscount;}
</script>
Thanks in Advance.
Regards,
Santto