I am trying to move sharepoint files, using the patch function from the MSGraph method, to another destination folder, but for some reason I keep getting the error when trying to compute the line, result = await self.user_client.drives.by_drive_id(parent_drive_id).items.by_drive_item_id(src_item_id).patch(request_body)
:
msgraph.generated.models.o_data_errors.o_data_error.ODataError:
APIError
Code: 403
message: None
error: MainError(additional_data={}, code='accessDenied', details=None, inner_error=InnerError(additional_data={}, client_request_id='XXXX', date=DateTime(2024, 5, 23, 8, 5, 34, tzinfo=Timezone('UTC')), odata_type=None, request_id='XXXX'), message='Access denied', target=None)
I think the issue lies in how I set up the request_body and/or how I defined the drive/item id’s in the results variable.
Does anyone have an idea what I might have done incorrectly? I also have the correct rights to move/write/read files in the corresponding Sharepoint site. Am I perhaps using the incorrect destination parent folder id in the request_body? If so, does anyone know how to get the correct id? Thank u in advance 🙂
async def _move_sharepoint_file(self, url, site_name, src_drive_path, file_name, dst_site_name, dst_drive_path, new_file_name=None):
# Check permissions and verify authentication - ensure proper scopes are included in token
# Get source site and its drive items
sites = await self.user_client.sites.with_url(url).get()
src_site_id = None
dst_site_id = None
for site in sites.value:
print(site.name)
if site.name == site_name:
src_site_id = site.id
if site.name == dst_site_name:
dst_site_id = site.id
# if src_site_id is None:
# raise ValueError(f"Source site with name '{site_name}' not found")
# if dst_site_id is None:
# raise ValueError(f"Destination site with name '{dst_site_name}' not found")
query_params = ItemsRequestBuilder.ItemsRequestBuilderGetQueryParameters(
filter = True
)
request_configuration = ItemsRequestBuilder.ItemsRequestBuilderGetRequestConfiguration(
query_parameters = query_params,
)
path_list = src_drive_path.replace("\", "/").split('/')
print(path_list)
drive_items = await self.user_client.drives.by_drive_id(src_site_id).items.get(request_configuration=request_configuration)
drive_items = drive_items.value
partial_dir = ""
print(f"path_list: {path_list}")
for dir in path_list:
if len(path_list) == 1 and path_list[0] == "":
correct_drive_items = drive_items
break
else:
partial_dir += f"/{dir}"
print(f"partial_dir: {partial_dir}") if self.verbose else None
[print(f"drive: {drive.name}") for drive in drive_items] if self.verbose else None
print() if self.verbose else None
for drive_item in drive_items:
if drive_item.name == dir:
break
else:
raise ValueError(f"Drive with name '{partial_dir}' not found")
drive_id = drive_item.id
drive_items = await self.user_client.drives.by_drive_id(src_site_id).items.by_drive_item_id(drive_id).children.get()
drive_items = drive_items.value
correct_drive_items = []
for drive_item in drive_items:
print(f"drive_item.name: {drive_item.name}") if self.verbose else None
parent_path = drive_item.parent_reference.path
print(f"drive_item.parent_reference.path: {drive_item.parent_reference.path}") if self.verbose else None
if parent_path.endswith(partial_dir):
correct_drive_items.append(drive_item)
print(f"YES") if self.verbose else None
drives = correct_drive_items
drives = [drive for drive in drives if drive.name in file_name]
if len(drives) == 0:
raise ValueError(f"Drive with name '{file_name}' not found")
src_item_id = drives[0].id
parent_drive_id = drives[0].parent_reference.drive_id
print(parent_drive_id)
# Find the source item
#src_item_id = await self.get_item_id(src_site_id, src_drive_path, file_name)
# Get destination folder ID
dst_drive_items = await self.user_client.drives.by_drive_id(dst_site_id).items.get(request_configuration=request_configuration)
dst_folder_id = None
for drive_item in dst_drive_items.value:
if drive_item.name == dst_drive_path.split('/')[-1]:
dst_folder_id = drive_item.id
parent_dst_id = drive_item.parent_reference.drive_id
print("Not none")
break
if src_item_id is None:
raise ValueError(f"Item with name '{file_name}' in path '{src_drive_path}' not found")
if dst_folder_id is None:
raise ValueError(f"Destination folder '{dst_drive_path}' not found")
# Get source drive ID
src_drive_items = await self.user_client.drives.by_drive_id(src_site_id).items.get(request_configuration=request_configuration)
src_drive_id = None
for drive_item in src_drive_items.value:
if drive_item.name == src_drive_path:
src_drive_id = drive_item.id
break
if src_drive_id is None:
raise ValueError(f"Source drive '{src_drive_path}' not found")
request_body = DriveItem(
parent_reference = ItemReference(
site_id= dst_site_id,
id = dst_folder_id,
),
name ="new-item-name.pdf",
)
result = await self.user_client.drives.by_drive_id(parent_drive_id).items.by_drive_item_id(src_item_id).patch(request_body)
if result.is_success:
print("File moved successfully.")
return result.content
else:
print(f"Failed to move file. Status code: {result.status_code}")
return None
I have tried several combinations of drive and item id’s, because I thought that would be the issue. But everytime I get the same error. The (almost) same method for downloading a Sharepoint file does work using MSGraph and I do have the correct rights in Azure but also in Sharepoint for the corresponding file.