My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and index you

Creating folders in custom list and insert items inside the folders

Objective:
In this article, I am going to create folder inside a SharePoint list using the SharePoint object model or in other words using .Net code.

Requirement:
Take an example of the requirement: We need to insert the log entry of every step of execution for a particular batch job in the SharePoint list on daily basis. If we start inserting an item for every step of execution in the root level, it will bring down the performance of data retrieval and also identification of log entry for a particular date would not be easy task. According to Microsoft, for optimum performance insert only 2,000 items in the root level and create nesting folders to improve performance.

Resolution:
To improve the performance and easy identification of day to day log entry, we will create folder structure in the custom list to store log entry. The screen shot below shows the folder structure inside the list.




The below screen shot shows the columns in the custom list. In this list we have created two custom columns:
* Comments
* TimeofExecution




Source Code:


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;


namespace CreateFolderInlist
{

/// <summary>
/// This class provide the function to
/// create folder in the custom list
/// </summary>
/// <createdby>Amit Kumar</createdby>
public static class CommonFunctions
{
/// <summary>
/// Create folder hierarchy in the list
/// like "2010 > July > Week_2 > 09July2010"
/// </summary>
/// <returns></returns>
public static void InsertLogInList(SPWeb web, SPList list,
string timeofExecution, string comments)
{
SPListItem item = null;
try
{

item = CreateFolder(web, list);
InsertLogInList(list, item, timeofExecution, comments);
}
catch (Exception ex)
{

throw;
}
}

/// <summary>
/// Create folder hierarchy in the list
/// like "2010 > July > Week_2 > 09July2010"
/// </summary>
/// <param name="web">SPWeb object</param>
/// <param name="list">SPList object</param>
/// <returns></returns>
public static SPListItem CreateFolder(SPWeb web, SPList list)
{
SPListItem item = null;
try
{
#region Create Year folder in the list
//Year folder
string currYear = System.DateTime.Now.Year.ToString();
SPListItem yearFolder = null;
if (!FolderExists(list.RootFolder.ServerRelativeUrl + "/" +
currYear, web))
{
//If not exist then create new folder 2010
yearFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl,
SPFileSystemObjectType.Folder, currYear);
yearFolder.Update();
}
else
{
//If exist then take the ref. of folder
yearFolder = list.Folders[web.GetFolder(
list.RootFolder.ServerRelativeUrl + "/" +
currYear).Item.UniqueId];
}
#endregion

#region Create month folder in the year folder
//Month folder
string currMonth = System.DateTime.Now.ToString("MMMM");
SPListItem monthFolder = null;
if (!FolderExists(yearFolder.Folder.ServerRelativeUrl + "/" +
currMonth, web))
{
//If not exist then create new folder JAN
monthFolder = list.Items.Add(yearFolder.Folder.ServerRelativeUrl,
SPFileSystemObjectType.Folder, currMonth);
monthFolder.Update();
}
else
{
//If exist then take the ref. of folder
monthFolder = list.Folders[web.GetFolder(
list.RootFolder.ServerRelativeUrl + "/" + currYear
+ "/" + currMonth).Item.UniqueId];
}
#endregion

#region Create week folder in the month folder
//Week folder
string currWeek = "Week_" + (System.DateTime.Now.Day / 7 + 1).ToString();
SPListItem weekFolder = null;
if (!FolderExists(monthFolder.Folder.ServerRelativeUrl
+ "/" + currWeek, web))
{
//If not exist then create new folder Week_2
weekFolder = list.Items.Add(monthFolder.Folder.ServerRelativeUrl,
SPFileSystemObjectType.Folder, currWeek);
weekFolder.Update();
}
else
{
//If exist then take the ref. of folder
weekFolder = list.Folders[web.GetFolder(
list.RootFolder.ServerRelativeUrl + "/" + currYear + "/" +
currMonth + "/" + currWeek).Item.UniqueId];
}
#endregion

#region Create date folder in the week folder
//Week folder
string currDate = System.DateTime.Now.ToString("ddMMMMyyyy");
SPListItem dateFolder = null;
if (!FolderExists(weekFolder.Folder.ServerRelativeUrl
+ "/" + currDate, web))
{
//If not exist then create new folder 09June2010
dateFolder = list.Items.Add(weekFolder.Folder.ServerRelativeUrl,
SPFileSystemObjectType.Folder, currDate);
dateFolder.Update();
}
else
{
//If exist then take the ref. of folder
dateFolder = list.Folders[web.GetFolder(
list.RootFolder.ServerRelativeUrl + "/" + currYear + "/" +
currMonth + "/" + currWeek + "/" + currDate).Item.UniqueId];
}
#endregion

item = dateFolder;

}
catch (Exception ex)
{

throw;
}
return item;
}

/// <summary>
/// Check folder exist in the SPWeb or not
/// </summary>
/// <param name="url">Complete url of the foler</param>
/// <param name="web">SPWeb object</param>
/// <returns></returns>
public static bool FolderExists(string url, SPWeb web)
{
try
{
//Check folder exist in the SPWeb or not

if (web.GetFolder(url).Exists)
{
return true;
}
return false;
}
catch (ArgumentException ex)
{
return false;
}
catch (Exception ex)
{
return false;
}
}

/// <summary>
/// Insert log entry in the log list
/// </summary>
/// <param name="list">SPList object</param>
/// <param name="dateFolder">SPListItem object for inserting log entry</param>
/// <param name="timeofExecution">time of execution</param>
/// <param name="comments">comments</param>
public static void InsertLogInList(SPList list, SPListItem dateFolder,
string timeofExecution, string comments)
{
try
{
if (dateFolder != null)
{
//Insert item in custom list > 2010 > July > Week_2 > 09July2010
SPListItem item = list.Items.Add(dateFolder.Folder.ServerRelativeUrl,
SPFileSystemObjectType.File, null);

// Insert time of execution
item["TimeofExecution"] = timeofExecution;
// Insert comments details
item["Comments"] = comments;
item.Update();
}
}
catch (Exception ex)
{
throw;
}
}
}
}


Comments

Nikhil Doomra said…
Good job... well done..

Popular posts from this blog

Sitecore GraphQL Queries

Twenty 20 Masti:

Sitecore Experience Edge GraphQL Queries