Search code examples
c#getstream-io

C# How to add custom fields to an activity in getstream.io?


I want to add Custom Fields in getstream.io activity. How to do that? I was trying to add by using dictionary, the activity gets added but the extra fields are not visible in getstream.io application. If extra fields got added then how can I view them?

Thanks.


Solution

  • The C# API client allows you to add custom fields with the SetData Activity method

    public void SetData<T>(string name, T data);
    

    and to read them back with the GetData method

    public T GetData<T>(string name);
    

    Eg.

    Task.Run(async () => {
        var APIclient = new StreamClient("key", "secret");
        var feed = APIclient.Feed("user", "sajad");
    
        var activity = new Activity("sajad", "say", "hello world!");
        activity.SetData("api-client", "c-sharp");
    
        await feed.AddActivity(activity);
    
        var response = await feed.GetActivities(0, 10);
        Console.WriteLine("{0}", response.Results[0].GetData<String>("text"));
    
    }).GetAwaiter().GetResult();