SharePoint User Group UK

Share the knowledge!

Welcome to SharePoint User Group UK Sign in | Join | Help
in
Home Blogs Forums Photos Files Roller

Submit InfoPath with unique filename using SharePoint Library ID

Last post 08-11-2008, 9:33 AM by Stephen.Hynds. 8 replies.
Sort Posts: Previous Next
  •  03-11-2008, 2:16 PM 8995

    Submit InfoPath with unique filename using SharePoint Library ID

    I have a form which submits using a button control set to submit and automatically generate the filename. However there doens't seem to be an obvious way to create a unique filename.

    Initially i user the now() function to generate the filename (sets the time now as the filename) however each time a form is re-opened it gets submitted as a new form instance rather than just being updated because the time is always different.

    I then changed the filename to use the concat() function (joins field names together to create the name). This is rarely in conflict but does not provide a failsafe unique name and we have uin testing already had users submitting the form with the same name and thus overwriting existing data. I then set the the form to never overwrite existing filenames, but that just throws an error and doesn't allow you to save.

    The most obvious thing to do logically seems to be to somehow take the max ID value in the sharepoint library and add on incrementally, setting a hidden field on the form with this value. I've been trying to set this up using the functions and options available in Infopath and my colleague has also been looking at achieving this from the code side of things.

    Has anyone done this or have any suggestions?

    Many thanks!

    Hannah Scott
    http://bytelab.blogspot.com
  •  03-13-2008, 8:27 PM 9096 in reply to 8995

    Re: Submit InfoPath with unique filename using SharePoint Library ID

    Hi Hannah,

    I've had similar problems and I have too used the xpath now() function to generate a unique file string. This function specifically:

    concat("filename_", substring(now(), 1, 4), substring(now(), 6, 2), substring(now(), 9, 2), substring(now(), 12, 2), substring(now(), 15, 2), substring(now(), 18, 2))

    I'm not 100% but I don't think you will be able to get at the ID value before the item exists in the library.

    Previously I've had to resort to using an event handler in the library which updates a hidden value within the xml - the filename of the infopath form saved. Then when the form is resubmitted the event handler detects this value, deletes the old file and renames the new file back to its original name.

    a bit of a hack but it did the trick!

    regards,

    richard


    Richard J Kennedy
    Trinity Expert Systems
  •  03-15-2008, 9:54 PM 9146 in reply to 8995

    Re: Submit InfoPath with unique filename using SharePoint Library ID

    Evening Hannah,

    Could you not achieve this by either fetching the document library list and get the last ID and incrementing it by 1?

    The way i handle my filenames is to have 2 fields, one a string which will contain the filename which will be generated and the other as a flag (boolean) as to whether or not the filename has been generated?

    Then when the button is clicked it checks the flag to see if it is set to say "1" and if it isn't then sets the filename (me + now() or how ever you wish to generate the filename) and also sets the flag to "1" thus stopping a new file name being generated and keeping the previous filename?

    Hope that can be of some use?
    James Callaghan, Cambridge UK


    James Callaghan

    Cambridge, UK
    MOSS 2007, InfoPath, SP Designer
  •  03-19-2008, 12:33 PM 9236 in reply to 8995

    Re: Submit InfoPath with unique filename using SharePoint Library ID

    Thanks for the suggestions. This is how we solved it in the end -

    We added two hidden fields to the form, one called formName, the other called currentIDMaxPlus1

    When the form loads it calls the loading event FormEvents_Loading (see full code below), which looks at the SP form library items and gets the maximum ID and then adds 1 to it and returns the value to the hidden text box called currentIDMaxPlus1.

    Then we applied two rules to the button as follows:

    1. GreaterThanZero

        Action: Set a field's value: formName = concat(currentIDMaxPlus1, " - ", RequiredFor, " _ ", DrawingType)
        Submit using a dataconnection: Main submit

    2. EqualZero

        Condition:
    currentIDMaxPlus1 = 0
        Submit using a dataconnection: Main submit

    The Main submit takes the values from the hidden field and a couple of others and concats them together.

    Here's the code:

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

    namespace RecordRequest2
    {
        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 Office InfoPath.
            // It can be modified using Microsoft Office InfoPath.
            public void InternalStartup(){
                EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
            }

            public void FormEvents_Loading(object sender, LoadingEventArgs e)
            {
                // Get a reference to the form's XmlNamespaceManager object.
                XmlNamespaceManager ns = this.NamespaceManager;
                XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

                try
                {
                    int currentMax = GetNextID(ns);

                    if (this.New)
                    {
                        xnDoc.SelectSingleNode("/my:Main/my:OfficeUse3/my:currentIDMaxPlus1", ns).SetValue(currentMax.ToString());                   
                    }
                    else
                    {
                        xnDoc.SelectSingleNode("/my:Main/my:OfficeUse3/my:currentIDMaxPlus1", ns).SetValue("0");
                    }
                }
                catch
                {
                    //xnDoc.SelectSingleNode("/my:Main/my:OfficeUse3/my:currentIDMaxPlus1", ns).SetValue(ex.Message);
                }
            }

            private int GetNextID(XmlNamespaceManager ns)
            {
                int currentMax = 0;

                try
                {
                    // Create a DataSource object for EB Record Requests
                    DataSource dsRecordRequests = this.DataSources["EB Record Requests"];

                    XPathNavigator xnRecordRequests = dsRecordRequests.CreateNavigator();

                    XPathNodeIterator xiRecordRequests = xnRecordRequests.Select("/dfs:myFields/dfs:dataFields/dfs:EB_Record_Requests", ns);

                    while (xiRecordRequests.MoveNext())
                    {
                        int currentID = int.Parse(xiRecordRequests.Current.SelectSingleNode("@ID").Value);
                        if (currentID > currentMax)
                        {
                            currentMax = currentID;
                        }
                    }

                    currentMax++;
                }
                catch
                {
                    throw new Exception("Could not get next ID from sharepoint list: GetNextID(XmlNamespaceManager ns)");
                }
                return currentMax;
            }
        }
    }





    Hannah Scott
    http://bytelab.blogspot.com
  •  03-20-2008, 7:20 PM 9273 in reply to 9236

    Re: Submit InfoPath with unique filename using SharePoint Library ID

    I should point out a pitfall of this approach.

    If two people open a new form at the same time they will both get the same id. Ids are only generated when the listitem is created i.e saved to SQL and that doesn't happen until the submit. So the last person to save will overwrite the previous persons data.

    If you need an incrementing id it needs to be a separate table and ideally a sql one so you can use exclusive transactions to allow one person at a time to increment the value.

    just a heads up.

    Colin.

  •  03-25-2008, 4:59 PM 9316 in reply to 9236

    Re: Submit InfoPath with unique filename using SharePoint Library ID

     

    Almost there. I solved a similar issue as follows:

    Created one hidden field: fileName

    Set the Sharepoint data connection in Main submit
    Set the Filename proprety in the Main submit to fileName field


    Then under submitting forms: "Main submit" I added two rules.

    Rule #1: setFileSave
    Condition: fileName field is blank
    Action #1: Set a field's value
             field:  fileName
            
    value:  a fomula using one of my form's fields concatenating the now() function
    Actione #2: Submit using data connection: Main submit

    Rule #2: ExistSaveFile
    Condition: fileName field is not blank
    Actione #1: Submit using data connection: Main submit

    So first time the form is saved, a unique file name is generated, but any subsequente saves use the same name. Also, this uses almost no coding..

     

  •  07-04-2008, 12:17 PM 11947 in reply to 9316

    Re: Submit InfoPath with unique filename using SharePoint Library ID

  •  08-08-2008, 6:54 AM 12838 in reply to 11947

    Re: Submit InfoPath with unique filename using SharePoint Library ID

    I made it in this way (solution without any code):

    1. make three hidden fields
      • lastID (represent last ID number in library)
      • lastFilename (represent last filename in library)
      • currentFilename (represent name of file which I currently fulfil)
      • isCreated (it helps me to check if the form is editing or its new form created)
      • filename
    2. Opening rules:
      • set lastID
        • lastID = max(@ID)
          ID is a field taken from connection to my library where I save my forms. This rule takes the last ID element from library and sets it to lastID
      • set lastFilename
        • lastFilename = @Filename[@ID = lastID]
          @Filename is a field that is equall to name of element in library. I create it during saving form as a accessible field in library. Don't know why but I can't take @Name from library.
          [@ID = lastID]- this filter takes @Filename of last element.
      • set currentFilename (condition: isCreated = "1")
        • currentFilename = @Filename[. = filename]
          This rule works when I edit form. It sets currentFilename to the name of file which I edit.
    3. Submit form rules:
      • set isCreated (condition: isCreated IS NOT "1")
        • isCreated = "1"
          When I fill new form and save it sets this fiels to 1 so when I open it next time it will be edited
      • set filename
        • filename = concat(substring_before(lastFilename; "_") + 1; "_2008)
          Names of my forms in library are: 1_2008, 2_2008, 3_2008, etc. This rule takes number from lastFilename, increase by 1 and concat with _2008 (or whatever you want
      • send
        • send using Sharepoint Library Sending connection
    4.  Sharepoint Library Sending connection: File Name: use field "filename"

    Now, when I fill new form it create me filename which is next number from library, user does't do anything just click "Send".

  •  08-11-2008, 9:33 AM 12880 in reply to 12838

    Re: Submit InfoPath with unique filename using SharePoint Library ID

    I did something reasonably similar with unique file names. Basically the first time the form loads I set a field called filename to the date and time like 080603152057. On subsequent reopening of the form I check to see if it's not then I just leave it the way it was.

    That does use code though, but the form itself is code heavy so it doesn't really matter!

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems