Submitting an InfoPath Form to SharePoint Form Library Using Code Behind.

I am trying to submit an InfoPath 2010 form using code behind (VSTA 2010). My Form only include 1 Text Box (TextValue) & one button that labeled as "Submit" that will trigger the form submission code.The form name as "SubmitByCode".

The Code is at the below.

using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;
using System.Data;

namespace SubmitByCode
{
    public partial class FormCode
    {
        // Member variables are not supported in browser-enabled forms.
        // Instead, write and read these values from the FormState
        // dictionary using code such as the following:
        //
        // private object _memberVariable
        // {
        //     get
        //     {
        //         return FormState["_memberVariable"];
        //     }
        //     set
        //     {
        //         FormState["_memberVariable"] = value;
        //     }
        // }

        // NOTE: The following procedure is required by Microsoft InfoPath.
        // It can be modified using Microsoft InfoPath.
        public void InternalStartup()
        {
            ((ButtonEvent)EventManager.ControlEvents["Submit"]).Clicked += new ClickedEventHandler(Submit_Clicked);
        }

        public void Submit_Clicked(object sender, ClickedEventArgs e)
        {
            //create the navigator 
            XPathNavigator nav = MainDataSource.CreateNavigator();

            int nextID = GetMaxID() + 1;
            //set the form name
            string formName = "MyForm" + DateTime.Now.Year.ToString() + " - " + nextID.ToString();
            //set the field to the form name
            nav.SelectSingleNode("/my:myFields/my:TextValue", NamespaceManager).SetValue(formName);
            //reference the submit connection
            //DataConnection spConn = DataConnection["SharePoint Library Submit"];
            
DataConnection SPConnection = (DataConnection)this.DataConnections["SharePoint Library Submit"];
            //submit the form
            SPConnection.Execute();

        }

        public int GetMaxID()
        {
            SPContext ctx = SPContext.Current;
            using (SPSite site = new SPSite(ctx.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList oList = web.Lists["Order Attachments"];
                        SPQuery query = new SPQuery();

                        query.Query = "";
                        SPListItemCollection items = oList.GetItems(query);

                        DataTable dtMax = new DataTable();
                        dtMax = items.GetDataTable();

                        return (dtMax == null) ? 0 : Convert.ToInt32(dtMax.Compute("Max(ID)", string.Empty));
                    }
                    finally
                    {
                        if (site != null)
                            site.Dispose();
                    }

                }
            }

        }
    }
}

But when I try to submit the form I got an error as "'Microsoft.Office.InfoPath.DataConnection' is a 'type' but is used like a 'variable'" at the

DataConnection SPConnection = DataConnection("SharePoint Library Submit");

statement. I think my references are OK but I cannot understand what this message is saying. Please can someone try to solve this error.

Regards,

Chiranthaka



January 13th, 2014 12:28am

I've solve it by my self & the modified snippet is as the below.

DataConnection SPConnection = (DataConnection)this.DataConnections["SharePoint Library Submit"];

But now I have another error at the following statement.

public int GetMaxID()
        {
            SPContext ctx = SPContext.Current;
            using (SPSite site = new SPSite(ctx.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList oList = web.Lists["Order Attachments"];
                        SPQuery query = new SPQuery();

                        query.Query = "";
                        SPListItemCollection items = oList.GetItems(query);

                        DataTable dtMax = new DataTable();
                        dtMax = items.GetDataTable();

                        return (dtMax == null) ? 0 : Convert.ToInt32(dtMax.Compute("Max(ID)", string.Empty));
                    }
                    finally
                    {
                        if (site != null)
                            site.Dispose();
                    }

                }
            }

        }
using (SPSite site = new SPSite(ctx.Site.Url))

The error is as the below.

'Object reference not set to an instance of an object'.

Troubleshooting Tips

Use the 'new' keyword to create an object instance.

Check to determine if the object is null before calling the method.

Get general help for this exception.

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

I cannot solve this error by my self. Could someone try to solve this matter?

Thank You

Regards,

Chiranthaka


Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 1:59am

I had a very quick view of your code and here are my observations -

If you are using SPContext ctx = SPContext.Current; then it is not a best practice to dispose it with using.

Also your finally is called in wrong block, you don't require finally because of using.

you need not create new site object, instead use the following code -

Note you can reference current site(SPWeb) using SPContext.Current.Web, and then you don't even require to get the reference of SPSite-


SPSite site = SPContext.Current.Site; SPWeb web = SPContext.Current.Web;

{

or use the below code

 using (SPSite site = new SPSite("URL"))
{
  using (SPWeb web = site.OpenWeb())
  {

}

}


Check whether your DataTable is returning any value, check the return value of ID

Hope this

January 13th, 2014 2:55am

Hello,

Where you running your code? Make sure that you are opening form in sharepoint environment and also check whether context is null or not using below code:

// Get the current SharePoint context
SPContext ctx = SPContext.Current;

if (ctx != null)
{
  // Retrieve the URL of the SharePoint site collection
  SPSite site = ctx.Site.Url;
}

Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 3:56am

Ok pal that problem was solved & the form submission to the library was also a successful one. But when I tried to open the submitted form from the form library it tried to download instead of opening in the web browser. But in my form library settings I set the 'Allow Management Of Content Types' to 'YES' & the 'Default Open Behavior For Browser Enabled Documents' to 'Open In The Browser'.

Also my forms are submitting as .xml files. There is another issue with my form. When I tried to upload the form template to SharePoint Central Admin I got an error as  the below.

" There was a failure loading System.Data.dll. Details: Could not load file or assembly 'file:///C:\Users\Administrator\AppData\Local\Temp\tmp335F.tmp\System.Data.dll' "

I cannot understand what I have done wrong. Please could you try to solve this matter also?

Regards,

Chiranthaka

January 13th, 2014 6:05am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics