Welcome to MSDN Blogs Sign in | Join | Help

InfoPath Team Blog

Tips and tricks to get the most out of Microsoft InfoPath

News

  • For questions, comments, and feedback please use the public newsgroup: microsoft.public.infopath
    This is provided "AS IS" with no warranties, and confers no rights. Use of included script samples and forms are subject to the terms specified in the Terms of Use.
Submitting to 'this' document library

Have you ever needed to develop an InfoPath form template that submits back to a SharePoint document library but you did not initially know the server or library name when developing the form? Or have a scenario where your InfoPath form template could be published (or added as a content type) to multiple SharePoint document libraries and you need the submit location to be dynamic? Well read on to find out how you can do this!

When you create an InfoPath form template that needs to be able to submit to a SharePoint document library, you need to specify that connection in the initial design of the form template. However, at run time you can use managed code to determine the server and library name where the form was launched and then modify the “FolderUrl” property of the submit data connection so the form is submitted to the appropriate library.

So let’s get started setting up this sample. The C# walkthrough below uses the new InfoPath 2007 managed object model; below, you will find attached the same logic implemented in InfoPath 2003 managed object model and in InfoPath 2003 JScript.

Step 1: Create a sample InfoPath Form Template

Create a browser-compatible form template as following:

  1. Add 3 text box controls, all with a data type of text, named as follows: strFormURL, strLocation and strFolderName
  2. Add a “Submit” data connection named “Main submit”:
    • Set the document library name to a dummy SharePoint document library (i.e. http://server/dummyLib)
    • Use the concat function to concatenate the strFolderName field and _Test for the File Name property: concat(my:strFolderName, "_Test")
    • Enable the Allow overwrite if file exists property
  3. Set the Security Level of the form to Full Trust and sign the form template with a digital certificate
  4. Enable the Submit functionality (Tools | Submit Options) and choose the “Perform custom action using code” option

Now that we have the form, controls and submit functionality, let’s add the code to make this process work:

 

Step 2: Add the code

  • Click the Edit Code button on the Submit Options dialog
  • Create the following FormState Dictionary object – this will be used to store the form’s location when the form is initially opened:

private object _strUri
{
   get { return FormState["_strUri"];
  
set { FormState["_strUri"] = value; }
}

 

  • Add the following code to the Forms Loading event: 

// Get the Uri (or SaveLocation in a browser form) of where
// the form was opened.
// See if the form was opened in the browser

Boolean OpenedInBrowser = Application.Environment.IsBrowser; 

// If so, we will get the "SaveLocation" from the InputParameters

if (OpenedInBrowser)
  
_strUri = e.InputParameters["SaveLocation"].ToString();
else  

   //If it was opened in the client, we will get the Uri
  
_strUri = this.Template.Uri.ToString();

 

// Populate the fields on the form - keep in mind, this

// not necessary - this is simply to see the results

PopulateLibInfo(OpenedInBrowser);

 

  • Add the following procedure to the Forms class: 

private void PopulateLibInfo(Boolean OpenedInBrowser)

{

// Create a Navigator object for the main DOM
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

 

// Create Navigator objects for each field

XPathNavigator xnFormURL = xnDoc.SelectSingleNode("my:myFields/my:strFormURL", this.NamespaceManager);
XPathNavigator xnLocation = xnDoc.SelectSingleNode("my:myFields/my:strLocation", this.NamespaceManager);

XPathNavigator xnFolderName = xnDoc.SelectSingleNode("my:myFields/my:strFolderName", this.NamespaceManager);

 

// Get the Uri stored in the FormState Dictionary variable

string strUri = _strUri.ToString();

 

// Create a variable to store the path (URL) to the document library

string strPath = "";

if (OpenedInBrowser == true) {

   //If we are open in the browser, the strUri value is just

   //the server name and library - so we just need to get

   //the URL without the last "/"

   strPath = strUri.Substring(0, strUri.LastIndexOf("/"));

} else {

   // Parse just the path to the document library -

   // this would return something like this:

   //  http://server/library

   strPath = strUri.Substring(0, strUri.IndexOf("Forms") - 1);
}


// Now, parse the URL to where the document library resides;
// this would return something like:

//    http://server or http://server/site
string strLoc = strPath.Substring(0, strPath.LastIndexOf("/"));


// Lastly, parse the URL to return just the document library name -

// in this case,we are looking for the last "/" character

// knowing that what comes after this is the document library name

string strFolder = strPath.Substring(strPath.LastIndexOf("/") + 1);

 

// Populate the fields on the form – we will use these

// values in the Submit process

xnFormURL.SetValue(strUri);

xnLocation.SetValue(strLoc);

xnFolderName.SetValue(strFolder);
}

 

  • Add the following code to the form’s Submit event:

// Create a Navigator object for the main DOM
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();


// Create Navigator objects for the fields we will

// use to modify the FolderUrl
XPathNavigator xnLocation = xnDoc.SelectSingleNode("my:myFields/my:strLocation", this.NamespaceManager);
XPathNavigator xnFolderName = xnDoc.SelectSingleNode("my:myFields/my:strFolderName", this.NamespaceManager);

 

// Get a reference to the submit data connection

FileSubmitConnection fc = (FileSubmitConnection)this.DataConnections["Main submit"]; 

 

// Modify the URL we want to submit to by concatenating the

// xnLocation and xnFolderName values

fc.FolderUrl = xnLocation.Value + "/" + xnFolderName.Value;

 

// Execute the submit connection

try

{

   fc.Execute();

   e.CancelableArgs.Cancel = false;

}

catch (Exception ex)

{

   e.CancelableArgs.Cancel = true;
}

 

  • Build and save the project
  • Publish and test

And that is it! You now have an InfoPath form template that will submit to whatever document library the form was opened from so you do not need to know this information when designing the template.

 

Scott Heim

Support Engineer

Posted: Wednesday, November 08, 2006 7:03 AM by infopath
Filed under: ,

Attachment(s): submitToThisLibrary.zip

Comments

Todd Baginski's SharePoint 2003 and MOSS 2007 Blog said:

First of all I would like to say THANK YOU to everyone who came to my sessions last week! I had a wonderful...

# November 14, 2006 1:04 AM

fordhamSteve said:

I think you are missing this line of code from your Submit event:

FileSubmitConnection fc = (FileSubmitConnection)this.DataConnections["Main submit"];

# November 15, 2006 3:54 PM

Scott Heim said:

Hi fordhamSteve,

Thank you for alerting us to the missing line of code - the sample has now been corrected.

Scott Heim

# November 17, 2006 8:11 AM

dlgross said:

I am getting the following error when i try to preview my form. I have limited experience with javascript.

Any idea what I might have done wrong? Thanks, Dean

InfoPath cannot open the selected form because of an error in the form's code.

The following error occurred:

Expected ';'

File:script.js

Line:8

private object _strUri

# November 17, 2006 10:52 AM

Scott Heim said:

Hi Dean,

Most likely the cause of this problem is the fact that this code is C#.NET and not JScript. If you want to test using the same form and C#, you will need to remove the JScript code first and then set the form to use C#:

    - Open the XSN in Design View

    - From the Tools menu choose Form Options

    - Select the Programming Category

    - Click the Remove Code button to remove the JScript files

    - From the Form Template Code Language box choose C#

Scott

# November 17, 2006 12:17 PM

dlgross said:

Scott, thanks, When I look at the Form Options, Advanced tab, there is no Programming Category, there is only a Programming Language box with Form Code Language choices of JScript and VB Script. I'm using IP 2003 SP1. How do I make it use C#?

Dean

# November 17, 2006 3:40 PM

Scott Heim said:

Ah - 2003...that's a bit different. Do you need to use this form or can you start over with a new form? Either way, you will need to use the Visual Studio Tools for Office InfoPath Toolkit (in conjunction with Visual Studio) to create a managed code solution. If you don't have Visual Studio then the only code options available are JScript and VBScript and you will need to convert the C# code to JScript.

Scott

# November 17, 2006 5:15 PM

dlgross said:

I was just testing your approach to see how it would work in a dummy form before I tried to implement it in an existing form. I do have VS 2003 with the toolkit. Would it be worthwhile to get VS 2005 if this is the only coding that I'm doing?

Dean

# November 17, 2006 9:38 PM

Scott Heim said:

Hi Dean,

Certainly there are some benefits to moving to VS2005 but although I have not tried it, theoretically the same basic process/code should work using the managed code option with VS2003.

Scott

# November 21, 2006 1:01 PM

dlgross said:

Scott, is this supposed to work with IP 2003 or 2007? I keep getting compile errors in 2003.

Dean

# November 21, 2006 1:28 PM

Scott Heim said:

Hey Dean,

Although that code is C#, it is specific to the new object model in 2007. To get this to work in 2003, you will need to convert that code to the 2003 object model.

Scott

# November 21, 2006 1:37 PM

infopath said:

By popular demand, Scott converted the code used in this article into the InfoPath 2003 managed object model (in C#) and into JScript. You can download the full listing in the attachments section above.

Alex

# November 22, 2006 1:12 PM

dlgross said:

Thanks for making the conversions Scott, I'm getting closer to getting this to work, but I don't have a Digital Certificate. Is there any workaround available? Dean

# November 27, 2006 12:16 PM

Scott Heim said:

Hi Dean,

Glad you hear you are making headway! For testing purposes, you can create a "self-cert" (basically a certificate that does not have a chain of trust, hence it is only valid on the machine in which it was created) by choosing the Create Certificate button in Tools | Form Options | Security and Trust category.

Scott

# November 27, 2006 1:06 PM

nemegeerm said:

Hi,

When I add the code strUri = thisXDocument.Solution.URI; to get the url I get a uniform resource identifier (urn:eLNDTX:Innx). Am I missing something ? Why don't I get a url ??

Marc

# January 9, 2007 11:56 AM

infopath said:

Hi Marc,

How are you testing? Did you deploy the form to your SharePoint site? Also - did you "install" this form on the the client machine?

Scott

# January 9, 2007 12:16 PM

nemegeerm said:

Hi Scott,

Yes, I deployed to a sharepoint site and installed this form on my client machine on the development environment. In production I have a certificate so that I don't have to install the form on the client machines. I attached VS to the infopath process, put a break in the on load and filled out a new form and inspected the contents of the variable.

Thanks for your input,

Marc

# January 10, 2007 2:02 AM

infopath said:

Hi Marc,

When you install a form template, then the code that I supplied as a sample is returning the correct data: it is the Uri of the installed form - not from the SharePoint doc lib. I would suspect that is most cases, if you deploy a form template to SharePoint, you will not be "installing" it to the client machines. (Hence, I did not test for this scenario.) If you needed to install the template on your dev machine just for testing purposes then you can simply create a "self-cert" and use this instead.

In short - to test the sample code, you will need to uninstall your form.

Let me know if this works for you!

Scott

# January 10, 2007 7:51 AM

nemegeerm said:

Scott,

I installed the office 2003 certification support, created a certificate, signed my compiled infopath template with it and last but not least deployed the form to my sharepoint document library.

1) The url problem is solved. I get the nice url that I was looking for ...

2) When I try to submit, I can't submit because my certificate is not from a trusted authority, which makes sense to me because I generated it.

And of course my full development environment is on one machine, a virtual one but that does not matter. It's a 2003 with Sharepoint services, visual studio, infopath, etc.

I get the following error:

InfoPath cannot submit the form.

An error occurred while the form was being submitted.

InfoPath cannot load this form. The signature on this form is not from a trusted publisher.

  at Microsoft.Office.Interop.InfoPath.SemiTrust.DAVAdapter.Submit()

  at Microsoft.Office.Interop.InfoPath.SemiTrust.DAVAdapterObjectWrapper.Submit()

  at Microsoft.Office.Interop.InfoPath.SemiTrust.DAVAdapterObjectWrapper.Submit()

  at eLNSolution.eLNSolution.DoSave() in c:\documents and settings\administrator\my documents\visual studio projects\elnsolution\formcode.cs:line 420

  at eLNSolution.eLNSolution.OnSubmitRequest(DocReturnEvent e) in c:\documents and settings\administrator\my documents\visual studio projects\elnsolution\formcode.cs:line 357

  at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnSubmitRequest(DocReturnEvent pEvent)

Am I missing something ?

Marc

# January 11, 2007 5:32 AM

infopath said:

Hi Marc,

I am a bit confused I think: are you using InfoPath 2003 or 2007? (You mentioned certificate support from 2003.) Also - you set the secuity level of the form template to Full Trust - correct?

Scott

# January 11, 2007 8:40 AM

nemegeerm said:

Scott,

I'm using Infopath 2003 with C# code for the form.

The security level is full-trust (managed code). THat's the reason why I install the form on my development machine. I production, there is a certificate.

Marc

# January 11, 2007 10:31 AM

infopath said:

Hi Marc,

Thank you for the clarification. When you launch the InfoPath form from your SharePoint library, do you see a security prompt with options to Open , Trust this Publisher, etc.?

I do all of my testing with a Self-Cert and this process works for; however, I do see this prompt when I launch the form. I choose Open (not Always Trust) and the submit works as well.

If you do not see this prompt, try this for me:

- Open your XSN in Design View

- From the Tools menu choose Form Options

- Select the Security tab

- Click the Create Certificate button to create a new certificate

- Select this new cert as the cert to sign the XSN

- Re-publish and test

Scott

# January 11, 2007 2:44 PM

nemegeerm said:

Scott,

Yes I see the form. I click the always trust and then yes.

I tried first with the "create certificate" in Infopath. Deployed and tested. WORKS

I tried again with the other certificate. Deployed and tested. It WORKS also.

Gues I've done something wrong last week ...

Thanks for your support,

Marc

# January 15, 2007 7:23 AM

infopath said:

Thanks for letting me know Marc - I am certainly glad you were able to get this working!

Scott

# January 15, 2007 7:46 AM

ivl said:

Hi Scott,

Thank you for posting this - I have been looking for such a solution for a LONG time.

This solves one of my two problems.

The second problem is related:

Is it possible to have a receive location that is dynamic? I have an InfoPath 2007 form which receives data from a SharePoint list (to populate a drop down) and would like the server name part of the url to be dynamic.

I have tried doing this using the following code:

SharepointListQueryConnection mylist = (SharepointListQueryConnection)this.DataConnections["mylist"];

mylist.SiteUrl = new Uri("http://otherserver/TheList");

but the SiteUrl property is read-only.

Is there any way to do this?

# January 16, 2007 6:50 AM

infopath said:

Hi,

Sorry for the delay in getting back to you on this. Unfortunately, I don't see a way to accomplish this as there are no methods to update these values.

Scott

# January 18, 2007 11:19 AM

derrickgh said:

Hi,

 I am using InfoPath 2003 and SharePoint v2. I was able to use the solution above on a test form and work perfectly.

Now when I try to add this code to the forms I have in production Iam getting a compiling error " 'null' is null or not an object File: script.js Line:92"

Line 92 is "xnFormURL.text = strUri;

These forms are also conected to a SQL DB backend, not sure if that could be causing the problem. Any assistance in getting this to work would be greatly appreciated. Thanks!

# January 18, 2007 3:44 PM

infopath said:

Hi,

It sounds like your xnFormURL object is null - which is typically caused by an invalid XPath expression to that node.

You will need to step through your code and make sure the XPath expression is correct.

Scott

# January 19, 2007 2:33 PM

Todd Baginski's SharePoint 2003 and MOSS 2007 Blog said:

First of all I would like to say THANK YOU to everyone who came to my sessions last week! I had a wonderful

# May 5, 2007 9:36 PM

shirinp said:

One scenario that I have had trouble with is if you publish the form as a content type on the site collection. In that situation, the form is published under the site collection url, not the form library url.

I set a document library under that site collection to manage content types and I select the content type to be the one that was published using infopath. When I click new on the document library and select that content type, it opens the form in the infopath client. However, I'm unable to capture the form library url that I was in.  

How can I capture that url so that I can use it to change the path of the submit dynamically?

# May 15, 2007 3:46 PM

Scott Heim said:

Hi shirinp,

Unfortunately I have not found a way around this...other than displaying the form in the browser instead of the client. This should allow you to capture the form library URL. If you must open it in the client and have it published as a site content type, I know of no way to capture this information.

Scott

# May 16, 2007 9:19 AM

zullu said:

Hi Scott,

I have tried the attached solution for Infopath 2007 C# code.

I am getting an error: "System.ArgumentOutOfRangeException, Length cannot be less than zero."

- in the PopulateLibInfo function at the line where you are trying to Parse the path to doc library to return something like: http://server/library.

strPath = strUri.Substring(0, strUri.IndexOf("Forms") - 1);

When I look at the value returning from strUri.IndexOf("Forms"), I can see it is returning "-1".

Am I going wrong somewhere!

Thanks in advance for any kind of help.

zullu.

# June 6, 2007 12:48 PM

infopath said:

Hi zullu,

Where did you publish the form and how are you trying to open it?

Scott

# June 6, 2007 12:59 PM

BobC said:

shirinp,

Just so you know, you are not alone regarding this problem...

I also tried passing a parameter indicating the location, but wss doesn't allow parms to be set for a content type.  Unless Scott has an idea for this.

# June 7, 2007 4:14 PM

BobC said:

I get prompted for the file name.  How do I set this in code?

# June 7, 2007 6:01 PM

infopath said:

Hi BobC,

You would typically use a field in the data source to store the name of the submitted form. So you can either set this using a Rule or use code similar to the following:

XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

XPathNavigator xnFormName = xnDoc.SelectSingleNode("/my:myFields/my:formNameField", this.NamespaceManager);

xnFormName.SetValue("Form1");

Scott

# June 8, 2007 10:58 AM

pkarpati said:

I've tried to call this.Submit() method with your sample code in my own Button Clicked event, but the Forms Server tell me that I cannot call XmlForm.Submit in a SubmitEventHandler.

I want to do this because I need to call Rules before Submit in your way.

Can you help me?

Peter

# July 19, 2007 9:44 AM

infopath said:

Hi pkarpati,

You have a couple of options:

- Change your button to simply be a "Submit" button (don't use code)

- Use the click event of the button to call the same code that is in the "Submit" event and remove the Submit event handler.

NOTE: if you use the 2nd option, you will  need to remove the "e.CancelableArgs" lines.

Scott

# July 19, 2007 6:00 PM

RChakravarti said:

Hello Scott,

I have a question, regarding using CODE to save the form instance to a subfolder that only an admin can see.

USE CASE:

========

  Once a user enters a form (such as a survey in WSS3, through a web enabled InfoPath form), the user should not be able to re-open OR read any other entries by other people.

We thought we could have a folder called "Submitted" in the WSS library, where the form could be saved using the "SaveLocation" QueryString property, and that this folder could only be permissioned to be viewable by "Admins".

ISSUE:

=====

Even when we used SPSecurity.RunWithElevatedPrivileges(),  we get an access denied for anyone NOT AUTHORIZED to see the "Submitted" subfolder. We are unable to fgiure out why.. When logged on as admin, the save works just fine.

---------------

public void FormEvents_Submit(object sender, SubmitEventArgs e)

       {

           SPSecurity.RunWithElevatedPrivileges(delegate()

           {

               // Get a reference to the submit data connection

               FileSubmitConnection fc = (FileSubmitConnection)this.DataConnections["Main submit"];

               // Modify the URL we want to submit to by concatenating the

               // xnLocation and xnFolderName values

               fc.FolderUrl = "http://servername:3000/sites/testsite/Feedback/Submitted";

               // Execute the submit connection

               try

               {

                   fc.Execute();

                   e.CancelableArgs.Cancel = false;

               }

               catch (Exception ex)

               {

                   e.CancelableArgs.Cancel = true;

               }

           });

       }

---------------

NOTE:

a) The form library is called "Feedback" with a subfolder "Submitted" which has Admin Rights only.

b) Users instantiate a form instance through a custom link which has the SaveLocation QueryString param pointing to the (hidden) "Submitted" folder, with the desired behaviour of them not being able to see the form once submitted.

