none
How to get the previous item in List RRS feed

  • Question

  • Hi Lovely people,

    I have a task list where I would like a reflow of dates. The task list has a start date and deadline date.

    where Deadline date = Start date + Duration. list is some what like this:

    Item 1

    Title- AA

    Predecessor-NULL

    Start Date - 03/09/2013

    Duration - 2

    Deadline date - 05/09/2013

    Item 2

    Title- BB

    Predecessor- AA

    Start Date - 05/09/2013

    Duration - 6

    Deadline date - 11/09/2013

    if someone changes the start date on one the first, I would like that the next one's start date = the deadline date of the previous one.

    Is there any code for this? I would be very grateful for any help


    Sharepoint Dummy

    Tuesday, September 3, 2013 12:40 PM

Answers

  • Hi,

    According to your description, I understand that you want to get the previous item in List.

    We can use Calculated column to achieve Deadline date = Start date + Duration. For the second requirement we can use JavaScript Client object model to get the previous item.

    1.  I suggest to set The Deadline date field  as a Calculated column.

    2.  Add a Formula in this column as below.

    3.Please reference the code below and it into the NewForm.aspx page using SharePoint Designer.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        ExecuteOrDelayUntilScriptLoaded(InitForm, "sp.js");
    });
    
    function InitForm() {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Task List');
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><OrderBy><FieldRef Name='Deadline_x0020_date' Ascending='False' /></OrderBy></Query></View>");
        this.collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    
    }
    
    function onQuerySucceeded() {
        if (collListItem.get_count() > 0) {
            var oListItem = collListItem.itemAt(0);
            var date = new Date(oListItem.get_item('Deadline_x0020_date'));
            $("input[Title='Predecessor']").val(oListItem.get_item('Title'));
            $("input[Title='Start Date']").val(date.format("MM/dd/yyyy"));
        }
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>
    
    Best Regards

    Dennis Guo
    TechNet Community Support

    Friday, September 6, 2013 3:15 AM
    Moderator
  • this code shows how to set Start date field of new item based on the nearest Deadline field (<OrderBy><FieldRef Name='Deadline_x0020_date' Ascending='False' /></OrderBy>), while author of the post probably needed to update existing list items, when Start date of one of them is changed.

    This can be done with ItemUpdated event receiver: you need to get id of updated item and then, using it, recursively iterate through hierarchy of dependent items using Predecessor field:

    SPListItem item = ...;
    item["Deadline"] = ((DateTime)item["Start date"]).AddDays((int)item["Duration"]);
    
    updateChildren(item.ParentList, item.ID, (DateTime)item["Deadline"]);
    ...
    void updateChildren(SPList list, int parendId, DateTime deadline)
    {
        var child = list.Items.Cast<SPListItem>().FirstOrDefault(i => (int)i["Predecessor"] == parendId);
        if (child != null)
        {
            child["Start date"] = deadline;
            child["Deadline"] = ((DateTime)child["Start date"]).AddDays((int)child["Duration"]);
            updateChildren(list, child.Id, (DateTime)child["Deadline"]);
        }
    }
    (this is pseudo code, but you should get the idea)

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Friday, September 6, 2013 5:29 AM

All replies

  • Hi,

    According to your description, I understand that you want to get the previous item in List.

    We can use Calculated column to achieve Deadline date = Start date + Duration. For the second requirement we can use JavaScript Client object model to get the previous item.

    1.  I suggest to set The Deadline date field  as a Calculated column.

    2.  Add a Formula in this column as below.

    3.Please reference the code below and it into the NewForm.aspx page using SharePoint Designer.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        ExecuteOrDelayUntilScriptLoaded(InitForm, "sp.js");
    });
    
    function InitForm() {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Task List');
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><OrderBy><FieldRef Name='Deadline_x0020_date' Ascending='False' /></OrderBy></Query></View>");
        this.collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    
    }
    
    function onQuerySucceeded() {
        if (collListItem.get_count() > 0) {
            var oListItem = collListItem.itemAt(0);
            var date = new Date(oListItem.get_item('Deadline_x0020_date'));
            $("input[Title='Predecessor']").val(oListItem.get_item('Title'));
            $("input[Title='Start Date']").val(date.format("MM/dd/yyyy"));
        }
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>
    
    Best Regards

    Dennis Guo
    TechNet Community Support

    Friday, September 6, 2013 3:15 AM
    Moderator
  • this code shows how to set Start date field of new item based on the nearest Deadline field (<OrderBy><FieldRef Name='Deadline_x0020_date' Ascending='False' /></OrderBy>), while author of the post probably needed to update existing list items, when Start date of one of them is changed.

    This can be done with ItemUpdated event receiver: you need to get id of updated item and then, using it, recursively iterate through hierarchy of dependent items using Predecessor field:

    SPListItem item = ...;
    item["Deadline"] = ((DateTime)item["Start date"]).AddDays((int)item["Duration"]);
    
    updateChildren(item.ParentList, item.ID, (DateTime)item["Deadline"]);
    ...
    void updateChildren(SPList list, int parendId, DateTime deadline)
    {
        var child = list.Items.Cast<SPListItem>().FirstOrDefault(i => (int)i["Predecessor"] == parendId);
        if (child != null)
        {
            child["Start date"] = deadline;
            child["Deadline"] = ((DateTime)child["Start date"]).AddDays((int)child["Duration"]);
            updateChildren(list, child.Id, (DateTime)child["Deadline"]);
        }
    }
    (this is pseudo code, but you should get the idea)

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Friday, September 6, 2013 5:29 AM