Calling OnSaveRequest Event using a custom button (c#)

B

Benjamin Wegner

Hi!

I created a form with a button. In the buttons code I tried to save the
current document using
thisXDocument.Save();
This will trigger the OnSaveRequest event but when
e.PerformSaveOperation()
is reached, an exception will be thrown.

When I click on the menuentry 'file -> save' the event gets triggered
as well and everything works fine.

It seems that when using the menu, Infopath sets some other values
prior triggering the event.

Am I able to do the same as Infopath? e.g. use some other method than
thisXDocument.Save();?

To give you an overview:
I want to create a form where users can click buttons like 'forward to
approver', 'approve request' or 'decline request'.

Regards,
Benjamin
 
S

Scott L. Heim [MSFT]

Hi Benjamin,

When you call the "Save" function (or a number of other functions as well)
through code it requires your form to be "fully trusted." This can be
accomplished by signing your form with a valid digital certificate or by
creating a setup package (with the RegForm tool from the InfoPath SDK)
where your users would need to install your solution.

Here are some links that should be of benefit:

Understanding Fully Trusted Forms
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/
ipsdkUnderstandingFullyTrustedForms.asp

Using the Form Registration Tool
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/
ipsdkUsingTheFormRegistrationTool.asp

Deploy a Fully Trusted Form to a SharePoint Form Library
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipsdk/html/
ipsdkDeployURNFormToSharePoint_HV01086376.asp

InfoPath SDK
http://www.microsoft.com/downloads/details.aspx?FamilyId=351F0616-93AA-4FE8-
9238-D702F1BFBAB4&displaylang=en

I hope this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Benjamin Wegner

Hi Scott,

thanks for your answer, but my form is alread signed and fully trusted.
I deployed this to a local folder and to a sps list, both the same
problem.

The problem occures only the first time I try to save a new document.

Regards,
Benjamin
 
B

Benjamin Wegner

When I call the save method within my btnApprove_OnClick eventhandler
in a new document, I get the following exception:
InfoPath cannot save the form.
The OnSaveRequest function returned a value indicating that the save
failed.
The form does not have a file name.
at
Microsoft.Office.Interop.InfoPath.SemiTrust.SaveEvent.PerformSaveOperation()
at
Microsoft.Office.Interop.InfoPath.SemiTrust.SaveEventWrapper.PerformSaveOperation()
at
Microsoft.Office.Interop.InfoPath.SemiTrust.SaveEventWrapper.PerformSaveOperation()
at mynamespace.ApplicationForm.OnSaveRequest(SaveEvent e) in
formcode.cs:line 85
at
Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnSaveRequest(SaveEvent
pEvent)

at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocument.Save()
at
Microsoft.Office.Interop.InfoPath.SemiTrust.XDocumentWrapper.Save()
at
Microsoft.Office.Interop.InfoPath.SemiTrust.XDocumentWrapper.Save()
at mynamespace.ApplicationForm.btnSubmit_OnClick(DocActionEvent e)
in formcode.cs:line 149
at
Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent
pEvent)
 
S

Scott L. Heim [MSFT]

Hi Benjamin,

Ah - now I have a better understanding. When you call the "Save" method,
this is used when the form has been previously saved - as such, the first
time you call this method it will fail.

As you are using .NET, there are a couple of options:

- Display a .NET Save Dialog to allow the user to save the form
- Use a "try/catch" statement and if the save fails, drop to the "SaveAs"
method and hard-code the location

Let me know if this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Benjamin Wegner

Hi Scott,
I am using the following code. It's nearly the same you suggested.

Regards,
Benjamin

if( thisXDocument.IsNew )
{
if( thisXDocument.Solution.URI.LastIndexOf("Forms/") > 0 )
{
string url = thisXDocument.Solution.URI.Substring(0,
thisXDocument.Solution.URI.LastIndexOf("Forms/"));
thisXDocument.SaveAs(url + this.GetFileName() + ".xml");
}
else
{
string url = thisXDocument.Solution.URI.Substring(0,
thisXDocument.Solution.URI.LastIndexOf("\\"));
thisXDocument.SaveAs(url + "\\" + this.GetFileName() + ".xml");
}
}
else
{
thisXDocument.UI.SetSaveAsDialogFileName(this.GetFileName());
thisXDocument.Save();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top