Any help you can provide would be great.

Thanks,

Rajiv

# July 23, 2007 12:39 PM

infopath said:

Hi Rajiv,

There is no built-in InfoPath functionality to limit the forms a user can see once submitted. You could create a custom SharePoint "View" that uses the "[Me]" filter to limit what documents they see when they open that library; however, this is not "security" per sé just a filter.

Regarding the permissions not working - I believe you would be better served by asking this of the SharePoint group.

Scott

# July 25, 2007 3:26 PM

Neelesh Kalani said:

I am also facing the same problem as Shirinp. In my case, i upload the form to the form server and then activate it to a particular sitecollection.

of cNow i have a number child sites in tht sitecollection which will use this form content type, so i need to find a way to get the URL of the form library at run time. Ofcourse this post solves the problem for the forms that are opened using the browser but what to do in case forms are opened using Infopath?

Have anybody got the solution for the problem ?

# September 21, 2007 2:25 AM

Neelesh Kalani said:

Also, I am able to submit the new form using this method, but how can i resubmit or save the same form when it is opened in edit mode.

# September 25, 2007 6:58 AM

infopath said:

Hi Neelesh,

If I understand correctly you are asking how to open an existing form from a library, modify it and then re-save it using the same name - correct? If so, then when you setup the "submit" data connection make sure you enable the option: Allow overwrite if file exists."

