koltyakov / gosip

⚡️ SharePoint SDK for Go
https://go.spflow.com
MIT License
145 stars 33 forks source link

Adding a calendar event #34

Closed brett0701 closed 4 years ago

brett0701 commented 4 years ago

I'm trying to add a calendar event to a calendar. I can add the event with the Title, Location and Description fields but I get an error when attempting to add the 'Start Time' and 'End Time'. The error I get is: {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The property 'End Time' does not exist on type 'SP.Data.MyCalendarListItem'. Make sure to only use property names that are defined by the type."}}}

This is an example of the the data, I'm posting: { "Start Time": "2020-09-08T16:00:00Z", "End Time": "2020-09-08T16:30:00Z", "Title": "My First Meeting", "Location": "Zoom Meeting", "Description": "This is my meeting" }

data, err := sp.Conf(config).Web().Lists().GetByTitle("MyCalendar").Items().Add(byteData)

Is there support for adding Calendar events or am I just doing something completely incorrect?

koltyakov commented 4 years ago

Long story short: Calendars are supported, it's just a list, but Start Time and End Time are wrong field names:

image

In SharePoint, there is a number of different field types: Display Name, Internal Name, Static Name, Entity Property Name.

What you see in the UI are Display Names, they can be multilingual and almost never used in code. Display names can be easily redefined in the UI and potentially break the app.

In code, it is usual using Internal Names instead. Internal Names contains no spaces and usually would be named as StartDate, EndTime if you provision some artifacts. With manually created artifacts many lazy or none-experience consultants provide names with spaces or in local languages, etc. This brings internal names being replaced based on rules avoiding unsupported characters, that's why sometimes internal names can be ugly.

Anyways, Internal and Display names are often different, as even a space character will force them to differ. The simplest way of getting an Internal Name is navigating in the UI to the list setting and a field, field edit URL contains the internal name:

image

In REST API, in OData methods, which you're referencing to Entity Property Names are used, in 90% of cases the Entity Property Name is the same as Internal Name, but there are some exceptions (e.g. an internal name which starts with _) they are different. It could be checked explicitly with requesting field props (not in runtime obviously):

fld, _ := sp.Web().GetList("Lists/MyCalendar").Fields().
    GetByInternalNameOrTitle("Start Time").
    Select("Title,InternalName,EntityPropertyName").
    Get()

fmt.Printf("Display Name: %s\n", fld.Data().Title)
fmt.Printf("Internal Name: %s\n", fld.Data().InternalName)
fmt.Printf("Entity Property Name: %s\n", fld.Data().EntityPropertyName)
brett0701 commented 4 years ago

Hi Andrew. Thank you for the detailed answer. I should've suspected that by the error I was receiving. Thanks for responding so quickly.