Python Forum
How to get all items in SharePoint recycle bin by using sharepy library in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get all items in SharePoint recycle bin by using sharepy library in Python?
#1
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
Reply
#2
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/sharep...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.
Reply
#3
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 244 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Updating sharepoint excel file odd results cubangt 1 933 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 860 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Removal of items in .txt using python nanakochan 8 1,846 Sep-02-2022, 04:58 PM
Last Post: perfringo
  SharePoint Online/365 authentication using .cer file mart79 0 2,071 Nov-08-2020, 03:31 PM
Last Post: mart79
  concatenating 2 items at a time in a python list K11 3 2,417 Oct-21-2020, 09:34 AM
Last Post: buran
  Authentication error when accessing Microsoft SharePoint klllmmm 3 10,521 Jun-10-2020, 07:46 AM
Last Post: nuffink
  how to use items combobox in table name sqlite in python hampython 1 2,736 May-24-2020, 02:17 AM
Last Post: Larz60+
  Access list items in Python kamaleon 2 2,409 Dec-31-2019, 11:10 AM
Last Post: kamaleon
  Pulling data from Sharepoint list gandiswapna0388 1 2,654 Jul-17-2019, 08:35 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020