Scott

# September 26, 2007 9:36 AM

Neelesh Kalani said:

Hi Scott,

Thanks for replying.

Is there any wrok around for my first problem?

Also, i need to add data (at run time) to a repeating table which has two columns, one consisting of checkbox and the other has text box.

Can you pass on some links.....

Also, is it okay to use Sharepoint object model directly in infopath code ?

Neelesh

# September 26, 2007 11:48 PM

infopath said:

Hi Neelesh,

As I have mentioned a couple of times before, there is no way that I know of to get this information.

Regarding adding data at runtime, since this is a repeating node you will want to use code to accomplish this. There are a number of ways this can be done: append the new row as I am dong below or if you want to extract the form files you can get the actual "OuterXml" of the node you want to append and use this to append the row or if this is a client-only form you can use the "ExecuteAction" method to add a new row.

The following sample assumes the following:

- A secondary data connection to the SQL Server Northwind Shippers table - this connection is called Shippers

- A Repeating Table with 3 columns: f1_ID, f2_CompanyName and f3_Phone

- On the Loading event of the form, I populate those repeating table fields using the following code:

public void FormEvents_Loading(object sender, LoadingEventArgs e)

       {

           //Create a NamespaceManager to use when referencing namespaces

           XmlNamespaceManager ns = this.NamespaceManager;

           //Create an XPathNavigator object for the main DOM

           XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

           //Create an XPathNavigator object for the Repeating Table node

           XPathNavigator xnRepeatingTable = xnDoc.SelectSingleNode("/my:myFields/my:group1", ns);

           //Create a DataSource object for our secondary data connection

           DataSource dsShippers = this.DataSources["Shippers"];

           //Create an XPathNavigator object for the data source

           XPathNavigator xnShippers = dsShippers.CreateNavigator();

           //Create an XPathNodeIterator object so we can enumerate all the records from the

           //secondary data connection

           XPathNodeIterator xiShippers = xnShippers.Select("/dfs:myFields/dfs:dataFields/d:Shippers", ns);

           //Loop through those records

           while (xiShippers.MoveNext())

           {

               //Create string variables for all the fields we need to populate

               //in the Repeating table

               string strID = xiShippers.Current.SelectSingleNode("@ShipperID", ns).Value;

               string strCompanyName = xiShippers.Current.SelectSingleNode("@CompanyName", ns).Value;

               string strPhone = xiShippers.Current.SelectSingleNode("@Phone", ns).Value;

               //Use the AppendChild method to add a new row to the repeating table and

               //concatenate each field value appropriately

               xnRepeatingTable.AppendChild("<my:group2><my:f1_ID>" + strID + "</my:f1_ID><my:f2_CompanyName>" + strCompanyName + "</my:f2_CompanyName><my:f3_Phone>" + strPhone + "</my:f3_Phone></my:group2>");

           }

           //Cleanup

           ns = null;

           xnDoc = null;

           xnRepeatingTable = null;

           dsShippers = null;

           xnShippers = null;

           xiShippers = null;

       }

