Library Initiation
Configuring authentication and API client

Binding authentication and API client

Understand SharePoint environment type and authentication strategy

Based on authentication provider supported and configured in your SharePoint Farm environment different library authentication strategies might be or not be applicable.
If you have no idea which strategy is used within your farm the question is better be addressed to SharePoint admins.
Let's assume it's SharePoint Online and Add-In Only permissions. Then strategy "github.com/koltyakov/gosip/auth/addin" sub package should be used.
1
package main
2
3
import (
4
"github.com/koltyakov/gosip"
5
"github.com/koltyakov/gosip/api"
6
strategy "github.com/koltyakov/gosip/auth/addin"
7
)
Copied!
It could have been SharePoint On-Premise and NTLM and strategy "github.com/koltyakov/gosip/auth/ntlm" as imported strategy.

Initiate authentication object

Different authentication strategies assumes different credential parameters. Sometimes it can be Username/Password, sometimes CertPath or ClientID/ClientSecret. Please refer a specific strategy documentation for relevalt for the auth type parameters.
Credential can be passed directly in AuthCnfg's struct. Here a sample for addin:
1
auth := &strategy.AuthCnfg{
2
SiteURL: os.Getenv("SPAUTH_SITEURL"),
3
ClientID: os.Getenv("SPAUTH_CLIENTID"),
4
ClientSecret: os.Getenv("SPAUTH_CLIENTSECRET"),
5
}
Copied!
An anternative is using a configuration file (see more). Which is a JSON containing the same parameters as used with AuthCnfg's struct.
1
configPath := "./config/private.json"
2
auth := &strategy.AuthCnfg{}
3
4
err := auth.ReadConfig(configPath)
5
if err != nil {
6
fmt.Printf("Unable to get config: %v\n", err)
7
return
8
}
Copied!

Bind auth client with Fluent API

Now when auth is bound it should be passed to client and Fluent API instance:
1
client := &gosip.SPClient{AuthCnfg: auth}
2
3
sp := api.NewSP(client)
Copied!
Most of the samples starts with sp assuming configuration described above is already in place.
1
res, err := sp.Web().Select("Title").Get()
2
if err != nil {
3
fmt.Println(err)
4
}
5
6
fmt.Printf("%s\n", res.Data().Title)
Copied!
Last modified 6mo ago