How to use request states for multiple items at the same time?

Hi Community,

maybe somebody has faced a similar task before and can give me some hints.

I have an relationship onAfterDelete server Method that is triggered when an relationship item is deleted. To ensure that I can still can use the related_id after the relationship was deleted, I pass the value with RequestState in an additional onBeforeDelete Method.

--> string related_id = (string)RequestState["prevRelID"]; 

This concept works fine, but only as long users just delete one relationship at a time. When they select multiple relationships and want to delete them, the following error message appears: "An entry with the same key already exists."

This of course makes sense. The RequestState "prevRelID" can only be generated once. But is there some way, to use this one multiple times? Or maybe it´s possible to store multiple ids in to the same RequestState as some kind of array?

Does anybody know an easy solution to solve this one? 

Thanks for any help!

Angela

  • Hi Angela,

    I haven't worked with RequestState all that much, but looking at it in Aras.Server.Core and playing around with it, it looks like you can add any type of object as the keyed value, including, for example, a List of strings. Not saying this is best, but what you could do is something like this in the onBeforeDelete method

    if (RequestState.Contains("prevRelID")) {
        List<string> requestList = (List<string>)RequestState["prevRelID"];
        requestList.Add(this.getProperty("related_id", ""));
    } else {
        RequestState.Add("prevRelID", new List<string> { this.getProperty("related_id", "") });
    }

    And then simply go through that list in the onAfterDelete method. Would something like that work for you?

    Cheers,

    C

  • Hi Angela, 

    my previous post was marked as spam for some reason, so I will try again:

    You can have any object as your value for a key in RequestState. So in your onBeforeDelete method you could include something like this:

    if (RequestState.Contains("prevRelID")) {

    List<string> relIdList = (List<string>)RequestState["prevRelID"];

    relIdList.Add(this.getProperty("related_id", ""));

    } else {

    RequestState.Add("prevRelID", new List<string> { this.getProperty("related_id", "") });

    }

    So on your onAfterDelete method, you can now access this list of related IDs with something like

    List<string> relIdList = (List<string>)RequestState["prevRelID"];

     

    Hope this helps some.

    Cheers,

    C

  • Hi cogres,

    thanks for you fast help! I already received your first answer via email. So the notification system works fine, even when your first posts didn´t make it to the forum.

    I can confirm that your solution works like a charm!

    Angela