Create a Unique Project ID and automated folder structure under document library using Event Receiver in SharePoint 2013/2016/2019

event receivers in sharepoint 2013 examples

This SharePoint tutorial explains, how to create unique ID and folder structure automatically using Event receiver in SharePoint 2013/2016/2019.

Create a Unique Project ID and automated folder structure under document library using Event Receiver in SharePoint

Below are the steps to create an event receiver in Visual Studio 2017/2019. Before we start, we have to create two things here.

SharePoint deveopment training course

1: Create a Project master List

event receivers in sharepoint
event receivers in sharepoint

2: Create a Deliverable Library

event receivers in sharepoint 2013
event receivers in sharepoint 2013

Step 1: Open Visual Studio – > Go to solution explorer -> Create a New Items -> Choose Event Receiver -> Click on Add button

event receivers in sharepoint 2013 examples
event receivers in sharepoint 2013 examples

Step 2: Next page will appear to choose Event Receiver Settings . So here you can select List Item Events and custom list in the event source. Next you can choose An item was added as per below screenshot.

event receivers in sharepoint 2013 for custom list example
event receivers in sharepoint 2013 for custom list example

Step 3: Next you can follow the below code to use in your visual studio snip to achieve your goal.

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

namespace SharePoint_Webparts.Create_Folder
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class Create_Folder : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            string Siteurl = properties.ListItem.Web.Url;
            using (SPSite site = new SPSite(Siteurl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPListItem currentItem = properties.ListItem;
                    currentItem["Project_ID"] = "KM-ID00" + Convert.ToInt32(properties.ListItem["ID"]).ToString();
                    currentItem.Update();
                    SPList spl = web.Lists["Deliverable Library"];
                    var FolderAdded = spl.Items.Add("", SPFileSystemObjectType.Folder, "KM-ID00" + Convert.ToInt32(properties.ListItem["ID"]).ToString());
                    FolderAdded.Update();
                    string[] namesArray = new string[] { "Analysis", "Design", "Development", "Testing", "Deployment" };
                    foreach (string name in namesArray)
                    {
                        SPListItem SubFolder = spl.AddItem(FolderAdded.Folder.ServerRelativeUrl, SPFileSystemObjectType.Folder, name);
                        SubFolder.Update();
                    }
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
    }
}

Step 4: in Element.xml – you need to modify the Receivers List URl instead of List template ID . You have to update your list URL here.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="/sites/KMIntranet/Lists/Project%20Master">
      <Receiver>
        <Name>Create_FolderItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>SharePoint_Webparts.Create_Folder.Create_Folder</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>

Step 5: Once your code get ready, right click on solution explorer and Deploy the solution.

event receivers in sharepoint 2013 examples
event receivers in sharepoint 2013 examples

Step 6: Next go back to your list and add an Item in the list and see what happen. Here Project_ID will generate automatically after you save your data. Please look into the below screenshot which is shown in the Project _ID column.

Create a Unique Project ID event receivers in sharepoint
Create a Unique Project ID event receivers in sharepoint

Step 7: Same time if you will open your document library, the folder is created as same name.

automated folder structure under document library using Event Receiver in SharePoint
automated folder structure under document library using Event Receiver in SharePoint

Step 8: If you open the folder, all folder structure has created automatically here. Please look into the screenshot.

event receivers in sharepoint 2013 examples
event receivers in sharepoint 2013 examples

You may like following event receiver tutorials:

This is all about creating folder structure automatically using SharePoint Event receiver.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in QATAR . Rajkiran having 6 + years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

View all posts by Rajkiran Swain →