Failing to upload the file in Sharepoint if the file is already opened. how to close the file

Hi Team,

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

Thanks in advance
Likitha

Use a file-lock check with FileShare.None if the file is open by another process, the stream will fail immediately.

Can you please eloborate. and explain in code format

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.

Cheers

Where to write this logic @manish.pal

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

in invoke code activity?

Filestream means how to use in the code @nishiijain2000

Thanks
Likitha

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)

End Try

In Invoke code activity what is the input and output arguments we pass

In input you can pass as file path

getting this exception

Please check the File Path

In assign you pass Filepath but in invoke code you are passing in_filepath

Check if file is exist on this location or not and you have access to read the file.

Try this code it will check file is exits or not

If Not System.IO.File.Exists(filePath) Then
Throw New Exception("File not found: " & filePath)
End If

it is showing file not found but same file link if i copy in chrome then the file is opening

image

Please check the permission

Please confirm where file is placed is it in sharepoint or your local folder

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.

1 Like

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:)?

which path i should pass?

Thanks
Likitha

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

they extract the data from the Current run output file and they are failing to close the file so when the next run starts its failing

so we have to go with either API or 0365 activities correct?