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.
1: Create a Project master List
2: Create a Deliverable Library
Step 1: Open Visual Studio – > Go to solution explorer -> Create a New Items -> Choose Event Receiver -> Click on Add button
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.
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.
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.
Step 7: Same time if you will open your document library, the folder is created as same name.
Step 8: If you open the folder, all folder structure has created automatically here. Please look into the screenshot.
You may like following event receiver tutorials:
- Remote Event Receivers in SharePoint online + Develop in Provider hosted Add-in using Visual Studio 2015 and Deploy to Microsoft Azure
- Difference between workflows and event receivers in SharePoint 2013
- SharePoint 2016/2013 event receiver example using Visual Studio 2017
- SharePoint Development Training
- Create remote event receiver SharePoint Online Step by Step Tutorial
- BeforeProperties and AfterProperties in Event Receiver in SharePoint 2013
- Different ways to create auto increment column in SharePoint 2013/2016/Online list
This is all about creating folder structure automatically using SharePoint Event receiver.
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ 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).