Wednesday, August 31, 2011

Sharepoint 2010: Folder Modified Date doesn't get updated

below is the code to update folder "ModifiedDate" when a new item is added or updated to the document library. it updates all, in case if you have more than one level of folder structure. here problem is it executes every time when you add/update any items to this Library. for example if you have 3 levels of folder structure this event calls 3 times as well.

i dont think it is a good practice...still if you have this kind of requirement from your client you can use below code.


    public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            if (properties.List.BaseTemplate.ToString() == "10017") // working documents
            {
                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (properties.ListItem.Url != null)
                        {
                            string listItemUrl = properties.ListItem.Url;
                            string folderPath = listItemUrl.Remove(listItemUrl.LastIndexOf('/'));
                           // string folderName = folderPath.Substring(folderPath.LastIndexOf('/') + 1);

                            SPFolder folder = web.GetFolder(folderPath);

                            if (folder != null)
                            {
                                SPListItem spitem = folder.Item;
                                if (spitem != null && spitem[SPBuiltInFieldId.Modified] != null)
                                {
                                    spitem[SPBuiltInFieldId.Modified] = DateTime.Now;
                                    spitem.Update();
                                }
                            }
                        }
                    }
                }
            }
        }
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            if (properties.List.BaseTemplate.ToString() == "10017") // working documents
            {

                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (properties.ListItem.Url != null)
                        {
                            string listItemUrl = properties.ListItem.Url;
                            string folderPath = listItemUrl.Remove(listItemUrl.LastIndexOf('/'));

                            SPFolder folder = web.GetFolder(folderPath);

                            if (folder != null)
                            {
                                SPListItem spitem = folder.Item;
                                if (spitem != null && spitem[SPBuiltInFieldId.Modified] != null)
                                {
                                    spitem[SPBuiltInFieldId.Modified] = DateTime.Now;
                                    spitem.Update();
                                }
                            }
                        }
                    }
                }
            }
        }

5 comments:

  1. Nice article. I was wondering where I would put this code on SharePoint 2010 to make it run? Do you create an event handler?

    ReplyDelete
  2. i want to get folder created date, modified date and author in sharepoint client object model please send me any related suggestions for that

    ReplyDelete
    Replies
    1. follow this article to update listitems using object model

      http://stackoverflow.com/questions/6947491/updating-field-value-in-sharepoint-using-client-object-model

      may be you can place the code in sharepoint designer and give a try.

      Delete

Followers

Blog Archive