-
Notifications
You must be signed in to change notification settings - Fork 53
Getting the value of a List property #107
-
Hello, I am trying to make a small application to print into the console whenever a page in my notion is edited. This is mainly to try out the API so I can make something more useful later on. I am stuck on trying to get the value of a property, In my database on notion I have the property "Taak". This property is a text property and I want to print it in the console. I can't seem to find the way to get just the text from this property though, I often get Notion.Client.TitlePropertyValue or the string "Title". Below are 2 of the things I tried
async Task DisplayUpdates() { // I use DateTime.MinValue to test for now var dateFilter = new DateFilter("Last edit", onOrAfter: DateTime.MinValue); var queryParams = new DatabasesQueryParameters { Filter = dateFilter}; var results = await _client.Databases.QueryAsync("eec1716264e54152861cf428c1b769b4", queryParams); var list = results.Results; foreach(Page p in list) { Console.WriteLine(p.Properties["Taak"]); Console.WriteLine(p.Properties["Taak"].Type); } _lastCheck = DateTime.Now; }
Both of the Console,WriteLine statements are the ones in question.
I'm fairly new to C#, but I decided to do it in this language because I've got to learn it for college, so there's probably a lot of stupid stuff in it. The error will be stupid too I expect
Beta Was this translation helpful? Give feedback.
All reactions
@KevinDaGame I see what you are doing wrong. The problem you are getting is that the Console.WriteLine
is printing the Type
of the data instead of the value that is because Console.WriteLine
try to convert the value of any type into string but there is no overload of ToString()
method defined for the custom type Notion.Client.TitlePropertyValue
(or for any PropertyValue type).
You need to write some method to extract the value out.
example.
foreach(Page p in list) {
var val = GetValue(p.Properties["Taak"])
Console.WriteLine(val);
}
object GetValue(PropertyValue p)
{
switch (p)
{
case RichTextPropertyValue richTextPropertyValue:
return richTextPr...
Replies: 1 comment 1 reply
-
@KevinDaGame I see what you are doing wrong. The problem you are getting is that the Console.WriteLine
is printing the Type
of the data instead of the value that is because Console.WriteLine
try to convert the value of any type into string but there is no overload of ToString()
method defined for the custom type Notion.Client.TitlePropertyValue
(or for any PropertyValue type).
You need to write some method to extract the value out.
example.
foreach(Page p in list) {
var val = GetValue(p.Properties["Taak"])
Console.WriteLine(val);
}
object GetValue(PropertyValue p)
{
switch (p)
{
case RichTextPropertyValue richTextPropertyValue:
return richTextPropertyValue.RichText.FirstOrDefault()?.PlainText;
// add other cases blocks to handle different types of PropertyValue
default:
return null;
}
}
Beta Was this translation helpful? Give feedback.
All reactions
-
I see! I had indeed seen the .PlainText method but it was not recognising it in my foreach loop. Now I get why, You first have to confirm the type before you can call it! Thank you!
Beta Was this translation helpful? Give feedback.