In this article we will see how to Get site storage information from multiple SharePoint online or Onprem. Suppose we have multiple sites and we are not have tenant or farm administrator rights, and we need to get the report for storage space occupied by the sites, so we can use the below the CSOM code to get those information
1) Place all the site url In excel sheet and read site url from excel sheet as shown in below code
public static List<TargetSite> ReadTargetSiteURL(string filepath)
{
DataTable dtTargetSiteUrl = ConvertCSVtoDataTable(filepath);
List<TargetSite> lstTargetSiteURl = new List<TargetSite>();
foreach (DataRow dr in dtTargetSiteUrl.Rows)
{
TargetSite targetSite = new TargetSite() { TargetSiteUrl = Convert.ToString(dr["TargetSiteUrl"]) };
lstTargetSiteURl.Add(targetSite);
}
2) The below method is used to print the output of space occupied by each site
public static void getsitesstorageInformation()
{
string outputFilePath = Path.Combine(Environment.CurrentDirectory, "Storagedetails", Path.GetFileName("Storagedetails.txt")
List<TargetSite> lstTargetsite = ReadTargetSiteURL(@"D:hariLNSitedetails.csv");
string userName = "***********";
string password = @"*******";
foreach (var item in lstTargetsite)
{
try
{
if (item.TargetSiteUrl != string.Empty)
{
// if online use below code
ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(item.TargetSiteUrl, userName, password);
// else
ctx = authMgr.GetNetworkCredentialAuthenticatedContext(siteUrl, userName, password, "");
ctx.Load(ctx.Web);
string usageData = string.Empty;
ctx.Load(ctx.Site, s => s.Usage);
ctx.ExecuteQuery();
UsageInfo usageInfo = ctx.Site.Usage;
var usageData2 = (usageInfo.Storage);
double SizeinGB = ConvertToGigabytes(usageData2);
System.IO.File.AppendAllText(outputFilePath, "t" + SizeinGB + "t" + item.TargetSiteUrl + Environment.NewLine);
}
}
}
}
static double ConvertToGigabytes(long bytes)
{
return (bytes / 1024f / 1024f / 1024f);
}
Leave a comment