Search code examples
c#foreachattask

Preventing this foreach loop from stopping early


Last week I posted this question, which was given the answer that the AtTask API does have a set limit, and to change this, "$$LIMIT" needs to be used, as explained by the AtTask site.

This is very helpful except that I am unsure where to put "$$LIMIT", as I am using C# and the AtTask C# example does not include this. I have been scouring the AtTask developer site for a solution and have been coming up empty handed.

How do I use this code in C# to raise the call limit?

Edit

The code sample included in my previous question:

JToken projects = client.Search(ObjCode.PROJECT, new {groupID = userGroupID});
foreach (var j in projects["data"].Children())
{
    Console.WriteLine("# " + counter + " Name: {0}", j.Value<string>("name")); 
    counter++;
}

Solution

  • Looks like their API turns the object passed to the second parameter of Search() into query string parameters. So in your example

    client.Search(ObjCode.PROJECT, new {groupID = userGroupID});
    

    The following url is created (123 represents whatever your group id is).

    GET /attask/api/project/search?groupID=123
    

    So to add $$LIMIT to the url add $$LIMIT to the object. Unfortunately, C# doesn't allow $ in names. So my suggestion is to modify their C# API slightly. First change your call to Search() to this

    client.Search(ObjCode.PROJECT, new {groupID = userGroupID, __Limit = 200 });
    

    Then, in AtTaskRestClient.cs, change parameterObjectToStringArray() at the end of the file to the following.

    private string[] parameterObjectToStringArray(object parameters, params string[] toAdd) {
        var properties = parameters.GetType().GetProperties();
        List<string> p = new List<string>(properties.Length);
        p.AddRange(toAdd);
        foreach(var prop in properties) {
            string propName = prop.Name;
            if (propName.StartsWith("__"))
            {
                propName = "$$" + propName.Substring(2);
            }
            string line = string.Format("{0}={1}", propName, prop.GetValue(parameters, null));
            p.Add(line);
        }
        return p.ToArray();
    }
    

    This change will also let you use their other parameters that start with $$ like $$FIRST.