Using thisXDocument with Managed C# code - InfoPath Dev
in

InfoPath Dev

Using thisXDocument with Managed C# code

Last post 06-18-2009 08:09 AM by Razor. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-18-2009 05:50 AM

    • Razor
    • Not Ranked
    • Joined on 02-01-2006
    • Posts 16

    Using thisXDocument with Managed C# code

    Infopath 2007 Visual Studio 2008, .NET 3.5, C# 

    Hi, I'm new on C# and Infopath 2007.

    I'm trying to write code in FormEvents_Loading to check if the current form is new (not previously saved) but I can not use the examples I find on web:

    public partial class FormCode
    {
      private Microsoft.Office.Interop.InfoPath.SemiTrust.XDocument thisXDocument;
      private Microsoft.Office.Interop.InfoPath.SemiTrust.Application thisApplication;


      void FormEvents_Loading(object sender, LoadingEventArgs e)
      {

        if
    (thisXDocument.IsNew == true)
        {
          //Some code...
        }

      }

    }

    When previewing the template there is a NullReferenceException saying "Object reference not set to an instance of an object".

    According to the information i've found on the web there should be an auto generated _Startup event where the thisXDocument variable should be initialized but I do not have such an event at all. If I add such an event like below it's never even called and I get the same exception. There is a Startup event on the other hand but I can't see how I could use it.

    public void _Startup(Application app, XDocument doc)
            {
                thisXDocument = doc;
                thisApplication = app;
            }

    /Lars

  • 06-18-2009 08:05 AM In reply to

    Re: Using thisXDocument with Managed C# code

     Chances are your form is set up to use the InfoPath 2007 object model.  Code that uses thisXDocument is using the InfoPath 2003 object model, which is considerably different.  But even in InfoPath 2007 you can work in the 2003 object model by changing the programming language to "C# (InfoPath 2003 compatible)" in the form options.

     In the 2007 object model, you can check if a document is new by referencing this.New or just New without anything before it.

    Here are a few links that might get you started on the 2007 OM:

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

    http://msdn.microsoft.com/en-us/library/aa945227(VS.80).aspx

     

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 06-18-2009 08:09 AM In reply to

    • Razor
    • Not Ranked
    • Joined on 02-01-2006
    • Posts 16

    Re: Using thisXDocument with Managed C# code

    By God Sir, You are right! And now it works perfectly.

    Thanks for Your much needed advice.

Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.