i have to read all the emails and extract the details in the pdf which is an attachment from each mail and append them into the output file which is downloaded from SharePoint.
now while uploading the file, sometimes it is failing to upload because the file is already opened by someone in the backend.
How to check the file prior not in the end state… whether file is already opened then throw exception.
or any other ways to handle it using outlook not 0365 activities
Try
Using fs As New FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
End Using
catch ex as IOException
Throw New Exception(“file is open or lock”)
End Try
If your SharePoint upload is failing because the file is already opened by another user or process, you can check the lock before processing by using a simple FileStream check
This method works without O365 activities and is compatible with Outlook + local file scenarios.
Try
Using fs As New FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
End Using
catch ex as IOException
Throw New Exception(“file is open or lock”)
End Try
We use FileStream with FileShare.None.If the file is open by someone else, FileStream will throw an error.This helps us check before upload that the file is not locked.
You can use invoke code activity and there set language as VB from the properties then create an argument as string add the code into the invoke activity
Try
Using fs As New System.IO.FileStream(filePath,
System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None)
End Using
Catch ex As System.IO.IOException
Throw New Exception("The file is already open or locked: " & file path)
Before uploading the final output file, you can check if the file is already
opened by someone by trying to open it in exclusive mode using FileStream.
If the file is locked by Excel/SharePoint/another user, FileStream throws an error.
Based on that, you can throw your own exception.
Use Invoke Code (VB):
Try
Using fs As New IO.FileStream(“filepath”, IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.None)
End Using
out_IsLocked = False
Catch ex
out_IsLocked = True
End Try
If out_IsLocked Then
Throw New Exception(“File is already opened by another user.”)
End If
This works without O365 activities and also detects SharePoint locks.
This logic will work if the file is opened in sharepoint? @MohammedShabbir
because filepath( https://CompanyName.sharepoint.com/teams/Shared Documents/TNT & Schenker Logs/Test/Dachser_Customs_Clearance.xlsx) value in the invoke code, i am passing sharepoint file path which is not accepting.
throwing error… file not found.
will file stream works only for checking local file filepath not the sharepoint files(https:)?
FileStream does not support SharePoint “https://” URLs.
The .NET file operations inside Invoke Code only work with local or network drive paths, not web links.
If you want to use FileStream, you must either:
Sync the SharePoint using OneDrive and pass the local synced file path, or
Use SharePoint/Graph API activities to download the file to a temp folder, process it, and re-upload it.
Also, can you tell me how long is the file kept open by user ? We can have some alternalte workaround