Basic CRUD
Create, read, update and delete
CRUD is the most commonly requested type of API consumption.
This example demonstrate basic operations on a list items sample. Hovewer, SharePoint has a variety of nuances even when it comes to just getting items from a list.

Getting list object

1
// The recommended way of getting lists is by using their relative URIs
2
// can be a short form without full web relative URL prefix
3
list := sp.Web().GetList("Lists/MyList")
4
5
// other common but less recommended way of getting a list is
6
// list := sp.Web().Lists().GetByTitle("My List")
Copied!

Getting items

1
// itemsResp is a byte array read from response body
2
// powered with some processing "batteries" as Data() or Unmarshal() methods
3
itemsResp, err := list.Items().
4
Select("Id,Title"). // OData $select modifier, limit what props are retrieved
5
OrderBy("Id", true). // OData $orderby modifier, defines sort order
6
Top(10). // OData $top modifier, limits page size
7
Get() // Finalizes API constructor and sends a response
8
9
if err != nil {
10
log.Fatal(err)
11
}
12
13
// Data() method is a helper which unmarshals generic structure
14
// use custom structs and unmarshal for custom fields
15
for _, item := range itemsResp.Data() {
16
itemData := item.Data()
17
fmt.Printf("ID: %d, Title: %s\n", itemData.ID, itemData.Title)
18
}
Copied!
Based on a use case, items can be reveived with alternative methods, as .GetAll, .GetPaged, .GetByCAML, or even lists methods as .RenderListData.

Adding an item

1
// Payload should be a valid JSON stringified string converted to byte array
2
// Payload's properties must be a valid OData entity types
3
itemPayload := []byte(`{
4
"Title": "New Item"
5
}`)
6
7
// Constructs and sends add operation request
8
// itemAddRes is a byte array read from response body with extra methods
9
itemAddRes, err := list.Items().Add(itemPayload)
10
if err != nil {
11
log.Fatal(err)
12
}
13
14
fmt.Printf("Raw response: %s\n", itemAddRes)
15
fmt.Printf("Added item's ID: %d\n", itemAddRes.Data().ID)
Copied!
Payloads can be constructed in a usual Go way using marshalling struct or string maps to JSON. E.g.:
1
itemMetadata := &struct{
2
Title string `json:"Title"`
3
}{
4
Title: "New Item",
5
}
6
7
itemPayload, _ := json.Marshal(itemMetadata)
Copied!
or:
1
itemMetadata := map[string]interface{}{
2
"Title": "New Item",
3
}
4
5
itemPayload, _ := json.Marshal(itemMetadata)
Copied!
up to your preferences.

Getting a specific item

1
itemID := 42 // should specific item ID
2
3
itemRes, err := list.Items().GetByID(itemID).Get()
4
if err != nil {
5
log.Fatal(err)
6
}
7
8
fmt.Printf("Raw response: %s\n", itemRes)
9
fmt.Printf("Item metadata: %+v\n", itemRes.Data())
Copied!

Updating an item

1
// Payload should be a valid JSON stringified string converted to byte array
2
// Payload's properties must be a valid OData entity types
3
itemUpdatePayload := []byte(`{
4
"Title": "Updated Title"
5
}`)
6
7
itemID := 42 // should specific item ID
8
9
// Constructs and sends update operation request
10
// itemUpdateRes is a byte array read from response body with extra methods
11
itemUpdateRes, err := list.Items().GetById(itemID).Update(itemUpdatePayload)
12
if err != nil {
13
log.Fatal(err)
14
}
15
16
fmt.Printf("Item is updated, %s\n", itemUpdateRes.Data().Modified)
Copied!
Payloads can be constructed in a usual Go way using marshalling struct or string maps to JSON, same as in add operations.

Delete an item

Item can be not only deleted but recycled with a further restore operation, which can be provide more safety.
1
itemID := 42 // should specific item ID
2
3
// list.Items().GetByID(itemID).Delete() // or .Recycle()
4
if _, err := list.Items().GetByID(itemID).Recycle(); err != nil {
5
log.Fatal(err)
6
}
Copied!