Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

Posts Tagged ‘InfoPath’

Get Count from SharePoint List for InfoPath

Posted by ken zheng on April 3, 2009

I always be asked to get a Numberic Id for new infopath form. Instead of writing code in Form, I created a Web Service which will go to the SharePoint List, grab the number and increment by 1, then update the list. By doing that, you can set rule in your Form Submit Button without code required.
Below is the code

[WebMethod]
        public int GetAndIncrementCount(string ListUrl, string CountName)
        {
            int count = 0;

            SPWeb oSPWeb = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSPSite = new SPSite(ListUrl))
                    {
                        oSPWeb = oSPSite.OpenWeb(ListUrl.Substring(ListUrl.IndexOf("/") + 1));
                    }
                });
                SPList oSPList = oSPWeb.GetList(ListUrl);

                if (oSPList != null)
                {
                    oSPWeb.AllowUnsafeUpdates = true;
                    SPListItemCollection items = oSPList.Items;
                    if (items.Count > 0)
                    {
                        SPListItem item = items[0];
                        count = int.Parse(item[CountName].ToString()) + 1;
                        item[CountName] = count;
                        item.Update();
                    }
                }
            }
            catch (Exception exception)
            {
                Log.WriteLogEvent(string.Format("An Error Occured In GetAndIncrementCount Method | Exception Message:{0} StackTrace: {1}", exception.Message, exception.StackTrace));
            }
            finally
            {
                oSPWeb.Dispose();
            }
            return count;
        }

Posted in Sharepoint | Tagged: , | Leave a Comment »

Close InfoPath Forms Services form using Code

Posted by ken zheng on February 24, 2009

I was working on a way to close an InfoPath form using C# or VB.NET from the form code-behind after the user submits the form. The steps below help in running some custom code to save the data and then close the form.

Here are the steps to close the Form

* Set the Submit Action[Tools->Submit Options] within InfoPath for the form to “Perform custom action using Code” . Click the Edit Code option to submit the form using code
* Set the After Submit option under Advanced to “Close the Form”

Click the Edit Code button next to the perform custom action to submit the data. Editing the code adds an handler for the Form Submit event and a FormEvents_Submit method is created in the code behind. Once the code completes the submission, set the CancelableArgs.Cancel property of the SubmitEventArgs to false.

Here is a sample FormEvents Submit method

In the SubmitConnection method, I retrieve the required data connection and call the execute method for submission

Dim fileSubmit as FileSubmitConnection = CType(Me.Connections(”SharePoint Save”),Microsoft.Office.InfoPath.FileSubmitConnection)

fileSubmit.Execute()

In the above sample, the data connection that I want to use for Submit is named “SharePoint Save”. I typecast it as a FileSubmitConnection.

I found this solution on one of the forum posts

http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=2605818&SiteID=17

Posted in Sharepoint | Tagged: | 1 Comment »

Infopath error: “The required version of the Microsoft .NET Framework is not installed on your computer or the InfoPath Primary Interop Assembly (PIA) is not registered”

Posted by ken zheng on February 6, 2009

Issue

User get’s a listed below error when opening a new or existing InfoPath form because of an error in the form’s code

Error
“The required version of the Microsoft .NET Framework is not installed on your computer or the InfoPath Primary Interop Assembly (PIA) is not registered. Use Add or Remove Programs in Control Panel to make sure that the required version of the Microsoft .NET Framework is installed. Or install it using Windows Update and run the Setup program again to confirm that the corresponding version of the .NET Programmability Support is installed, or contact your system administrator.”
Cause\Resolution

As the error message suggests either the Microsoft .NET Framework or the InfoPath Primary Interop Assembly (PIA) is not installed. If Microsoft .NET framework is not present on the system then this will need to be installed. If the user does not have the .NET Framework already installed (when installing Office 2007) then PIAs will not be installed. Additionally, the option to install the PIAs doesn’t show up in the Custom setup for Office. If the user does a complete install of Office 2007, then PIAs will get installed into the GAC automatically. It’s strongly recommended that a complete install of Office 2007 be performed.

If .Net Framework is installed after installing Office 2007, then PIA does not get installed automatically and this can to be installed by following the steps listed below

1. Go to ‘Add or Remove Programs…’
2. Select the Microsoft Office 2007 installation and click on change.
3. Select “Add or remove features” and click Continue.
4. Expand the “Microsoft Office InfoPath” and “.NET Programmability Support” node. Add the ‘.NET programmability support’. To add, right click on the node and select “Run from my computer”. Press continue and follow the on screen instructions to complete the setup.

Posted in Sharepoint | Tagged: | Leave a Comment »

Clear InfoPath Form Cache in Local Computer

Posted by ken zheng on January 12, 2009

When loading an InfoPath form from an intranet/Sharepoint etc, the form template is downloaded and cached locally. Sometimes after the template has been upgraded on the server the local client doesn’t always get the upgraded form template but uses the local cached copy instead.

You can rectify this by clearing the local cache from Start -> Run;

Infopath /cache clearall
or VB Script

Dim wshShell
Dim sRun, sParam, sMsg

sParam = “C:\Program Files\Microsoft Office\OFFICE11\INFOPATH.EXE /cache clearall”
Set wshShell = Createobject(“WScript.Shell”)

wshShell.Exec sParam

Set wshShell = Nothing

Verify that the cache has been cleared by navigating to the following folder for XP+

C:\Documents and Settings\[User]\Local Settings\Application Data\Microsoft\InfoPath\FormCache2

Or for Vista;

C:\Users\[User]\AppData\Local\Microsoft\InfoPath\FormCache2

Underneath the FormCache2 folder there will be a folder with a GUID name, check inside here, it should be empty, apart from a file called _SCVer.dat.

Posted in Sharepoint | Tagged: | Leave a Comment »

Replace Text in InfoPath XML file

Posted by ken zheng on December 12, 2008

I have an interesting problem when trying to update an InfoPath Form Data. If I use XMLDOCUMNET and node to update the xml file. I got problem to load the data. So I have to use

StreamReader sr = new StreamReader(filename);
String file = sr.ReadToEnd();
sr.Close();
file = file.Replace(oldtext, newtext);
StreamWriter sw = new StreamWriter(filename);
sw.Write(file);
// Close StreamWriter
sw.Close();

Posted in Uncategorized | Tagged: | Leave a Comment »