forked from koltyakov/gosip
/
webs.go
91 lines (72 loc) · 2.65 KB
/
webs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/recolabs/gosip"
)
//go:generate ggen -ent Webs -item Web -conf -coll -mods Select,Expand,Filter,Top,OrderBy -helpers Data,Normalized
// Webs represent SharePoint Webs API queryable collection struct
// Always use NewWebs constructor instead of &Webs{}
type Webs struct {
client *gosip.SPClient
config *RequestConfig
endpoint string
modifiers *ODataMods
}
// WebsResp - webs response type with helper processor methods
type WebsResp []byte
// NewWebs - Webs struct constructor function
func NewWebs(client *gosip.SPClient, endpoint string, config *RequestConfig) *Webs {
return &Webs{
client: client,
endpoint: endpoint,
config: config,
modifiers: NewODataMods(),
}
}
// ToURL gets endpoint with modificators raw URL
func (webs *Webs) ToURL() string {
return toURL(webs.endpoint, webs.modifiers)
}
// Get gets Webs response - a collection of WebInfo for the parent Web
func (webs *Webs) Get(ctx context.Context) (WebsResp, error) {
client := NewHTTPClient(webs.client)
return client.Get(ctx, webs.ToURL(), webs.config)
}
// Add creates a sub web for a parent web with provided `title` and `url`.
// `url` stands for a system friendly URI (e.g. `finances`) while `title` is a human friendly name (e.g. `Financial Department`).
// Along with title and url additional metadata can be provided in optional `metadata` string map object.
// `metadata` props should correspond to `SP.WebCreationInformation` API type. Some props have defaults as Language (1033), WebTemplate (STS), etc.
func (webs *Webs) Add(ctx context.Context, title string, url string, metadata map[string]interface{}) (WebResp, error) {
endpoint := fmt.Sprintf("%s/Add", webs.endpoint)
if metadata == nil {
metadata = make(map[string]interface{})
}
metadata["__metadata"] = map[string]string{
"type": "SP.WebCreationInformation",
}
metadata["Title"] = title
metadata["Url"] = url
// metadata["Description"]
// Default values
if metadata["Language"] == nil {
metadata["Language"] = 1033
}
if metadata["UseSamePermissionsAsParentSite"] == nil {
metadata["UseSamePermissionsAsParentSite"] = true
}
if metadata["WebTemplate"] == nil {
metadata["WebTemplate"] = "STS"
}
parameters, _ := json.Marshal(metadata)
body := TrimMultiline(`{
"parameters": ` + string(parameters) + `
}`)
client := NewHTTPClient(webs.client)
headers := getConfHeaders(webs.config)
headers["Accept"] = "application/json;odata=verbose"
headers["Content-Type"] = "application/json;odata=verbose;charset=utf-8"
return client.Post(ctx, endpoint, bytes.NewBuffer([]byte(body)), patchConfigHeaders(webs.config, headers))
}