Monday, July 19, 2010

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;
}
}
}
}


Share:

1 comments:

Nikhil Doomra said...

Good job... well done..

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels