Generating unique random string in SharePoint document library

sharepoint

I'm working on customizing a SharePoint document library called "Quality Documents" so that when new docs are added to the library, a random and unique number is generated and applied to a field named "Document Number". I coded the feature below, but it's not working. Can anyone see what might be the problem? Nothing happens, no errors nothing, the page just works fine, but no Document Number gets generated. Any suggestions?

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace QualityDocHandler
{
    class DocumentHandler : SPItemEventReceiver
    {
    /// <summary>
    /// Generates a random string with the given length
    /// </summary>
    /// <param name="size">Size of the string</param>
    /// <returns>Random string</returns>

    private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        return builder.ToString();
    }

    private string createDocNum(SPItemEventProperties properties)
    {
        int newRnd = 0;

        do
        {
            // set static department
            string dept = "QUA";

            // set date without separators
            string dateString = DateTime.Today.ToString("ddMMyyyy");

            // get 1st random string 
            string Rand1 = RandomString(4);

            // get 1st random string 
            string Rand2 = RandomString(4);

            // creat full document number
            string docNum = dept + "-" + dateString + "-" + Rand1 + "-" + Rand2;

            using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
            {
                SPList oList = oWeb.Lists["Quality Documents"];

                //create query
                SPQuery oQuery = new SPQuery();

                //configure the query  //
                oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";

                //get the collection of items in the list
                SPListItemCollection oItems = oList.GetItems(oQuery);

                if (oItems.Count > 0)
                {
                    newRnd = 0;
                }
                else
                {
                    newRnd = 1;
                }
            }
            return docNum;
        }
        while (newRnd < 1);

    }

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
    }

    public override void ItemAdding(SPItemEventProperties properties)
    {

        string documentNum = createDocNum(properties);
        using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
        {
            SPListItem listItem = properties.ListItem;
            properties.AfterProperties["Document_x0020_Number"] = documentNum;
            listItem.Update();
            oWeb.Update();
        }
        base.ItemAdding(properties);

    }

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);
    }

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);
    }

}

}

Best Solution

A few things:

  • You don't need to get a reference to listItem and use listItem.Update(). Just setting the AfterProperties should be enough.

  • Prevent the same event from firing multiple times by wrapping your ItemAdding method code with:

this.DisableEventFiring();
try
{
    // ...
}
finally
{
    this.EnableEventFiring();
}
  • Run SPDisposeCheck over your code. You might have a memory leak on the SPSite object with new SPSite().OpenWeb().

  • Have a read of Workarounds for ItemAdding/ItemAdded Event Handlers. I've never had to do this but using the display name instead of internal name may fix the problem.

  • In case of desperation, use ItemAdded() instead. Get a full reference to the original item and update that.

Related Question