ARCHIVING Items in a SPList

Find/Create the Archive Folder :

private SPListItem GetArchiveFolder(SPList list, string Month, string Year)
{
//Get the folder for the current month
//folderName = System.DateTime.Now.Month.ToString() + “-” + System.DateTime.Now.Year.ToString();

folderName = Month + “-” + Year;

SPListItem destinationFolder = null;

foreach (SPListItem folder in list.Folders)
{
if (folder.Name == “Archive – (” + folderName + “)”)
{
destinationFolder = folder;
break;
}
}

if (destinationFolder == null)
{
//Create new folder
destinationFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);

if (destinationFolder != null)
{
destinationFolder[“Name”] =
“Archive – (” + folderName + “)”;
destinationFolder.Update();
}
}
return destinationFolder;
}

**********************************************************************

Move Existing Item from Root Folder to the Archive Folder :

private void MoveItemToArchive(SPListItem sourceItem, SPListItem destinationFolder, string newItemLocation)
{
//Create a new item
SPListItem archiveItem = destinationFolder.ListItems.Add(newItemLocation, sourceItem.FileSystemObjectType);

string SOURCEID = sourceItem.ID.ToString().Trim();

//loop over the sourceitem, restore it
for (int i = sourceItem.Versions.Count – 1; i >= 0; i–)
{
//set the values into the archive
foreach (SPField sourceField in sourceItem.Fields)
{
SPListItemVersion version = sourceItem.Versions[i];

if ((!sourceField.ReadOnlyField) && (sourceField.Type != SPFieldType.Attachments))
{
archiveItem[sourceField.Title] = version[sourceField.Title];
}
else if (sourceField.Title == “Created” &&
sourceField.Title == “Created By” &&
sourceField.Title == “Modified” &&
sourceField.Title == “Modified By”)
{

archiveItem[sourceField.Title] = version[sourceField.Title];
}
}

//update the archive item and
//loop over the the next version
archiveItem.Update();
}

//now get the attachments, they are not versioned
foreach (string attachmentName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(
sourceItem.Attachments.UrlPrefix + attachmentName);

archiveItem.Attachments.Add(attachmentName, file.OpenBinary());
}

archiveItem.Update();

//Now delete the current item.
sourceItem.Delete();
}

*********************************************************************

Finally ARCHIVE the selected record :

private void ArchiveRecord(int RecordID)
{
SPList recordList = SPContext.Current.Web.Lists[“MyList”];
SPListItem archiveITEM = recordList.GetItemById(RecordID);

string strODate = archiveITEM[“Created”].ToString().Trim();
string year = string.Empty;
string month = string.Empty;

if (strODate != null && strODate.Trim().Length > 0)
{
DateTime dt = DateTime.Parse(strODate);
year = dt.Year.ToString();
month = dt.ToString(“MMM”);
}

SPListItem DestinationITEM = GetArchiveFolder(recordList, month, year);
folderurl = SPContext.Current.Web.Url + “/” + recordList.RootFolder.Url + “/” + DestinationITEM.DisplayName.ToString().Trim();
MoveItemToArchive(archiveITEM, DestinationITEM, folderurl);
}

Advertisements

~ by Neha Sinha on July 18, 2008.

5 Responses to “ARCHIVING Items in a SPList”

  1. Hey Neha, I was just looking for something like this. Thanks!

  2. thanks neha, i was looking for a way by which i could retain the created date of the archived item. Hope your post helps 🙂

  3. This was useful …

  4. I did not understand this part of the code. Am I missing something. How can title column have all the four values.

    if (sourceField.Title == “Created” &&
    sourceField.Title == “Created By” &&
    sourceField.Title == “Modified” &&
    sourceField.Title == “Modified By”)
    {
    archiveItem[sourceField.Title] = version[sourceField.Title];
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: