0

I am having some issues with creating an auto incrementing column. The requirements are as follows;

The column needs to editable If the column contains data then do nothing Each time an item is added then get the highest value in a column named "EnquiryNumber" and make the new item 1 higher

I have created an event receiver that works off an item being created and I have added the code below that I can not take credit for. But the issues that I am having is that when I create items it always seems to go off one number, I have added a screen shot below to depict this.

Any help is much appreciated.

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Diagnostics;

namespace Technical_Enquiries_Register.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SPWeb web = properties.OpenWeb();
            bool allowUpdates = web.AllowUnsafeUpdates; //store original value
            this.EventFiringEnabled = false;

            try
            {
                web.AllowUnsafeUpdates = true;
                /*get the currentQ list*/
                SPList list = web.Lists[properties.ListId];
                var highestvalue = 0;
                var objQuery = new SPQuery
                {
                    Query = "<OrderBy><FieldRef Name='" + "EnquiryNumber"
                    + "' Ascending='True' /></OrderBy><RowLimit>1</RowLimit>",
                    Folder = list.RootFolder
                };

                SPListItemCollection colItems = list.GetItems(objQuery);
                if (colItems.Count > 0)
                {
                    highestvalue = int.Parse(colItems[0]["EnquiryNumber"].ToString());
                }

                var currItem = properties.ListItem;
                currItem["EnquiryNumber"] = highestvalue + 1;
                currItem.SystemUpdate(false);

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            finally
            {
                this.EventFiringEnabled = true;
                web.AllowUnsafeUpdates = allowUpdates; //restore original value
            }
        }
    }
}

Broken

1

To get the last record of the list you have to take Ascending='False' in CAML Query.

<OrderBy>
    <FieldRef Name='EnquiryNumber' Ascending='False' />
</OrderBy>
<RowLimit>1</RowLimit>
  • Unfortunately this did not work, I changed it to { Query = "<OrderBy><FieldRef Name=EnquiryNumber Ascending='False'/></OrderBy><RowLimit>1</RowLimit>", Folder = list.RootFolder }; But I get the same result – Daniel Jun 22 '16 at 4:44
  • Any further help would be appreciated – Daniel Jun 23 '16 at 0:55
0

There is a default ID column which get auto incremented every time you add new item in list. You can use that. But that column in non editable. If you want to keep column editable you can write below code and it doesn't need CAML query

    using System;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.Workflow;
    using System.Diagnostics;

    namespace Technical_Enquiries_Register.EventReceiver1
    {
        /// <summary>
        /// List Item Events
        /// </summary>
        public class EventReceiver1 : SPItemEventReceiver
        {
            /// <summary>
            /// An item was added.
            /// </summary>
            public override void ItemAdded(SPItemEventProperties properties)
            {
                SPWeb web = properties.OpenWeb();
                bool allowUpdates = web.AllowUnsafeUpdates; //store original value
                this.EventFiringEnabled = false;

                try
                {
                    web.AllowUnsafeUpdates = true;
                    /*get the currentQ list*/
                    SPList list = web.Lists[properties.ListId];                

                    var currItem = properties.ListItem;
                    currItem["EnquiryNumber"] = currItem["ID"].ToString();
                    currItem.SystemUpdate(false);

                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
                finally
                {
                    this.EventFiringEnabled = true;
                    web.AllowUnsafeUpdates = allowUpdates; //restore original value
                }
            }


  }
}
  • I would like to avoid using the ID column too as we are migrating this from an excel spredsheet which already has 4500+ entries and they wish to maintain that number. your help is appreciated though – Daniel Jun 22 '16 at 5:04
  • Any further help would be appreciated – Daniel Jun 23 '16 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.