I hope this helps!

Scott

# September 27, 2007 8:44 AM

Neelesh Kalani said:

Many Thanks for the suggestions.

That worked perfectly.

If some one also wish to clear the repeating tables befor filling them...following code will be useful:

string NodeGroup = /my:myFields/my:group7/my:openTable";

XPathNavigator FirstNode = MainDataSource.CreateNavigator().SelectSingleNode(NodeGroup + "[1]", NamespaceManager);

               XPathNavigator LastNode = MainDataSource.CreateNavigator().SelectSingleNode(NodeGroup + "[position()=last()]", NamespaceManager);

               if(FirstNode != null)

               {                  

                   if (LastNode != null)

                   {                    

                       FirstNode.DeleteRange(LastNode);

                   }

                   else

                   {

                       FirstNode.DeleteSelf();

                   }

               }

# October 11, 2007 4:48 AM

Neelesh Kalani said:

I have one more query.

One of the field in the repeating table is a date field and i am populating it from sharepoint list. it shows me the validating screentip "Only Date allowed".

I have not applied any validation and also the date which i am filling in the date filed is in the same format as specified for that field.

I also tried to delete the nil attribute but still no luck.

Any clues...

# October 11, 2007 4:58 AM

infopath said:

Hi neelesh,

Here is some sample code that works for me - in this case, "field1" has a "Date" data type:

XmlNamespaceManager ns = this.NamespaceManager;

           XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

           XPathNavigator xnDateField = xnDoc.SelectSingleNode("/my:myFields/my:field1", ns);

           //Remove the "nil" attribute

           if (xnDateField.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))

               xnDateField.DeleteSelf();

           xnDateField.SetValue("2007-10-12");

           ns = null;

           xnDoc = null;

           xnDateField = null;

Scott

# October 11, 2007 9:11 AM

Neelesh Kalani said:

Hi Scott,

I have already tried the same code, but it does not works.

In my case, i am first adding the row to the repeating table at run time using the code:

xnRepeatingTable.AppendChild("<my:group2><my:MyDate></my:MyDate></my:group2>");

Where, MyDate is the date field which is initialy empty and also not having any nil attribute.

After this i wrote the code to remove the nil attribute and set the value of the date field which is as follows:

XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

          XPathNavigator xnDateField = xnDoc.SelectSingleNode"/my:myFields/my:group1/my:group2" + "[" + Index of the row+ "]" + "/my:MyDate", ns);

          //Remove the "nil" attribute

          if (xnDateField.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))

              xnDateField.DeleteSelf();

          xnDateField.SetValue("2007-10-12");

But it still showing me the same error.

# October 12, 2007 1:15 AM

infopath said:

Hi neelesh,

And just to confirm, your date field is a node in your "main" DOM - correct? And if you look at the properties for this node, the data type is set to "Date (date)" - correct?

Scott

# October 15, 2007 8:35 AM

Neelesh Kalani said:

Yes, That is correct.

And i am getting the error when the date field is empty.

I am trying to insert a blank string in a field after delting the nil vallue.

I don't know how to insert blank date in the field.

Neelesh

# October 18, 2007 12:41 PM

infopath said:

Hi Neelesh,

I am confused: in your prior response, you advised you tried the code I supplied, which actually inserts a date, failed for you. Is this correct? If you actually specify a date in the code it fails? Or does it only fail if you try to insert a blank date? (If only when inserting a blank date, why do you want to insert a blank date??)

Scott

# October 18, 2007 12:50 PM

Neelesh Kalani said:

Hi Scott,

If i use the following code to add the date field then it shows the error "can not blank", while i have not used any validations.

xnRepeatingTable.AppendChild("<my:group2><my:MyDate></my:MyDate></my:group2>");

Neelesh

# October 24, 2007 12:26 AM

infopath said:

Hi Neelesh,

Can you please complete these steps and let me know the results:

- Create a new, blank form template

- Add a Repeating Table with just 2 columns

- Change the name of the text box in column 1 to: textField

- Change the name of the text box in column 2 to: dateField

- Your data source should look like this:

myFields

    group1

         group2

              textField

              dateField

- Change the data type of dateField to: Date (date)

- Add a button to the View

- Add the following code to the click event of the button:

