Requirement - In SharePoint Online, First Stage Recycle Bin consumes your site quota space. The following code gets how much data is there under the First Stage Recycle Bin.
Note - Always pass Site URL. Web URL will not work and always return 0.
Function getRecycleBinSize takes URL is parameter and stores all results in a data table.
public void getRecycleBinSize(string URL)
{
DataTable dtRecycleBin = new DataTable();
using (ClientContext clientContext = new ClientContext(URL))
{
SecureString passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);
Site s = clientContext.Site;
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.Load(s);
clientContext.Load(s.RecycleBin);
clientContext.ExecuteQuery();
if(dtRecycleBin.Columns.Count==0)
{
dtRecycleBin.Columns.Add(constantVariables.Title);
dtRecycleBin.Columns.Add(constantVariables.deletedBy);
dtRecycleBin.Columns.Add(constantVariables.deletionDate);
dtRecycleBin.Columns.Add(constantVariables.location);
dtRecycleBin.Columns.Add(constantVariables.itemState);
dtRecycleBin.Columns.Add(constantVariables.itemType);
dtRecycleBin.Columns.Add(constantVariables.leafName);
dtRecycleBin.Columns.Add(constantVariables.size);
}
RecycleBinItemCollection recyclebinItemCollection = s.RecycleBin;
clientContext.Load(recyclebinItemCollection);
clientContext.ExecuteQuery();
int count = recyclebinItemCollection.Count;
int i = 0;
foreach (RecycleBinItem item in recyclebinItemCollection)
{
if (item.ItemState.ToString().ToLower() == "FirstStageRecycleBin".ToLower())
{
DataRow newRow = dtRecycleBin.NewRow();
dtRecycleBin.Rows.Add(newRow);
newRow[0] = item.Title.ToString() ;
newRow[1] = item.DeletedBy.ToString();
newRow[2] = item.DeletedDate ;
newRow[3] = item.DirName ;
newRow[4] = item.ItemState ;
newRow[5] = item.ItemType ;
newRow[6] = item.LeafName;
newRow[7] = item.Size ;
}
i++;
}
}
dataGridView2.DataSource = null;
dataGridView2.DataSource = dtRecycleBin;
}
Note - Always pass Site URL. Web URL will not work and always return 0.
Function getRecycleBinSize takes URL is parameter and stores all results in a data table.
public void getRecycleBinSize(string URL)
{
DataTable dtRecycleBin = new DataTable();
using (ClientContext clientContext = new ClientContext(URL))
{
SecureString passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);
Site s = clientContext.Site;
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.Load(s);
clientContext.Load(s.RecycleBin);
clientContext.ExecuteQuery();
if(dtRecycleBin.Columns.Count==0)
{
dtRecycleBin.Columns.Add(constantVariables.Title);
dtRecycleBin.Columns.Add(constantVariables.deletedBy);
dtRecycleBin.Columns.Add(constantVariables.deletionDate);
dtRecycleBin.Columns.Add(constantVariables.location);
dtRecycleBin.Columns.Add(constantVariables.itemState);
dtRecycleBin.Columns.Add(constantVariables.itemType);
dtRecycleBin.Columns.Add(constantVariables.leafName);
dtRecycleBin.Columns.Add(constantVariables.size);
}
RecycleBinItemCollection recyclebinItemCollection = s.RecycleBin;
clientContext.Load(recyclebinItemCollection);
clientContext.ExecuteQuery();
int count = recyclebinItemCollection.Count;
int i = 0;
foreach (RecycleBinItem item in recyclebinItemCollection)
{
if (item.ItemState.ToString().ToLower() == "FirstStageRecycleBin".ToLower())
{
DataRow newRow = dtRecycleBin.NewRow();
dtRecycleBin.Rows.Add(newRow);
newRow[0] = item.Title.ToString() ;
newRow[1] = item.DeletedBy.ToString();
newRow[2] = item.DeletedDate ;
newRow[3] = item.DirName ;
newRow[4] = item.ItemState ;
newRow[5] = item.ItemType ;
newRow[6] = item.LeafName;
newRow[7] = item.Size ;
}
i++;
}
}
dataGridView2.DataSource = null;
dataGridView2.DataSource = dtRecycleBin;
}