How to get all items in SharePoint recycle bin by using sharepy library in Python?

Huynh Truong Minh Quang 0 Reputation points
2024-04-03T02:41:17.13+00:00

I'm trying to retrieve all items from the recycle bin on SharePoint. Below is my code. Currently, I notice that I can only retrieve items deleted by the user used to make authentication. This user is currently the site owner. I want to retrieve all items inside the recycle bin, including those deleted by other users. How can I achieve this? Any help from the community would be greatly appreciated. Thank you very much.

def _auth_sharepy(self, USERNAME, PASSWORD,SITE_URL):
    result = None
    try:
        conn_s = sharepy.connect(SITE_URL, USERNAME, PASSWORD)
        result = conn_s
    except Exception as ex:
        print("An error occurred: %s", ex)
    return result

def _get_item_information_from_recycle_bin(self, s, SHAREPOINT_SITE):
    selected_results=None
    recycle_bin_endpoint = f"{SHAREPOINT_SITE}/_api/web/RecycleBin"
    if s is not None:
        try:
            response = s.get(recycle_bin_endpoint)
            if response.status_code == 200:
                results=response.json()['d']['results']
                selected_results = [{'LeafName': item['LeafName'], 
                                    'DirName': item['DirName'], 
                                    'ItemType': item['ItemType'], 
                                    'DeletedDate': item['DeletedDate'],
                                    'DeletedByEmail': item['DeletedByEmail'],
                                    "Id": item['Id']} 
                                    for item in results
                                    ]
                print('Successfully get items from recycle bin')
            else:
                print("Error code: %s", str(response.status_code))
        except Exception as ex:
            print('Error occoured: %s', ex)
    else:
        print('Connection is None!')
    return selected_results
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,978 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shawn Collins 615 Reputation points
    2024-04-13T02:34:24.6033333+00:00

    To retrieve all items from the recycle bin on SharePoint, including those deleted by other users, you need to adjust the permissions or use an account with higher privileges. By default, a site owner's permissions might restrict them to only see items they have deleted, depending on the site's configuration.

    import sharepy

    def _auth_sharepy(self, USERNAME, PASSWORD, SITE_URL):

    result = None
    
    try:
    
        conn_s = sharepy.connect(SITE_URL, USERNAME, PASSWORD)
    
        result = conn_s
    
    except Exception as ex:
    
        print(f"An error occurred: {ex}")
    
    return result
    

    def _get_item_information_from_recycle_bin(self, s, SHAREPOINT_SITE):

    selected_results = None
    
    recycle_bin_endpoint = f"{SHAREPOINT_SITE}/_api/web/RecycleBin"
    
    if s is not None:
    
        try:
    
            response = s.get(recycle_bin_endpoint)
    
            if response.status_code == 200:
    
                results = response.json()['d']['results']
    
                selected_results = [
    
                    {
    
                        'LeafName': item['LeafName'],
    
                        'DirName': item['DirName'],
    
                        'ItemType': item['ItemType'],
    
                        'DeletedDate': item['DeletedDate'],
    
                        'DeletedByEmail': item['DeletedByEmail'],
    
                        'Id': item['Id']
    
                    } for item in results
    
                ]
    
                print('Successfully retrieved items from recycle bin')
    
            else:
    
                print(f"Error code: {response.status_code}")
    
        except Exception as ex:
    
            print(f'Error occurred: {ex}')
    
    else:
    
        print('Connection is None!')
    
    return selected_results
    

    Note: Make sure to pass credentials of a Site Collection Administrator here if possible.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.