XPath query to check if a field would be a duplicate? - InfoPath Dev
in

InfoPath Dev

XPath query to check if a field would be a duplicate?

Last post 05-27-2009 09:59 AM by burtikus. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 05-20-2009 12:13 PM

    XPath query to check if a field would be a duplicate?

    Hi all.  I'm having a bit of trouble figuring out how to accomplish something. 

    Basically, I have an InfoPath 2007 form being submitted to a MOSS 2007 Form Library.  I originally set this up to use a Title field that's on the form itself as the document title when the form is submitted.  This "seemed like a good idea at the time" (TM). 

     Now, though, I realize that if someone ever names their form the exact same thing as any other form, it will overwrite the old one, not make a new on with the same title.  This strikes me as "Not a Good Idea" and so I am trying to fix it.

    My thought is that I would create a second connection to the SharePoint form library to receive data, and set it to retrieve only the titles of all the documents.  Then in the updated event of my title field, check whether there is a duplicate title name and raise a validation error if that's the case.  I'm having some difficulty in figuring out how to handle the xpath stuff to make this happen, as I really don't know xml or infopath all that well.

    Here's my code:

     

    public void Title_Changed(object sender, XmlEventArgs e)
    {
    // Write your code here to change the main data source.

    XPathNavigator xnMyForm = this.CreateNavigator();
    XmlNamespaceManager ns = this.NamespaceManager;

    string sNewTitle = xnMyForm.SelectSingleNode("/my:ChangeRequest/my:HeaderInfo/my:Title", ns).Value;

    DataConnection TitleConnection = this.DataConnections["RFC_Titles"];
    TitleConnection.Execute();


    string sExistingTitle = xnMyForm.SelectSingleNode("/dfs:myFields/dfs:dataFields/dfs:Request_For_Change[@Title = '" + sNewTitle + "']/@Title", NamespaceManager);


    // if there WAS a result returned, raise an error
    -->Add if statement here to check this
    this.Errors.Add(TitleField, "TitleError", "The RFC Titlte Must be Unique.");

    // else, if there was not, make sure there is no error
    -->figure out how to do this right also
    try
    {
    this.Errors.Delete("TitleError");
    }
    catch { }
    }

    Can anyone offer any suggestions?  Am I on the right track?  Is there a much much easier way to do this that I'm not thinking of?

    Thanks!!!

    Filed under: ,
  • 05-20-2009 03:00 PM In reply to

    Re: XPath query to check if a field would be a duplicate?

    I think you want:

    string sExistingTitle = xnMyForm.SelectSingleNode("/dfs:myFields/dfs:dataFields/dfs:Request_For_Change/@Title[. = '" + sNewTitle + "']", NamespaceManager);

     

    Hilary Stoupa

  • 05-22-2009 01:59 PM In reply to

    Re: XPath query to check if a field would be a duplicate?

    That expression gets me most of the way there.  Thanks Hilary!!

    One question though.  This code now works for preventing duplicates against the most recent 100 forms in the library.  Further back than that though, it does not find any results to the xpath query.  I know the SharePoint user interface displays the library in pages of 100 forms - but accessing it through a query like this?  I gotta say, this puzzles me.  Is there a workaround?

    Tim

     

  • 05-27-2009 09:59 AM In reply to

    Re: XPath query to check if a field would be a duplicate?

     Actually, this issue turned out to be related to the default view of the library in SharePoint.  Updating the default view to show all the items in the library fixed the issue.  I will experiment with basing my data source off a different view, so that the default view can still be paged, but for now my issue is resolved.  Thanks Hilary for your help!

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