Tags

,

I always be asked to get a Numberic Id for new infopath form. Instead of writing code in Form, I created a Web Service which will go to the SharePoint List, grab the number and increment by 1, then update the list. By doing that, you can set rule in your Form Submit Button without code required.
Below is the code

[WebMethod]
        public int GetAndIncrementCount(string ListUrl, string CountName)
        {
            int count = 0;

            SPWeb oSPWeb = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSPSite = new SPSite(ListUrl))
                    {
                        oSPWeb = oSPSite.OpenWeb(ListUrl.Substring(ListUrl.IndexOf("/") + 1));
                    }
                });
                SPList oSPList = oSPWeb.GetList(ListUrl);

                if (oSPList != null)
                {
                    oSPWeb.AllowUnsafeUpdates = true;
                    SPListItemCollection items = oSPList.Items;
                    if (items.Count > 0)
                    {
                        SPListItem item = items[0];
                        count = int.Parse(item[CountName].ToString()) + 1;
                        item[CountName] = count;
                        item.Update();
                    }
                }
            }
            catch (Exception exception)
            {
                Log.WriteLogEvent(string.Format("An Error Occured In GetAndIncrementCount Method | Exception Message:{0} StackTrace: {1}", exception.Message, exception.StackTrace));
            }
            finally
            {
                oSPWeb.Dispose();
            }
            return count;
        }
Advertisements