Documents
Download & upload files from/to SharePoint is simple
SharePoint is ECM (Enterprise Content Management) system and it's common to expect files being uploaded, downloaded, migrated, processes, and managed in a variety ways.
Gosip provides an easy way of dealing with SharePoint document listaries, files and folders.

Getting library object

Document library in SharePoint is almost the same as a List, but with intention of being a container for files.
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("MyLibrary")
4
5
// other common but less recommended way of getting a list is
6
// list := sp.Web().Lists().GetByTitle("My Library")
Copied!

Getting folder object

1
foler := sp.Web().GetFolder("MyLibrary/Folder01")
Copied!

Getting file object

1
file := sp.Web().GetFile("MyLibrary/Folder01/File01.txt")
Copied!

Adding new folder

1
// folderResp is a byte array read from response body with extra methods
2
folderResp, err := foler.Folders().Add("New Folder Name")
3
if err != nil {
4
log.Fatal(err)
5
}
6
7
fmt.Printf("New folder URL: %s\n", folderResp.Data().ServerRelativeURL)
Copied!

Deleting folders

1
folderRelativeURL := "MyLibrary/Folder01/New Folder Name"
2
if _, err := sp.Web().GetFolder(folderRelativeURL).Delete(); err != nil {
3
log.Fatal(err)
4
}
Copied!

Adding/uploading a file

1
// fileAddResp is a byte array read from response body with extra methods
2
fileAddResp, err := foler.Files().
3
Add("My File.txt", []byte("File content"), true)
4
5
if err != nil {
6
log.Fatal(err)
7
}
8
9
fmt.Printf("New file URL: %s\n", fileAddResp.Data().ServerRelativeURL)
Copied!
Obviously, file content can be a result of reading a file from disk, e.g.:
1
content, err := ioutil.ReadFile("/path/to/file.txt")
2
if err != nil {
3
log.Fatal(err)
4
}
5
6
fileAddResp, err := foler.Files().Add("My File.txt", content, true)
7
if err != nil {
8
log.Fatal(err)
9
}
Copied!
For the large files it's better using AddChunked API, hovewer, it was not available in SharePoint 2013.
1
file, err := os.Open("/path/to/large/file.zip")
2
if err != nil {
3
log.Fatalf("unable to read a file: %v\n", err)
4
}
5
defer file.Close()
6
7
fileAddResp, err := foler.Files().AddChunked("My File.zip", file, nil)
8
if err != nil {
9
log.Fatal(err)
10
}
11
12
fmt.Printf("New file URL: %s\n", fileAddResp.Data().ServerRelativeURL)
Copied!

Downloading files

1
fileRelativeURL := "MyLibrary/Folder01/File01.txt"
2
data, err := sp.Web().GetFile(fileRelativeURL).Download()
3
if err != nil {
4
log.Fatal(err)
5
}
6
7
file, err := os.Create("/path/toLocal/file.txt")
8
if err != nil {
9
log.Fatalf("unable to create a file: %v\n", err)
10
}
11
defer file.Close()
12
13
_, err = file.Write(data)
14
if err != nil {
15
log.Fatalf("unable to write to file: %v\n", err)
16
}
17
18
file.Sync()
Copied!
For a large files it's better getting file reader thought:
1
fileRelativeURL := "MyLibrary/Folder01/File01.txt"
2
fileReader, err := sp.Web().GetFile(fileRelativeURL).GetReader()
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer fileReader.Close()
7
8
file, err := os.Create("/path/toLocal/file.txt")
9
if err != nil {
10
log.Fatalf("unable to create a file: %v\n", err)
11
}
12
defer file.Close()
13
14
if _, err := io.Copy(file, fileReader); err != nil {
15
log.Fatalf("unable to save a file: %v\n", err)
16
}
Copied!

Summary

Using Gosip you can concentrate on business logic and Go language aspects while processing documents actions in SharePoint seemlessly.
With use of IntelliSense and Fluent syntax other supported actions can be consumed based on a specific requirement.
Last modified 2yr ago