Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List - InfoPath Dev
in

InfoPath Dev

Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List

Last post 11-08-2011 02:20 PM by Horobi. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 02-19-2010 09:58 AM

    Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List

    I used the Data Connections Wizard to create a Webservice to submit from InfoPath to a Sharepoint list using the UpdateListItem service.  I'm using custom_code, triggered from a submit form button, to accomplish this and execute the Webservice.  What I can't figure out is how to programmatically get at the results of that service in order to extract some crucial information I need.  I want to get a hold of the new ID that was assigned for my new list item.  I want to be able to use this ID in order to trigger a secondary Webservice call (addAttachment) to add attachments to the newly created list item.

    Some more details: 

    In my submit form programming, I am creating a WebServiceConnection object that ties back to the Data Connection I created with the wizard in Infopath:

    WebServiceConnection wsSubmit = (WebServiceConnection)this.DataConnections["Web Service Submit"];

    I perform a series of steps to create CAML code and then finish things up by executing the WebServiceConnection:

    wsSubmit.Execute();

    I want to be able to get the new ID that was created for my list item, assign it to field defined in Main which is referenced in a second WebServiceConnection that calls the AddAttachment service which utilizes the listitemID.

    In addition, I just want to be able to access any return results from the web service (e.g. error codes).

     

    Any suggestions on this?

  • 02-26-2010 12:45 PM In reply to

    Re: Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List

    Hi - you might want to try qRules. It has a SubmitToSharePointList command which uses the UpdateListItems web service to submit and/or update list items and return the ID. Works great for repeating data too!

    For more info, please check this blog post: http://www.infopathdev.com/blogs/hilary/archive/2009/11/16/submit-items-to-a-sharepoint-list-with-qrules-v2-0.aspx

    You can try out qRules for free by downloading the tool here.

    Cheers!

    Mel Clemente
    Qdabra® Software/ InfoPathDev.com
    The InfoPath Experts – Streamline data gathering to turn process into knowledge.™

  • 02-26-2010 01:10 PM In reply to

    Re: Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List

    Thanks for the suggestion but I prefer a non 3rd party solution to what appears to be possible without one.

     

  • 06-21-2010 05:44 AM In reply to

    Re: Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List

    Here is a solution to this problem. public static int NextListItemID(SPSite oSite, Guid listId) { int listItemId = -1; Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate() { if (oSite.WebApplication.ContentDatabases.Count > 0) { //Get the connection string for the sharepoint database string connString = oSite.WebApplication.ContentDatabases[0].DatabaseConnectionString; //Establish a connection SqlConnection con = new SqlConnection(connString); try { con.Open(); http://www.mindfiresolutions.com/How-to-programatically-get-the-next-item-ID-for-a-SharePoint-list--884.php
    Cheers,
    Eliza
  • 11-08-2011 02:20 PM In reply to

    • Horobi
    • Not Ranked
    • Joined on 11-08-2011
    • Posts 1

    Re: Getting Webservice UpdateListItem to return the newly created list item ID from a Sharepoint List

    Basically you need to look in the result XML from your UpdateListItems call to find the value of the ows_ID node and use that for the listItemID value in the AddAttachment call:

     

    My main function that is calling the sharepointServices:

    //Add new Row to SharepointList

    result = listService.UpdateListItems(strListID, batchElement);

    //Add Attachment to new row

    KeyValuePair<int, string> listItemId;

    listItemId = CheckListUpdateResults(result);

    listService.AddAttachment(strListID, listItemId.Key.ToString(), "Test", bytes);

     

    A useful function that will return the new rowID from your UpdateListItems call (found here):

     

    //Returns ID of new List Row

    private static KeyValuePair<int, string> CheckListUpdateResults(XmlNode resultsNode)

    {

    if (resultsNode == null) return new KeyValuePair<int, string>(0, "Null results node supplied");

    XmlNamespaceManager ns = new XmlNamespaceManager(resultsNode.OwnerDocument.NameTable);

    ns.AddNamespace("sp", resultsNode.NamespaceURI);

    var errorCode = resultsNode.SelectSingleNode("//sp:ErrorCode", ns).InnerText;

    if (errorCode == "0x00000000")

    {

    // grab the list item ID

    var owsID = resultsNode.SelectSingleNode("//@ows_ID").Value;return new KeyValuePair<int, string>(Convert.ToInt32(owsID), null);

    }

    var errorText = resultsNode.SelectSingleNode("//sp:ErrorText", ns).InnerText;

    var result = new KeyValuePair<int, string>(0, string.Format("Error {0}, {1}",

    errorCode ?? "n/a",

    errorText ?? "n/a"));

    return result;

    }

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