Python Forum
How to get all items in SharePoint recycle bin by using sharepy library in Python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to get all items in SharePoint recycle bin by using sharepy library in Python? (/thread-41888.html)



How to get all items in SharePoint recycle bin by using sharepy library in Python? - QuangHuynh - Apr-03-2024

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 owner of the site. 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



RE: How to get all items in SharePoint recycle bin by using sharepy library in Python? - pintailscratchy - Apr-10-2024

You need to accomplish two things here.

Get a list of files (directories or basic files) in the directory of your choice.
Loop through this list of files, determining whether each item is a file or a directory. Repeat steps 1 and 2 for each directory.

More documentation is available at https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-files-attached-to-list-items-by-using-rest.
def getFilesList(directoryName):
    ...
    return filesList

# This will tell you if the item is a file or a directory.
def isDirectory(item):
    ...
    return true/false
I hope this helps.



RE: How to get all items in SharePoint recycle bin by using sharepy library in Python? - SandraYokum - Apr-10-2024

To retrieve all items from the recycle bin on SharePoint, including those deleted by other users, you can modify your code to use SharePoint's administrative APIs. Here's an updated version of your _get_item_information_from_recycle_bin function that uses administrative privileges to access the recycle bin:
def _get_all_items_from_recycle_bin(self, s, SHAREPOINT_SITE):
    selected_results = None
    recycle_bin_endpoint = f"{SHAREPOINT_SITE}/_api/site/recyclebin"
    if s is not None:
        try:
            headers = {'accept': 'application/json;odata=verbose'}
            response = s.get(recycle_bin_endpoint, headers=headers)
            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 the recycle bin')
            else:
                print("Error code: %s", str(response.status_code))
        except Exception as ex:
            print('Error occurred: %s', ex)
    else:
        print('Connection is None!')
    return selected_results
[/python]