XmlNamespaceManager ns = this.NamespaceManager;

           XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

           XPathNavigator xnTable = xnDoc.SelectSingleNode("/my:myFields/my:group1", ns);

           xnTable.AppendChild("<my:group2><my:textField>Test</my:textField><my:dateField></my:dateField></my:group2>");

- Test - when I execute this code, the new row is added and by default, the dateField field will be required.

Does this work for you?

Scott

# October 24, 2007 9:10 AM

Neelesh Kalani said:

This is what the problem is...I do not want the date field to be made required BY DEFAULT.

# October 25, 2007 12:04 AM

infopath said:

Hi neelesh,

Sorry about that - I guess that was the part I was not understanding. Modify the "AppendChild" line as follows:

xnTable.AppendChild("<my:group2><my:textField>Test</my:textField><my:dateField xsi:nil='true'></my:dateField></my:group2>");

As you can see, I have added the attribute "xsi:nil='true'" to that line of code. This allows for a blank value and insures the field is not required.

Scott

# October 25, 2007 8:25 AM

Brian111 said:

Hi

This worked great thanks.

Is it possible to use this technique on data connections ?

Thanks

Brian

# October 30, 2007 5:16 AM

scorphg said:

Is there a solution like this for vb?  Here is my issue.  I have a shrepoint library which is/will be storing many documents.

I wrote a infopath form that needs to submit the new document(s) to the sharepoint site.  I cannot use the default submit because of the need to organize the documents into specific locations/folders.  Once the form is completed, i have a field which has the "save as" filled in as well as the location with the appropriate url and folder.  so the question is how to submit to sharepoint server for this scenario.  The other scenario is "old" documents.  Those which have already been saved... how do I get the actual filename for the document, then proceed to overwrite it if any changes are made?  Is there a method which I can extract the url, filename, location/folder??

thanks in advance

# October 30, 2007 2:51 PM

infopath said:

Hi Brian,

Not sure I follow - are you asking if you can append data to secondary data connections in this manner?

Hi scorphg,

For the submit part of your question, that is what this post basically does: allow you to specify the submit location at runtime. Am I misunderstanding what you are asking?

Scott

# October 31, 2007 5:16 PM

scorphg said:

Hi Scott,

Thanks for the very quick response.  I dont think you are misunderstanding the question, as much as I may not be stating it correctly.  

Basically, the question is how do i get the filename of an existing form saved to sharepoint using vb managed code?

The c# code above is great, and I will use that for future projects, so thanks... is there the same code for vb managed code?

# November 7, 2007 10:34 AM

infopath said:

Hi scorphg,

Sorry for the delay - it has been quite busy. You would need to parse the Uri I believe but unfortunately I don't have any sample code for any of this in VB.

If I get some time, I'll see what I can come up with.

Scott

# November 15, 2007 5:14 PM

Neelesh Kalani said:

How can we populate a drop down using c# code?

I need to set both the value and the display names and also need to read those vales and display name back.

Right now i am only able to fill the values.

Any suggesstions?

# November 20, 2007 7:57 AM

infopath said:

Hi neelesh,

As you have probably seen, you do not have programmatic access to controls in InfoPath. What you would need to do is either populate a repeating structure in the main DOM that the dropdown is bound to or populate an XML resource file that is used as the source of data for the dropdown or use a web service to retrieve the data.

Scott

# November 21, 2007 10:45 AM

boverton said:

Hello,

This code works great for the initial submit.  But when editing an existing document I get errors.  5537   Any suggestions?  I think the URLs are different for a new document submit and an edit

cheers,

Brad

# December 1, 2007 3:49 PM

moss_sharepoint said:

Though this query does not belong to this blog, still I am posting it here as this is a very critical requirement in one of our projects.

Is there some way to convert the browser enabled infopath 2007 file saved in the form library to pdf/word document programmatically?

We have a windows service which will send these converted pdf/word files as an attachment in the email.

Thanks in advance.

# December 28, 2007 1:12 AM

infopath said:

Hi Brad,

I was finally able to get to the issue of opening and submitting an existing form and I did need to modify the code. This updated sample code is a bit verbose but I wanted to show the various steps. Here are the updates:

- Replace the sample Loading event code with the following:

// See if the form was opened in the browser

           Boolean OpenedInBrowser = Application.Environment.IsBrowser;

           //Variables to store the results of testing if the form

           //being opened is new and if not, the "source" location

           //parameter so we can get the server or server/site name

           Boolean newForm = true;

           string strSource = string.Empty;

           // Get the Uri (or SaveLocation/XmlLocation in a browser form) of where

           // the form was opened.

           if (OpenedInBrowser)

               if (this.New) //We will get the SaveLocation parameter

                   _strUri = e.InputParameters["SaveLocation"].ToString();

               else //We will get the XmlLocation parameter

               {

                   _strUri = e.InputParameters["XmlLocation"].ToString();

                   newForm = false;

                   //Store the "Source" parameter so we can parse this for

                   //the server/site URL

                   strSource = e.InputParameters["Source"].ToString();

               }

           else //Opened in the client

           {

               //See if we are opening a new form - if so, we will get the

               //Uri to the template

               if (this.New)

               {

                   _strUri = this.Template.Uri.ToString();

               }

               else

               {

                   //If opening an existing form, we will get the Uri

                   //of the form we are opening.

                   _strUri = this.Uri;

                   newForm = false;

               }

           }

           // Populate the fields on the form - keep in mind, this

           // not necessary - this is simply to see the results

           PopulateLibInfo(OpenedInBrowser, newForm, strSource);

- Replace the sample PopulateLibInfo procedure with this one:

private void PopulateLibInfo(Boolean OpenedInBrowser, Boolean newForm, string strSourceParam)

       {

           // Create a Navigator object for the main DOM

           XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

           // Create Navigator objects for each field

           XPathNavigator xnFormURL = xnDoc.SelectSingleNode("my:myFields/my:strFormURL", this.NamespaceManager);

           XPathNavigator xnLocation = xnDoc.SelectSingleNode("my:myFields/my:strLocation", this.NamespaceManager);

           XPathNavigator xnFolderName = xnDoc.SelectSingleNode("my:myFields/my:strFolderName", this.NamespaceManager);

           // Get the Uri stored in the FormState Dictionary variable

           string strUri = _strUri.ToString();

           // Create a variable to store the path (URL) to the document library

           string strPath = "";

           if (OpenedInBrowser == true)

           {

               if (newForm)

               {

                   //If we are open in the browser, the strUri value is just

                   //the server name and library - so we just need to get

                   //the URL without the last "/"

                   strPath = strUri.Substring(0, strUri.LastIndexOf("/"));

               }

               else

               {

                   //We need to get the server URL (up to the library name) so

                   //we can successfully submit the form.

                   string strServer = string.Empty;

                   //The following is broken into 2 parts so it is easier to see

                   //what we need to do:

                   //Get the URL up to the word "Forms" in the URL

                   strServer = strSourceParam.Substring(0, strSourceParam.IndexOf("Forms"));

                   //Get the URL up to the last "/" before "Forms" - this will be

                   //the server or server/site

                   strServer = strServer.Substring(0, strServer.LastIndexOf("/"));

                   strPath = strServer + strPath;

               }

           }

           else //Opened in the client

           {

               // Parse just the path to the document library -

               // this would return something like this:

               //  http://server/library

               //NOTE: This process will not work when an ADMIN deployed form

               //is opened in the client.

               try

               {

                   strPath = strUri.Substring(0, strUri.IndexOf("Forms") - 1);

               }

               catch { }

           }

           // Now, parse the URL to where the document library resides;

           // this would return something like:

           // http://server or http://server/site

           string strLoc = strPath.Substring(0, strPath.LastIndexOf("/"));

           // Lastly, parse the URL to return just the document library name -

           // in this case,we are looking for the last "/" character

           // knowing that what comes after this is the document library name

           string strFolder = strPath.Substring(strPath.LastIndexOf("/") + 1);

           // Populate the fields on the form – we will use these

           // values in the Submit process

           xnFormURL.SetValue(strUri);

           xnLocation.SetValue(strLoc);

           xnFolderName.SetValue(strFolder);

       }

