Saturday, October 17, 2009

Folder Modified Date doesn't get updated

Scenario:
One of the bug in sharepoint folders is that they don't get updated when a new item inside the folder is added / updated. Modified Date of the folder will still show the old time stamp.

Solution:
I quickly wrote an Item Event Handler which can fix the time stamp for the item folder.

Code:

/// <summary>
/// Asynchronous after event that occurs after a new item has been added to its containing object.
/// </summary>
/// <param name="properties">
/// A Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler.
/// </param>
public override void ItemAdded(SPItemEventProperties properties)
{
string listItemUrl = properties.ListItem.Url;
string temp = listItemUrl.Remove(listItemUrl.LastIndexOf('/'));
string folderName = temp.Substring(temp.LastIndexOf('/') + 1);

SPQuery query = new SPQuery()
{
Query = string.Format("<Where><And><Eq><FieldRef Name='ContentType' /><Value Type='Text'>folder</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></And></Where>",folderName),
RowLimit = 1
};

SPList list = properties.ListItem.ParentList ;
SPListItemCollection items = list.GetItems(query);

items[0].Update();
list.Update();
}
Note:
Right now this code will fix the Modified date of the folder only when a new item is added , so need to have the same code in Item updated if you want Folder modified date to be changed.

Also please test fully in case you have more than one level of folder structure.I think it will work but I haven't tested it.

6 comments:

Uday Kiran,  November 5, 2009 at 12:34 PM  

Dude thanks for the code but this does not seem to be working for sub folders. SPList list = properties.ListItem.ParentList; gives "Shared Documents" as parent list even though when files are in sub folder. I guess you got what I mean, is there any work around to update modified date when files are added to sub folders.

Thanks again.

Thanks

Sandeep November 5, 2009 at 12:37 PM  

Please read the note on the blog once, it will work for one level folder in any document library

Uday Kiran,  November 5, 2009 at 1:10 PM  

Hmm lets say the folder structure is as follows:

"Shared Documents" under which there is folder called "ABC" under which there is a folder called "XYZ"(Shared Document>ABC>XYZ) and we are adding files to "XYZ". I am not sure if SPQuery works on sub folders but this code bombs when it reaches items[0].Update();

I know code is working like charm when there is one level of folder structure but my question was for more that one level of folder structure.

Anonymous,  February 27, 2011 at 4:14 PM  

for someone who doesnt know how to edit the script in a sharepoint site... can you please give a short directions on where i can go to put in the code you have...

I do know programming so i can take full responsiblity of adding the code correctly - but I havent worked on sharepoint sites much so not really sure where i need to go to edit the code in order to have my folders show the correct modify date based on their content and not the folder itself.

Thanks again - Your kind help is much appreciated.

Regards

Sandeep February 27, 2011 at 9:44 PM  

this code will be a part of event
handler. This ensures that this code fire whenever any new item is added or existing item is modified.

Here's a helpful link
http://blogs.msdn.com/b/brianwilson/archive/2007/03/05/part-1-event-handlers-everything-you-need-to-know-about-microsoft-office-sharepoint-portal-server-moss-event-handlers.aspx

Anonymous,  March 27, 2014 at 2:45 AM  

Hi
Can you please explain about the query.whats the field ref name?