Update SharePoint List Item if it Already Exists - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Update SharePoint List Item if it Already Exists

Last post 06-18-2010 12:21 AM by elizas. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 11-12-2008 11:49 AM

    Update SharePoint List Item if it Already Exists

    Hi everyone,

    I am new to both InfoPath and SharePoint so I appreciate any help you can give. Through the use of other sources, I have put together a form which will create List Items (really events in a Calendar List) in SharePoint programmatically. Using CAML, the UpdateListItems method and some VBScript, the form creates items just fine.

    The issue is this: I would like to modify my form so that it will overwrite the List Item/Event if the same title is being input. How would I go about doing this? Can the CAML be modified to overwrite if a duplicate is found?

     I appreciate any help you can give. Here is my code thus far:

    CAML Script:
    <?xml version="1.0" encoding="UTF-8" ?>
    <Batch>
    <Method ID="1" Cmd="New">
     <Field Name="Title" />
     <Field Name="Location" />
     <Field Name="Description" />
     <Field Name="EventDate" />
     <Field Name="EndDate" />
     <Field Name="ProjectManager" />
     <Field Name="ProjectSponsor" />
     <Field Name="DIPhase" />
     <Field Name="ProjectName" />
    </Method>
    </Batch>

    The basics of my code:

    Dim batch
    Set batch = XDocument.DataObjects("EventCAML").DOM
    batch.selectSingleNode("/Batch/Method/Field[@Name='Title']").text = ProjectName
    XDocument.DataAdapters("Submit").Submit

     

  • 11-12-2008 08:33 PM In reply to

    • Shiva
    • Top 25 Contributor
    • Joined on 04-15-2005
    • India
    • Posts 694

    Re: Update SharePoint List Item if it Already Exists

    Hello Welcome to the Forums,

    It will update the existing list item using your code. While submitting where you are getting the error? Can you give more detail on error message?

  • 11-13-2008 05:24 AM In reply to

    Re: Update SharePoint List Item if it Already Exists

    I apologize if I was not clear. The code I have currently seems to create new List Items, but I would like it to, if the Title Fields are equivalent, to update the existing List Item instead of creating a new one. What seems to happen if I press the button and the script runs again, with any of the information changed, is that a completely new List Item will be created. There are no errors that seem to crop up, just duplicate creation instead of editing existing items.

     Would I need to query the existing list items to determine if it is a duplicate and then run an update? If so, how would I go about doing that?

    Thanks for your help!

  • 11-13-2008 09:34 PM In reply to

    Re: Update SharePoint List Item if it Already Exists

    Hi,

    Can you verify the below link once.

    http://msdn.microsoft.com/en-us/library/cc162745.aspx#ip2007SubmitDataSPList_AddingaConnectiontotheListsWebServiceandSpecifyingtheParametersoftheUpdateListItemsWebMethod

    In that “Adding a Connection to the Lists Web Service and Specifying the Parameters of the UpdateListItems Method” will tell you how to use UpdateListItems method.

    Hope this helps.

    Rajitha
  • 11-14-2008 07:17 AM In reply to

    Re: Update SharePoint List Item if it Already Exists

    I appreciate your help. Using the above source I developed a solution which, while it may not be extremely efficient, gets the job done.

    I set up another data connection which queries the Event List. That connection is used to filter for the identical title, and returns the row ID to update.

    Using a variable called "EventExists" I keep track of the event existing, and then determine whether to add a new one or update an exisitng one.

  • 11-30-2009 12:40 AM In reply to

    • Daniels
    • Not Ranked
      Male
    • Joined on 07-27-2009
    • Melbourne, Australia
    • Posts 18

    Re: Update SharePoint List Item if it Already Exists

     

    The following article provides a similar solution which uses a CAML template and the UpdateListItems method of the Lists web service to update an existing item in SharePoint.  The article includes some important notes when submitting data to Sharepoint using a CAML query, including the DateTime field, which requires an XML format date string (ISO8601 - "YYYY-MM-DDTHH:MM:SSZ" , eg. "2009-07-27T21:20+01:00", or "2009-07-27T20:20Z" ) for the CAML query to work.  Code used in the form to populate the CAML template is written in JScript and is provided in the article.

    See InfoPath - Update Existing SharePoint List Item Programmatically Using CAML for details.

    Blogs:
    SharePoint Admin & Dev - SharePoint and InfoPath Development

    Web Development & Programming - Php, C#, .NET Framework, SEO Tips

    WebmasterHub.net - Free SEO, Webmaster Resources
  • 06-18-2010 12:21 AM In reply to

    Re: Update SharePoint List Item if it Already Exists

    Provided below are code samples written in C# to format an input as per SharePoint DateTime field. /* -- Format for Date Only Field -- */ private static String ToSPDate(String strDt) { if (strDt == String.Empty) return strDt; else return (Convert.ToDateTime(strDt)).ToString("yyyy-MM-dd"); } /* -- Format for DateTime Field -- */ private static String ToSPDateTime(String strDt) { if (strDt == String.Empty) return strDt; else return (Convert.ToDateTime(strDt)).ToString("yyyy-MM-ddTHH:mm:ssZ"); } http://www.mindfiresolutions.com/Format-Input-for-SharePoint-DateTime-field-186.php
    Cheers,
    Eliza
    Filed under: , ,
Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.