Dear all,
Below post describes how to get Site Storage report using JavaScript in SharePoint 2013.
In SharePoint 2010 we use to get the Usage report by
http://sitename/_layouts/15/Usage.aspx
We can still retrieve the data we require in the same way in SharePoint 2013 although the web analytic options from Site administration is missing.
REST call to get Usage Report in 2013:
https://<siteUrl>/_api/site/usage
Below was my purpose to create the Usage report Program:
1. To get usage report for 85 sites
2. To understand the Used storage and Max store for each site
Hence instead of hitting all sites manually I had to write a program.
Process:
1. Create a List called "All Site Usage Tracker"
Columns: Title -> (Single line of text) -> Site name
URL -> (multi line of text)-> Site Url
Columns: This are populated after my program runs
Current Storage (GB) -> (number)
Currently Used Storage (MB) -> (Single line of text)
Storage Percentage Used -> (Single line of text)
Max Storage (MB) -> (Single line of text)
Columns: Calculated column. This was for my own purpose to segregate the sites into <10 GB, <20 GB and so on.
Usage -> (calculated column) -> Based on Current Storage (GB)
2. Program
As I had to call all the sites from the "All Site Usage Tracker" I had to use Sp services thrice:
1. To retrieve the names of sites
2. To get the usage report for each site
3. To update the list with storage data
I have used an array to store the data for all my sites.
/* Start of Program*/
//Empty Array
var SitesData = new Array();
// Get all sites from List and store in array
function GetSites() {
$().SPServices({
debug: true,
operation: "GetListItems",
async: false,
listName: "All Site Usage Tracker",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function () {
// UsageReport($(this).attr("ows_URL"));
SitesData.push({
Id: $(this).attr("ows_ID"),
URL: $(this).attr("ows_URL"),
Storage: null,
PercentStorage: null,
MaxStorage: null,
UsedStorageinMB: null,
});
});
}
});
UsageServices();
UpdateItems();
SitesData.length = 0;
alert("End of Usage Update");
}
// Get the Storage report of all sites and update the array
function UsageServices() {
for (var i = 0; i < SitesData.length; i++) {
var SiteURL = SitesData[i].URL;
var call = $.ajax({
url: SiteURL + "/_api/site/usage",
type: "GET",
dataType: "json",
async: false,
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
try {
SitesData[i].Storage = Math.round(data.d.Usage.Storage / 1024 / 1024 * 10 / 1024) / 10; //Used Storage in GB
SitesData[i].PercentStorage = Math.round(data.d.Usage.StoragePercentageUsed * 1000) / 10 + ' %'; //Percentage of storage used
SitesData[i].MaxStorage = Math.round(data.d.Usage.Storage / data.d.Usage.StoragePercentageUsed / 1024 / 1024 * 10) / 10; //Maximum storage for site in MB
SitesData[i].UsedStorageinMB = Math.round(data.d.Usage.Storage / 1024 / 1024 * 10) / 10; //Used Storage in MB
console.log("Sites Data " + [i] + "SiteUrl: " + SitesData[i].URL);
console.log("Sites Data " + [i] + "Storage: " + SitesData[i].Storage);
console.log("Sites Data " + [i] + "MaxStorage: " + SitesData[i].MaxStorage);
}
catch (e) {
//alert(e);
}
});
call.fail(function (jqXHR, textStatus, errorThrown) {
console.log("Error generating Report: " + jqXHR.responseText);
});
}
}
//Update List with Storage data
function UpdateItems() {
for (var i = 0; i < SitesData.length; i++) {
$().SPServices({
operation: "UpdateListItems",
async: false,
batchCmd: "Update",
listName: "All Site Usage Tracker",
ID: SitesData[i].Id,
valuepairs: [["Current_x0020_Storage", SitesData[i].Storage], ["Storage_x0020_Percentage_x0020_U", SitesData[i].PercentStorage], ["Max_x0020_Storage", SitesData[i].MaxStorage], ["Currently_x0020_Used_x0020_Stora", SitesData[i].UsedStorageinMB]],
completefunc: function (xData, Status) {
if (Status != "success") {
console.log("Error in Connection: " + xData.responseXML);
}
}
});
}
}
/* End of Program*/ 3. I had appended a button and html code in the allitems.aspx page of above list.
Below is the code:
<script type="text/javascript" src="path for jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="path for jquery.SPServices-2013.01.js"></script>
<script type="text/javascript" src="path for Usage.js"></script> //Your program file
<input id='btnSend' type='button' class='btnClass' value='Click to Update above List' onclick='GetSites();'/>
Below post describes how to get Site Storage report using JavaScript in SharePoint 2013.
In SharePoint 2010 we use to get the Usage report by
http://sitename/_layouts/15/Usage.aspx
We can still retrieve the data we require in the same way in SharePoint 2013 although the web analytic options from Site administration is missing.
REST call to get Usage Report in 2013:
https://<siteUrl>/_api/site/usage
Below was my purpose to create the Usage report Program:
1. To get usage report for 85 sites
2. To understand the Used storage and Max store for each site
Hence instead of hitting all sites manually I had to write a program.
Process:
1. Create a List called "All Site Usage Tracker"
Columns: Title -> (Single line of text) -> Site name
URL -> (multi line of text)-> Site Url
Columns: This are populated after my program runs
Current Storage (GB) -> (number)
Currently Used Storage (MB) -> (Single line of text)
Storage Percentage Used -> (Single line of text)
Max Storage (MB) -> (Single line of text)
Columns: Calculated column. This was for my own purpose to segregate the sites into <10 GB, <20 GB and so on.
Usage -> (calculated column) -> Based on Current Storage (GB)
2. Program
As I had to call all the sites from the "All Site Usage Tracker" I had to use Sp services thrice:
1. To retrieve the names of sites
2. To get the usage report for each site
3. To update the list with storage data
I have used an array to store the data for all my sites.
/* Start of Program*/
//Empty Array
var SitesData = new Array();
// Get all sites from List and store in array
function GetSites() {
$().SPServices({
debug: true,
operation: "GetListItems",
async: false,
listName: "All Site Usage Tracker",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function () {
// UsageReport($(this).attr("ows_URL"));
SitesData.push({
Id: $(this).attr("ows_ID"),
URL: $(this).attr("ows_URL"),
Storage: null,
PercentStorage: null,
MaxStorage: null,
UsedStorageinMB: null,
});
});
}
});
UsageServices();
UpdateItems();
SitesData.length = 0;
alert("End of Usage Update");
}
// Get the Storage report of all sites and update the array
function UsageServices() {
for (var i = 0; i < SitesData.length; i++) {
var SiteURL = SitesData[i].URL;
var call = $.ajax({
url: SiteURL + "/_api/site/usage",
type: "GET",
dataType: "json",
async: false,
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data, textStatus, jqXHR) {
try {
SitesData[i].Storage = Math.round(data.d.Usage.Storage / 1024 / 1024 * 10 / 1024) / 10; //Used Storage in GB
SitesData[i].PercentStorage = Math.round(data.d.Usage.StoragePercentageUsed * 1000) / 10 + ' %'; //Percentage of storage used
SitesData[i].MaxStorage = Math.round(data.d.Usage.Storage / data.d.Usage.StoragePercentageUsed / 1024 / 1024 * 10) / 10; //Maximum storage for site in MB
SitesData[i].UsedStorageinMB = Math.round(data.d.Usage.Storage / 1024 / 1024 * 10) / 10; //Used Storage in MB
console.log("Sites Data " + [i] + "SiteUrl: " + SitesData[i].URL);
console.log("Sites Data " + [i] + "Storage: " + SitesData[i].Storage);
console.log("Sites Data " + [i] + "MaxStorage: " + SitesData[i].MaxStorage);
}
catch (e) {
//alert(e);
}
});
call.fail(function (jqXHR, textStatus, errorThrown) {
console.log("Error generating Report: " + jqXHR.responseText);
});
}
}
//Update List with Storage data
function UpdateItems() {
for (var i = 0; i < SitesData.length; i++) {
$().SPServices({
operation: "UpdateListItems",
async: false,
batchCmd: "Update",
listName: "All Site Usage Tracker",
ID: SitesData[i].Id,
valuepairs: [["Current_x0020_Storage", SitesData[i].Storage], ["Storage_x0020_Percentage_x0020_U", SitesData[i].PercentStorage], ["Max_x0020_Storage", SitesData[i].MaxStorage], ["Currently_x0020_Used_x0020_Stora", SitesData[i].UsedStorageinMB]],
completefunc: function (xData, Status) {
if (Status != "success") {
console.log("Error in Connection: " + xData.responseXML);
}
}
});
}
}
/* End of Program*/ 3. I had appended a button and html code in the allitems.aspx page of above list.
Below is the code:
<script type="text/javascript" src="path for jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="path for jquery.SPServices-2013.01.js"></script>
<script type="text/javascript" src="path for Usage.js"></script> //Your program file
<input id='btnSend' type='button' class='btnClass' value='Click to Update above List' onclick='GetSites();'/>
No comments:
Post a Comment