Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Getting the value of a List property #107

Answered by KoditkarVedant
KevinDaGame asked this question in Q&A
Discussion options

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

You must be logged in to vote

@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

Comment options

@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;
 }
}
You must be logged in to vote
1 reply
Comment options

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!

Answer selected by KevinDaGame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /