MOSS: Updating a custom list

There are many good examples of updating custom lists via the SDK. Here is yet another.

Business problem: InfoPath form has been designed that enables users to enter online purchase requisitions. PO Requisition numbers should be traditional sequence based integer values and calculated automatically.

Business Solution: Create a custom MOSS list containing two columns: "ControlField" and "ControlValue". The value column contains the next purchase requisition number. Note that the generic "control" naming convention provides for future control fields that may be used as needed.

Technical Solution: Create a web service accessed by the InfoPath client. The web service returns back the next purchase requisition number and updates the value of the list.

Lessons Learned:

  • When adding this web service as a data source to the InfoPath form, I found it necessary to convert it to a udc and store it into a data connection library.
  • I also found it necessary to enable cross domain scripting via central services administration // application management // form server configuration.
  • The first time the form tried to access the web service, it takes a while and on occasion, it would time out. I fiddled with settings in form server configuration to expand the timeout settings and that seemed to help.

The code:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;
using System.Configuration;

[WebService(Namespace = "http://www.conchango.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PoService : System.Web.Services.WebService
{
    public PoService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /// <summary>
    /// Obtain the next PO number from the sharepoint po number control list.
    /// Increment the PO number in that list.
    /// </summary>
    /// <returns></returns>
    [WebMethod]
    public string GetNextPoNumber()
    {
        string SpPoControlSiteName; // Name of the actual MOSS site that hosts the PO Control list.
        string SpPoControlListName; // Name of the actual MOSS list containing the Po control.

        SpPoControlSiteName = ConfigurationSettings.AppSettings["PoControlListHostingSite"].ToString();
        SpPoControlListName = ConfigurationSettings.AppSettings["PoControlList"].ToString();

        string nextPoReqNumber = "xyzzy";

        using (SPSite site = new SPSite(SpPoControlSiteName))
        {
            using (SPWeb web = site.OpenWeb())
            {

                SPList currentList = web.Lists[SpPoControlListName];

                foreach (SPItem controlItem in currentList.Items)
                {

                    if (((string)controlItem["ControlField"]).Equals("NextPoNumber"))
                    {
                        nextPoReqNumber = (string)controlItem["ControlValue"];

                        int int_nextPoReqNumber;
                        int_nextPoReqNumber = Convert.ToInt32(nextPoReqNumber);

                        int_nextPoReqNumber++;

                        controlItem["ControlValue"] = int_nextPoReqNumber;
                        controlItem.Update();
                    }

                } // Locating, reading and updating the PO number in the list.

                
            } // using spweb web = site.openweb()
        } // using spsite site = new spsite("http://localhost/mizuho")

        return nextPoReqNumber;

    }
}

One thought on “MOSS: Updating a custom list

  1. KSP wrote:
    Hi Paul,
     
    We have a similar situation what you are mentioning but the problem is i want to use this web servie on a load balance server.Hence,how to handle concurrency requests in this case or how to make the new request wait and only one request should update the list number till then all other requests should be in que.Please let me know how to achieve this.
     
    Thanks in Advance.
    Reply

Leave a Reply to KSP wrote: Cancel reply

Your email address will not be published. Required fields are marked *