none
Calculate item age for list item

    Question

  • Hi All,

    I want to display item age for each list item.

    For that I have created Today column - single line of text, then I created AgeinDays column which is a calculated field with following formula  - =DATEDIF(Created,Today,"d") 

    And the I delete the Today column, so this shows the item age but the issue is that it is not updating the Age.

    Example : I created this calculated column on 14th Jan, so the oldest item was created on 11th Jan, so the AgeinDays column shows age = 3 but if I check the Age today, it still shows age =3.

    It does not update the age, and it calculates age wrt to the date when the calculated column was created which is 14th Jan.

    Please help 


    Thanks in Advance, Jiniv Thakkar

    Monday, January 16, 2017 7:35 AM

Answers

  • Hi Romeo,

    I already had a console which was scheduled to run everyday in midnight so I just added another function to update the itemage.

    static void Main(string[] args)
            {
                ReadList();
            }
    
            public static void ReadList()
            {
                string server = "http://myserver:9999/";
                var ctx = new ClientContext(server);
                var web = ctx.Web;
                var list = web.Lists.GetByTitle("Mylist");
                var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
    
                // always use QueryTrimming to minimize size of 
                // data that has to be transfered
    
                ctx.Load(listItemCollection,
                           eachItem => eachItem.Include(
                            item => item,
                            item => item["Created"]));
                // ExecuteQuery will pull all data from SharePoint
                // which has been staged to Load()
                ctx.ExecuteQuery();
    
                foreach (ListItem listItem in listItemCollection)
                {
                    DateTime itemDate =(DateTime)listItem["Created"];
                    DateTime today = DateTime.Now;
    
                    TimeSpan difference = today - itemDate;
                    int ageInDays = difference.Days;
    
                    listItem["Test"] = ageInDays;
                    listItem.Update();
                    ctx.ExecuteQuery();
    
                }
            }


    Thanks in Advance, Jiniv Thakkar

    • Marked as answer by Jiniv Thakkar Tuesday, January 17, 2017 10:36 AM
    Tuesday, January 17, 2017 10:36 AM

All replies

  • Hi

    first remark

    calculated columns are updated only when an item is modified/updated

    For your example , keep Today Column, but set it not visible in forms

    For your issue I recommend you a scheduled powershell script, at midnight which will update all items in your list

    Let me know if you need more details


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    • Proposed as answer by croute1 Monday, January 16, 2017 3:46 PM
    Monday, January 16, 2017 7:46 AM
  • Hi Romeo,

    I already had a console which was scheduled to run everyday in midnight so I just added another function to update the itemage.

    static void Main(string[] args)
            {
                ReadList();
            }
    
            public static void ReadList()
            {
                string server = "http://myserver:9999/";
                var ctx = new ClientContext(server);
                var web = ctx.Web;
                var list = web.Lists.GetByTitle("Mylist");
                var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
    
                // always use QueryTrimming to minimize size of 
                // data that has to be transfered
    
                ctx.Load(listItemCollection,
                           eachItem => eachItem.Include(
                            item => item,
                            item => item["Created"]));
                // ExecuteQuery will pull all data from SharePoint
                // which has been staged to Load()
                ctx.ExecuteQuery();
    
                foreach (ListItem listItem in listItemCollection)
                {
                    DateTime itemDate =(DateTime)listItem["Created"];
                    DateTime today = DateTime.Now;
    
                    TimeSpan difference = today - itemDate;
                    int ageInDays = difference.Days;
    
                    listItem["Test"] = ageInDays;
                    listItem.Update();
                    ctx.ExecuteQuery();
    
                }
            }


    Thanks in Advance, Jiniv Thakkar

    • Marked as answer by Jiniv Thakkar Tuesday, January 17, 2017 10:36 AM
    Tuesday, January 17, 2017 10:36 AM