I made an event handler by overriding SharePoint’s ItemAdded method. Inside my event handler, I call the following code:
ConvertedItem result = ConvertFileToPdf(file, folder, options, pdfOptions);
However, the conversion fails; the exception returned is “Source file is invalid. It does not represent an existing object in a document library or current user does not have permission to access it.” I also tried wrapping the aforementioned code with SPSecurity.RunWithElevatedPrivileges(…) but it didn’t help. Do you have any advice on this?
You can find sample code for adding an ItemAdded event handler in our demo: https://releases.aspose.com/words/sharepoint. Please note that it is made with VSeWSS 1.1 for Visual Studio 2005.
If it does not help you, please show how you create the source SPFile object to pass as a first argument to ConvertFileToPdf(). I will try to reproduce the issue and provide you more information.
Hi,
Thank you very much for your prompt response. I tried the sample code but it’s unsuccessful also. I noticed that the event handler is triggered before I see in the UI that the document was actually added; is this a possible reason why the conversion fails? Kindly see the attachment for my code. I deployed and tried your Converter feature and it works so I suspect I must be calling the ConvertFileToPDF(…) at the wrong time. I’ve also thought about your workflow but my requirement right now is to use an event handler. Hope your team can further advise me on this.
Thank you for your time.
Hi,
I tested out my event handler on another document library and the conversion worked. In the versioning settings of the document library where the conversion worked, the “Require documents to be checked out before they can be edited?” field was set to “No”. The previous document library where the conversion did not work was set to “Yes”. Maybe this information can help others who are attempting to write an event handler like mine. Most probably there’s a more appropriate event handler to override to cater to the setting where documents are required to be checked out before they can be edited.
Hi,
Thank you for the additional information. I managed to reproduce an issue similar to yours. It occurs when the document library has the settings “Require documents to be checked out before they can be edited?” set to true. Could you please check that your document library requires a checkout before editing? If so, could you please check if turning this setting off solves the problem?
The issue occurs because we actually use an elevated security context in order to check file permissions. Using RunWithElevatedPrivileges() in the event handler like you attempted will not help in this case. The current user obviously has the permissions to the item because he just added it. But the problem occurs because from the context of another (elevated) user the newly added file does not exist until it is checked in. So the converter does not see just added file until it is checked in.
Thanks and Best Regards,
Well, I posted my previous message before I refreshed the thread in the browser and saw your previous post.
Thanks you for the additional information. So it confirms that the problem occurs when a document library requires a checkout before editing. I have linked this thread to the appropriate issue in our defect database. You will be notified as soon as the issue is resolved.
Thanks and Best Regards,
Hi Dmitry,
Thanks for your prompt help. I just added an ItemUpdated event receiver to accommodate my document libraries with checkout enforced. My ItemAdded event receiver is still there but to avoid redundancy, I put an extra condition to convert only when the document library does not have checkout enforced.
Thank you once again for your prompt response; it’s much appreciated.