forked from koltyakov/gosip
/
features.go
64 lines (56 loc) · 1.9 KB
/
features.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pnocera/gosip"
)
// Features represent SharePoint Webs & Site Features API queryable collection struct
// Always use NewFeatures constructor instead of &Features{}
type Features struct {
client *gosip.SPClient
config *RequestConfig
endpoint string
}
// FeatureInfo - features API response payload structure
type FeatureInfo struct {
DefinitionID string `json:"DefinitionId"`
}
// NewFeatures - Features struct constructor function
func NewFeatures(client *gosip.SPClient, endpoint string, config *RequestConfig) *Features {
return &Features{
client: client,
endpoint: endpoint,
config: config,
}
}
// Get gets features collection (IDs)
func (features *Features) Get() ([]*FeatureInfo, error) {
client := NewHTTPClient(features.client)
data, err := client.Get(features.endpoint, features.config)
if err != nil {
return nil, err
}
data, _ = NormalizeODataCollection(data)
var res []*FeatureInfo
if err := json.Unmarshal(data, &res); err != nil {
return nil, err
}
return res, nil
}
// Add activates a feature by its ID (GUID) in the parent container (Site or Web)
func (features *Features) Add(featureID string, force bool) error {
endpoint := fmt.Sprintf("%s/Add", features.endpoint)
client := NewHTTPClient(features.client)
body := []byte(fmt.Sprintf(`{"featdefScope":0,"featureId":"%s","force":%t}`, featureID, force))
_, err := client.Post(endpoint, bytes.NewBuffer(body), features.config)
return err
}
// Remove deactivates a feature by its ID (GUID) in the parent container (Site or Web)
func (features *Features) Remove(featureID string, force bool) error {
endpoint := fmt.Sprintf("%s/Remove", features.endpoint)
client := NewHTTPClient(features.client)
body := []byte(fmt.Sprintf(`{"featureId":"%s","force":%t}`, featureID, force))
_, err := client.Post(endpoint, bytes.NewBuffer(body), features.config)
return err
}