Retrieve SharePoint Form ID in code? - InfoPath Dev
in

InfoPath Dev

Retrieve SharePoint Form ID in code?

Last post 02-26-2009 07:15 AM by burtikus. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 02-24-2009 11:14 AM

    Retrieve SharePoint Form ID in code?

    Hi all,

     

    I have an InfoPath form that is being submitted to a SharePoint form library.  After the form has been submitted, I would like to retrieve the ID field from the form library and store it in the form's data source.  I can do this in many different ways, but so far nothing I've tried is working.

     

    I have set up a secondary data connection to the form library and set it to have only one column, namely the ID field.  

     

    So when I try something like:

                    DataConnection SubmitConnection = this.DataConnections["Submit to Form Library"];
                    SubmitConnection.Execute();

                    if(NewStatus == "New") {
                        //query for Form ID, set ChangeRequestNumber to FormID, and resubmit
                        DataConnection QueryConnection = this.DataConnections["ChangeManagementID"];
                        QueryConnection.Execute();
                       
                        string RFCID = xnMyForm.SelectSingleNode("/dfs:myFields/dfs:dataFields/dfs:Request_For_Change/@ID", ns).Value;

                        xnMyForm.SelectSingleNode("/my:ChangeRequest/my:HeaderInfo/my:ChangeRequestNumber", ns)
                            .SetValue(RFCID);

                        SubmitConnection.Execute();
                    }
     

    I get the object reference not set to an instance of an object error.

     

    Alternately, since this particular form will be opened and closed about 10 times during it's life and I just want to get the information there EVENTUALLY, I tried setting the form options -> open rules to set a field to a value, and set my ChangeManagementID field equal to @ID from the secondary datasource.  That does not work either, but I don't get an error either.

     

    Any suggestions?  I have considered trying to hack this on the first submit by trying to calculate max(id) + 1, but that does not seem like a good solution.  I could also maintain my own ID field in the form library and populate with max(id) + 1, but that's far from the best solution either.  What I really want here is the actual sharepoint form library id, but I'm having a hard time finding it.

     

    Thanks so much for any help!

     

    Tim

     

  • 02-24-2009 06:18 PM In reply to

    Re: Retrieve SharePoint Form ID in code?

    Hi, and welcome!

    Offhand, I'd guess your Object ref error would be related to a bad Xpath in your code, or you are not getting results from your query. What line causes the error? Can you put a breakpoint in your code and run it in debug?

    Hilary Stoupa

  • 02-25-2009 06:37 AM In reply to

    Re: Retrieve SharePoint Form ID in code?

     The XPath I just copied from the data source, so I'm assuming that's right. 

     

    The line that causes the error is

         string RFCID = xnMyForm.SelectSingleNode("/dfs:myFields/dfs:dataFields/dfs:Request_For_Change/@ID", ns).Value;

     

    I'm pretty new at InfoPath and SharePoint, but I do basically agree that I'm likely not getting results from the query.  Trying to access the contents of that data source through the UI doesn't work either. I even tried creating a button that I could click on the form to execute the query and then set a field's value to the result.  Nothing.

     The odd thing is that when I setup the secondary data source, it finds the ID field just fine, and when I create a view in Sharepoint to show the ID field it is there.  But I cannot seem to retrieve the field at all.  I'm kinda banging my head on this one as it seems like I must have just done something wrong, but I cannot figure out just what that was.

  • 02-25-2009 04:57 PM In reply to

    Re: Retrieve SharePoint Form ID in code?

    I overlooked your variable 'xnMyForm' -- I bet that is pointed to your Main Data Source, and, of course, the XPath you are using won't be valid against that.... you'd need something along the lines of:

    string RFCID = this.DataSources["MyDataSource"].CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/dfs:Request_For_Change/@ID", NamespaceManager).Value;

    With MyDataSource replaced with the name of your secondary data source.

    If you just drag the main repeating group from your secondary data source onto the form somewhere and preview the form, you get data?

    Hilary Stoupa

  • 02-26-2009 07:15 AM In reply to

    Re: Retrieve SharePoint Form ID in code?

    Hilary Stoupa:

    I overlooked your variable 'xnMyForm' -- I bet that is pointed to your Main Data Source, and, of course, the XPath you are using won't be valid against that.... you'd need something along the lines of:

    string RFCID = this.DataSources["MyDataSource"].CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/dfs:Request_For_Change/@ID", NamespaceManager).Value;

     Well as soon as I read this, it's like a lightbulb went off over my head.  I was just sure you were right about that.  But, it didn't affect the behavior at all. :(

     I still get the error:

    System.NullReferenceException was unhandled by user code
      Message="Object reference not set to an instance of an object."
      Source="RFC_Master"

    Hilary Stoupa:

    If you just drag the main repeating group from your secondary data source onto the form somewhere and preview the form, you get data?

     

    So i tried this, and the answer, surprisingly to me, was no, I did not.  So I went back to modify the query data source, and noticed that there was a check box that said "Include data for the active form only" that was checked.  I unchecked this, just to see what happened, and now all of a sudden I'm retrieving the most recent ID from the library - but it's NOT the one associated with this form, as it exists before this form is submitted to the library. What I'm guessing is going on is that it actually is retrieving all of the form ids, and select single node just displays the one?  But I could be way wrong about that too.

    This does, sort of solve my problem though, I think.  Now if in my initial submit code I query that data connection after the form is submitted the first time, the field does contain the latest form id and I can populate my data source and resubmit.  But I don't think it's working in the sense I want it to, as I could see at least a theoretically possible situation where two forms get submitted at exactly the same moment... but maybe that's just picking nits.

     Thank you so much for your help, Hilary.  This has been driving me NUTS, and I don't know that I ever would have figured out that different navigator thing.

     

    Tim

     

     

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