With these changes, you should be able to open and submit either a new or existing form.

Scott

# January 2, 2008 2:17 PM

derrickgh said:

I was able to get this work when creating a blank template but could not get it to work using a form that is connected to a SQL DB. The error I get when opening the form from SharePoint is:

"System.NullReferenceException

Object reference not set to an instance of an object.

  at Template13.FormCode.PopulateLibInfo()

  at Template13.FormCode.FormEvents_OnLoad(DocReturnEvent e)

  at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnLoad(DocReturnEvent pEvent)"

Will this solution work with a form that is created for a SQL DB? Thanks!

# January 22, 2008 3:32 PM

infopath said:

Hi derrickgh,

Yes - this will work when your form template has been created from a database; however, the XPATH will be different for the fields you create in this sample. For instance, the strLocation node in this sample has an XPATH of: my:myFields/my:strLocation. However, when your form is created from a database connection, the XPATH is now: /dfs:myFields/my:group1/my:strLocation.

It sounds as if you may not have the correct XPATH referenced. If you are using InfoPath 2007, simply right-click on each node that you have referenced in your code and choose Copy XPath. Then paste this into your code and compare.

Scott

# January 22, 2008 5:43 PM

derrickgh said:

Thanks Scott that worked!!

One last question. The forms I need to use this on use a search functionality, so once the form is the person finds their project in a drop-down hits search and this populates the form, which in turn clears the 3 fields used for this solution. Is there a way to run the code after the search event? Thanks again for your help

# January 23, 2008 10:04 AM

infopath said:

Hi derrickgh,

The fields that store the folder name, location, etc. - are these in your database table as well?

Scott

# January 23, 2008 11:40 AM

derrickgh said:

Scott,

Yes, the 3 fields are in DB table.

# January 23, 2008 11:45 AM

infopath said:

Hi derrickgh,

OK - then you can either move the code to populate these to the submit code or (the easier option) remove those fields from the database and simply add them to your datasource. Unless I am missing something from your design needs, these fields are not required to be in the database. You can display the Data Source Task Pane, right-click on the "myFields" node and choose Add. Then simply add these 3 fields. Now these will not get submitted to the database table but that should not be needed.

Scott

# January 23, 2008 11:50 AM

derrickgh said:

I moved the code under the submit button and it works perfectly. Thanks again for your help!

# January 23, 2008 3:55 PM

dkarantonis said:

Hi Scott,

nice article.

I have an InfoPath form created in order to be used as a workflow association form.

I would like to retrieve the list that the workflow is (about to be) associated from the InfoPath form's FormLoad event code.

When the association form is loaded from the web browser (through InfoPath form services), the url is "http://vmserver/development/Company1/_layouts/CstWrkflIP.aspx?List={345EB1D6-434E-4CA9-9138-823CB320AD6C}".

How can i get this List ID?

When using the e.InputParameters["SaveLocation"], i get the following exception "The given key was not present in the dictionary."

How can i get the URL?

regards

# January 24, 2008 5:08 AM

infopath said:

Hi dkarantonis,

In this scenario, the "List" parameter is not an "InputParameter" to the form. If you debug this you will see the InputParameter count is zero. I have been unable to find a way to get this from behind the InfoPath form - so you may want to look at trying to get the URL and parsing it from the ASPX page.

Scott

# January 24, 2008 9:26 AM

dkarantonis said:

Hi Scott

i finally got through it by adding a System.Web reference to my project and retreiving the list GUID from the url by using the HttpContext.Current.Request.QueryString["List"].ToString() command.

I don't know if this is the best way to handle this issue, but it did work for me. If there is a better way, please let me know.

thanks for the support

# January 24, 2008 10:46 AM

dkarantonis said:

Hi Scott,

do you have any idea about how i could programmatically retreive the workflow association name from inside the form's code at runtime (the workflow association name is not known at design time? Is it something i could get also from the aspx page?

# January 31, 2008 4:37 AM

infopath said:

Hi dkarantonis,

Unfortunately I do not - your best bet would probably be to post that query to a workflow forum or you could open a support case with the SharePoint folks.

Scott

# January 31, 2008 8:28 AM

boverton said:

Hello Scott,

quick question.  In reference to your answer

# re: Submitting to 'this' document library

Thursday, July 19, 2007 6:00 PM by infopath

In reference to putting this code behind a button.   I need to be able to run Rules first and then submit.   This works fine but I can't get my document to Close when submitted.   I have close document in the submit options but I don't think they are being called because you're going directly thru the Data connection.   Is there a way to close the form after your submit code?

# March 4, 2008 1:42 PM

infopath said:

Hi boverton,

The easiest way is to simply add a Rule to the same button where you are executing code and the Rule will have just one action: Close the form.

The code should execute to submit the form and then close.

Scott

# March 5, 2008 8:56 AM

mehtavipulk said:

Hi,

I have a InfoPath form with VSTA code embedded to it. In InfoPath development mode, we attach the project file to the .xsn form. So while  publishing the form the .xsn form compiles the project file (in to a .dll file). But how do we manage this situation in the production enviornment? If we try to publish the form (using File -> Publish option of the InfoPath form), using VSTA it will start searching for the project file & try to compile it. But in the production enviornment there wont be Visual Studio installed to map the project file & compile it while publishing the form.

In the production enviornment how do we manage to publish InfoPath form with with embedded VSTA code?

Regards,

Vipul Mehta

# July 2, 2008 1:33 AM

infopath said:

Hi Vipul,

As you know, when you compile the VSTA code behind your InfoPath form template (XSN file), the compiled DLL becomes part of the XSN. You are correct that each time you publish we attempt to compile the code; however, when you publish on a machine that does not have the project files you can simply click "Ignore" and the publish process will continue. (Obviously the other option, since you have InfoPath on that machine is to simply copy the project files to that computer and then when you get that message click the 'Browse' button to locate the project file.)

Scott

# July 2, 2008 7:30 AM

svarukala said:

Scott,

Firstly, thanks for a very useful post. We are implementing around 100 forms in SharePoint and you sample code helps a lot to make the form submission independent of the form library.

