I have a timer job by which i programmatically loop through each web site in the site collection , and want to get recycle bin items for each website . Below is my code -

public override void Execute(Guid targetInstanceId) { SPSecurity.RunWithElevatedPrivileges(delegate { SPFarm farm = SPFarm.Local;

            SPWebService service = farm.Services.GetValue<SPWebService>("");

            foreach (SPWebApplication webapp in service.WebApplications)
            {
                foreach (SPSite site in webapp.Sites)
                {
                    foreach (SPWeb web in site.AllWebs)
                    {
                        //Get the recycle Bin Items of the site
                                SPRecycleBinQuery rbQuery = new SPRecycleBinQuery();
                                rbQuery.ItemState = SPRecycleBinItemState.SecondStageRecycleBin;
                                rbQuery.RowLimit = 10000;
                                SPRecycleBinItemCollection actualRBItems = web.GetRecycleBinItems(rbQuery);


                                if (actualRBItems != null)
                                { if (actualRBItems.Count != 0) {//do something} }

}}}});}

But even after having items in the web site Recycle bin, i get count 0 in code every time for SPWeb.RecycleBin.Count , while I am able to get site collection RB SPWeb.Site.RecycleBin with items. Does anybody have any idea, My requirement is to get items separately from each website within the site coll . Any HELP is much appreciated !!

Also , just to add, I tried same code within an event reciever , it works! Is it some thing a limitation of timer job or problem in my code

1

There are 1 best solutions below

1
On

I have tried below code and was able to iterate through list items.

static void GetSiteRecycleBin(string pSiteUrl, int pMaxRecycleBinDelete)
    {
        try
        {
            using (SPSite site = new SPSite(pSiteUrl))
            {
                SPRecycleBinQuery query = new SPRecycleBinQuery();
                query.RowLimit = pMaxRecycleBinDelete;
                SPRecycleBinItemCollection recycleitems = site.GetRecycleBinItems(query);

                foreach (SPRecycleBinItem item in recycleitems)
                {
                }
            }
        }
        catch (Exception ex)
        {               
        }
    }