However, I am having a problem with updating the existing form. I got your latest code you wrote in your comments which will updated an already existing form. I got it working but it has one glitch. The program is raising 'unauthorized exception' unless I use an account with full rights(owner). The user who created the form, is als not able to update it. I worked around it by calling the data connection submit call within SPSecurity.RunWithElevatedPrivileges block. But here is the problem, the sharepoint library columns 'Created By' and 'Modified By' are populated with 'System Account' or the admin account name instead of the current user name.

So, is there any other work around for this? or Are we missing anything? Is there any explanation for this behaviour(user not able to update)? Thanks for your help.

-Srinivas

# August 12, 2008 8:38 PM

infopath said:

Hi Srinivas,

I had not heard this before so I just tried this on my test site and it seems to work correctly. My "TestUser" has Contribute permissions to my site - I then logged on as that user, created and submitted a new form and then successfully opened that same form, modified data and re-submitted. In this test, the form shows Modified By: TestUser.

What you may want to look at is doing a Netmon or Fiddler trace to see if you can track down down the cause of the failure - obviously remove your workaround code. :)

Offhand I cannot think of a reason why...do you have your form set to Full Trust?

Scott

# August 13, 2008 8:15 AM

irgendwo said:

Hi Scott,

My problem is that by clicking the button in infopath form published on sharepoint site i should open existing form that resides in infopath forms library and one of it's fields should match certain value.How could i do that?Neither i found the opportunity to get the full url of existing form nor the opportunity to request the form with desired field value.

Will be grateful for any help.

Best regards,

Vitaly.

# August 13, 2008 10:19 AM

infopath said:

Hi Vitaly,

I am not sure I completely follow the issue but it sounds like you want to open an InfoPath form based on either command-line or query (URL) parameters...is this correct? If so - have you seen these:

How to: Use Query Parameters to Invoke Browser-Enabled InfoPath Forms

http://msdn2.microsoft.com/en-us/library/ms772417.aspx

Command-line switches for InfoPath

http://office.microsoft.com/en-us/infopath/HP101483281033.aspx

Scott

# August 13, 2008 5:51 PM

svarukala said:

Scott,

Thanks for your reply. I just tried copying your code as it is and it worked just fine. Even a user with just the contribute access is able to add and update a form. I will retest the same in my other problematic environment at my work place. I will let you know the result.

In between, I wonder if a form can be published once and deployed into multipe environments (development, staging, production)? As of now its not possible because we have to give the URL to the sharepoint site to which we publish and so it is hardwired to the form. Technically, I agree that might not be possible because we digitally sign the form for a particular environment. But with a wild card certificate this should not be a problem right?

Thx,

Srinivas

# August 13, 2008 10:27 PM

irgendwo said:

Hi Scott

Thanks for reply.

After looking through these articles I still haven't found the way to open the form with desired value in certain field.It should be made via some CAML query or what?

Thanks in advance

Vitaly

# August 14, 2008 6:07 AM

infopath said:

Srinivas - I am glad to hear the sample works as needed! In regard to publishing once - the answer could be "maybe." <G> Let me explain: If your various sites (dev, prod, etc.) are in the same farm then you could "Admin" deploy the form and then activate it to the appropriate site collections. If your various sits are not under the same farm, you could still perform the "Admin" publish and then simply move that published form where needed and then complete the Admin process of uploading and activating to the appropriate site collection.

Keep in mind, either of these would still require the solution in this post but the process is indeed possible. The other thing you would need to consider though are data connections. Now again, you may be able to cover this with code but you do need to keep it in mind.

Vitaly - Unless I am missing something, what you would need to do is open the 2nd form using a command-line or query parameter and then you would have code in the "Loading" event that looked for that parameter and populated the appropriate field (node) in the form.

Scott

# August 14, 2008 7:47 AM

infopath said:

Vitaly - here is one more item on command-line/query parameters that may help:

http://blogs.msdn.com/infopath/archive/2007/02/26/passing-data-into-a-form-input-parameters.aspx

Scott

# August 14, 2008 7:48 AM

irgendwo said:

Hi Scott

The thing is that the 2nd form is not always the same.It's different every time and depends on the data in 1st form.So i think that this problem could be solved using SPQuery class from SharePoint OM.By doing so we can select what existing form to open by making the query.Could we somehow use this class in InfoPath?We should add some using directive or assembly reference?

Thanks for your help

Vitaly

# August 14, 2008 9:29 AM

infopath said:

Hi Vitaly,

Yes - you can use this in your InfoPath manged code...note that you will need to develop this on your server machine.

You will need to add a Reference to Windows SharePoint Services and then a "using" directive: using Microsoft.SharePoint;

Once you have these in place in your managed code, you can then use the SharePoint OM.

Scott

# August 14, 2008 9:50 AM

svarukala said:

Scott, I got it working today. I still don't know what was the mistake I was doing. But the good the thing is its working now.

Regarding the publishing once, I thought a form has to be published separately for each target site. I will give a try if I can deploy a form which was published to a Dev site into QA site. We have different server farm for different environments. For the data connections I am using UDC's, so, that is taken care of. All the forms are fully trusted, admin published forms.

Srinivas

# August 14, 2008 12:31 PM

irgendwo said:

Hi Scott

Thanks for your help but i've got another question..i've decided to make my template browser-enabled but thus some methods of Application class,such as NewFromFormTemplate disappeared..maybe i'm missing some references or what?i've added references to

C:\Program Files\Microsoft Office\Office12\InfoPathOM\Microsoft.Office.Infopath.dll

C:\Program Files\Microsoft Office\Office12\Microsoft.Office.InfoPath.FormControl.dll and others but still NewFromFormTemplate method and some others are not available.What should i do in order to be capable to use them?

Thanks in advance,

Vitaly

# August 18, 2008 9:11 AM

infopath said:

Hi Vitaly,

As you have seen, everything that is available for designing for the InfoPath client is *not* always available for designing for the browser. This applies not only to object model differences but also controls, etc.

The method you are asking about is an InfoPath client-only method:

http://msdn.microsoft.com/en-us/library/aa942326.aspx

Scott

# August 18, 2008 9:19 AM

infopath said:

Hi Vitaly,

As you have seen, everything that is available for designing for the InfoPath client is *not* always available for designing for the browser. This applies not only to object model differences but also controls, etc.

The method you are asking about is an InfoPath client-only method:

http://msdn.microsoft.com/en-us/library/aa942326.aspx

Scott

# August 18, 2008 9:20 AM

irgendwo said:

Hi Scott

Is there any other way to open template from form?For example knowing the URL displayed when you manually create new form from template located in forms library?

Vitaly

# August 18, 2008 10:12 AM

infopath said:

Hi Vitaly,

If you need to create a new form from the same template, you can set the "Submit" option: Create a new, blank form - this way when your form is submitted, a new blank form will be created. And if you currently do not have a submit connection, you could create one just to "Submit to host" - won't really do anything other than cause a server postback and then create a new form. (It will obviously create a new form so all existing data is gone.)

However, I don't believe it is possible to create a new form from a different template. In fact - as you have seen, programmatically creating a new template from the same one is not supported - nor is the Rule to create a new form (this too is not available for forms services.)

So short of the above submit option, I don't believe there is a way to do this. (Other than maybe using your own ASPX page to host the XmlFormView control and using code behind your ASPX page to launch a new form.)

Scott

# August 18, 2008 1:43 PM

irgendwo said:

Hi Scott

It's a pity there's no simple way to do this..

What can you say about this:i have a browser-enabled form with managed code which simply adds content of one field to another.But each time i'm clicking the button to perform this action i get this message:"An entry has been added to the Windows event log of the server".

Log ID:5337.

My code is just this:

XPathNavigator xNavMain2 = this.MainDataSource.CreateNavigator();

string string1 = xNavMain2.SelectSingleNode("//my:Comments", this.NamespaceManager).Value;

string string2 = xNavMain2.SelectSingleNode("//my:Add_comment", this.NamespaceManager).Value;

xNavMain2.SelectSingleNode("//my:Add_comment", this.NamespaceManager).SetValue("");

xNavMain2.SelectSingleNode("//my:Comments", this.NamespaceManager).SetValue(string1 + "\n" + System.DateTime.Now + " " + string2);

What should i do to get rid of this error?

Vitaly

# August 19, 2008 9:38 AM

infopath said:

Hi Vitaly,

You need to determine what is failing for you. This code works fine in my testing so I would suggest you either see if there is any relevant information in the Application Event log or enable Verbose logging for the various Forms Services categories, reproduce the error and then check the ULS logs on your SharePoint server to see if these point to the cause of the problem.

Have you tried running this in the InfoPath client? Does it fail there as well?

Scott

# August 19, 2008 11:24 AM

irgendwo said:

Hi Scott

Got that problem fixed,thanks.It was security issue i think.After setting full trust mode and creating trusted certificate that code worked.

Scott,what should we do if we want to run some code on client machine?The only available way i see is to save the form in source files and then insert javascript code in,manifest.xsf.Haven't tried it yet,though.Is it really possible?Maybe there are other more friendly ways?

Vitaly

# August 22, 2008 2:56 AM

infopath said:

Hi Vitaly,

As you know with a form opened in the browser, all managed code will execute on the server. I have no idea if modifying the manifest as you have described will work; however, it would not be supported.

As you probably know you can create a custom ASPX page that hosts the XmlFormView control to display your InfoPath forms - there may be some way to get client side script to run from that ASPX page but I am not sure about that.

Scott

# August 22, 2008 10:50 AM

liga said:

hey,

I'm fairly new to form services.. maybe you can shed some light on this.. I'd really appreciate that. The questions are serious :-)

1. save to "this" sounds promising. But somehow "SaveLocation" is an unknown key in my dictionary. Now I happen to know there's a SaveLocation Property for XmlFormView. But setting that property doesn't really help. So how am I supposed to open forms carrying along this parameter? Thinking about that.. if I still have to set the parameter manually there's no gain here, is there?

2. And as you have to publish the form template to the "Form Templates" library I cannot see how saving to "this" library actually makes sense anymore :-/ Don't you typically want to open the form from the forms library and then store filled out forms to another library?

I'm currently looking for a way to open, save and close within a aspx site automagically. For opening purposes I use the XmlFormView control. The saving part is where I'm stuck right now. Among choosing the right library I also need a way to create unique (filled out) form names. Now() doesn't sound not really promising. Any way I can have GUIDs here?

Again, help is much appreciated!

Thanks in advance

# November 4, 2008 9:32 AM

infopath said:

Hi Liga,

Let me try to address each question:

#1: See my response to a question on January 2nd - it has modified code.

#2: First of all, you do not actually "publish" to the Form Templates library. That is a special library that is used when you "Admin" deploy a form template. So the short answer to this is: yes - you want to determine where this form was opened from so you can dynamically determine where to submit the completed form. This is what this sample demonstrates.

#3: Are you using a custom ASPX page for any special reason? Does the built-in FormsServer.aspx page not provide what you need? At any rate - yes, you can use a GUID to create unique names but you would need to do this in code. There are certainly other options as well:

- Use a combination of fields from your form template

- Use code (or a web service) to pull a unique number from a database (i.e. SQL Server)

- Use a web service to generate and return a random number or GUID - this would eliminate the need to do this in code behind your form template

Scott

# November 5, 2008 9:53 AM

liga said:

Thanks for your reply, Scott :-)

@3: Yes. Special reasons being: The form has to be shown in the context of a website. We do not want to open a new window. I already tried assigning a GUID but as it turns out Filename is a read-only property (on FileSubmitConnection). But as I'm still struggling with all the "dynamically save to library x" part I didn't really look into that yet. Any directions you can point me to?

@2: Yes, I understand that. But still you open the form directly from that "special" library don't you? (no matter if you use formsserver.aspx or some custom.aspx site with a xmlformview control)

So let's assume I have 10 different forms in my forms template library. Doesn't each and every one have the same "Request url"?

It would make sense if you had a webpage in a library (let's say "NovemberForms" and within that page open a form from the forms template library and save that to novemberforms library. Much like reading the Request object in asp.

On a side note.. are there forums I can turn to?

# November 5, 2008 1:14 PM

infopath said:

Hi Liga,

The point of the code in this sample is this: let's say you publish a browser-compatible XSN and want to use this in multiple document libraries. With the out of the box functionality, when you setup a "submit" connection to allow the user to "submit" the completed form, it can only submit to the library which you setup at design time. With this sample, it dynamically determines the library from which the form was launched so it submits the completed form to that library.

Now, maybe where the confusion is coming in is this: you are correct that when you "Admin" deploy an XSN, the resulting (uploaded and activated) XSN resides in a special "Form Server Templates" library; however, this is *not* where you want to use this form. What you should be doing is creating other libraries where you want to use that XSN, setting the "Allow management of content types" to Yes for those libraries and then adding that XSN from the Content Types list to that library.

Scott

# November 6, 2008 9:59 AM

liga said:

Great! It all makes sense now :-)

Oh and there's a SaveLocation parameter in the url now, so that's working, too. *happy*

Unfortunately another part of my solution broke apart while switching to the new multi-purpose library.

I can't open the new form within an XmlViewControl (within my webpart) anymore.

I set XsnLocation to the url I get when creating a new form from my library.

"The following file is not a valid InfoPath form template"

http://forms.domain.tld/_layouts/FormServer.aspx?XsnLocation=http://forms.domain.tld/FormServerTemplates/SampleSimpleFormWithCode.xsn&SaveLocation=http://forms.domain.tld/test.

Opening the form directly (without xmlformview control) works perfectly fine.

# November 7, 2008 10:14 AM

infopath said:

Hi Liga,

Glad you are making progress! In regard to the XmlViewControl, have you tried setting the XsnLocation to this:

http://forms.domain.tld/FormServerTemplates/SampleSimpleFormWithCode.xsn

Scott

# November 7, 2008 8:20 PM

liga said:

Hi Scott,

I tried that. As soon as you add any additional parameters to the XsnLocation property the XmlFormView fails to load the form.

If I omit the SaveLocation parameter.. guess what.. KeyNotFoundException :-)

So I guess I'll have to specify the target library prior to deployment rather than figuring it out dynamically :-(

# November 10, 2008 9:13 AM

infopath said:

Hi liga,

I have never used the XmlView control (I am assuming this is a SharePoint web part?) so this may not work as you need.

Scott

# November 10, 2008 9:17 AM
Anonymous comments are disabled
Page view tracker