This repository was archived by the owner on Dec 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathFileOperations.cs
More file actions
249 lines (211 loc) · 9.56 KB
/
FileOperations.cs
File metadata and controls
249 lines (211 loc) · 9.56 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office365.SharePoint.FileServices;
using Microsoft.Office365.SharePoint.CoreServices.Extensions;
using model = O365_APIs_Start_ASPNET_MVC.Models;
using System.Threading.Tasks;
using Microsoft.OData.Client;
using Microsoft.OData.Core;
using System.Diagnostics;
using System.Text;
using System.IO;
namespace O365_APIs_Start_ASPNET_MVC.Helpers
{
public class FileOperations
{
/// <summary>
/// Performs a search of the default Documents folder.
/// </summary>
/// <returns>A collection of information that describes files and folders.</returns>
internal async Task<List<model.FileObject>> GetMyFilesAsync()
{
var sharePointClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");
//IOrderedEnumerable<IFileSystemItem> files = null;
List<model.FileObject> returnResults = new List<model.FileObject>();
try
{
// Performs a search of the default Documents folder.
// You could also specify other folders using the syntax: var filesResults = await _client.Files["folder_name"].ExecuteAsync();
// This results in a call to the service.
var filesResults = await sharePointClient.Files.ExecuteAsync();
var files = filesResults.CurrentPage;
foreach (IItem fileItem in files)
{
// The item to add to the result set.
model.FileObject modelFile = new model.FileObject(fileItem);
returnResults.Add(modelFile);
}
}
catch (ODataErrorException)
{
return null;
}
catch (DataServiceQueryException)
{
return null;
}
catch (MissingMethodException e)
{
Debug.Write(e.Message);
}
return returnResults;
}
//<summary>
//Creates a new file named demo.txt in the default document library.
//</summary>
//<returns>A Boolean value that indicates whether the new text file was successfully created.</returns>
internal async Task<String> CreateNewTextFileAsync()
{
//bool isSuccess = false;
String newID = string.Empty;
var sharePointClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");
try
{
// First check whether demo.txt already exists. If it exists, delete it.
// If it doesn't exist, swallow the error.
IItem item = await sharePointClient.Files.GetByPathAsync("demo.txt");
await item.DeleteAsync();
}
catch (ODataErrorException)
{
// fail silently because demo.txt doesn't exist.
}
try
{
// In this example, we'll create a simple text file and write the current timestamp into it.
string createdTime = "Created at " + DateTime.Now.ToLocalTime().ToString();
byte[] bytes = Encoding.UTF8.GetBytes(createdTime);
using (MemoryStream stream = new MemoryStream(bytes))
{
// File is called demo.txt. If it already exists, we'll get an exception.
Microsoft.Office365.SharePoint.FileServices.File newFile = new Microsoft.Office365.SharePoint.FileServices.File
{
Name = "demo.txt"
};
// Create the empty file.
await sharePointClient.Files.AddItemAsync(newFile);
newID = newFile.Id;
// Upload the file contents.
await sharePointClient.Files.GetById(newFile.Id).ToFile().UploadAsync(stream);
}
}
// ODataErrorException can be thrown when you try to create a file that already exists.
catch (Microsoft.Data.OData.ODataErrorException)
{
//isSuccess = false;
}
return newID;
}
/// <summary>
/// Deletes the selected item or folder from the ListBox.
/// </summary>
/// <returns>A Boolean value that indicates whether the file or folder was successfully deleted.</returns>
internal async Task<bool?> DeleteFileOrFolderAsync(string selectedItemID)
{
bool? isSuccess = false;
try
{
// Make sure we have a reference to the SharePoint client
var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");
// Get the file to be removed from the SharePoint service. This results in a call to the service.
IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID);
IFileFetcher thisFileFetcher = thisItemFetcher.ToFile();
var thisFile = await thisFileFetcher.ExecuteAsync();
// Delete the file or folder. This results in a call to the service.
await thisFile.DeleteAsync();
isSuccess = true;
}
catch (Microsoft.Data.OData.ODataErrorException)
{
isSuccess = null;
}
catch (NullReferenceException)
{
isSuccess = null;
}
return isSuccess;
}
/// <summary>
/// Reads the contents of a text file and displays the results in a TextBox.
/// </summary>
/// <param name="_selectedFileObject">The file selected in the ListBox.</param>
/// <returns>A Boolean value that indicates whether the text file was successfully read.</returns>
internal async Task<object[]> ReadTextFileAsync(string selectedItemID)
{
string fileContents = string.Empty;
object[] results = new object[] { fileContents, false };
try
{
// Make sure we have a reference to the SharePoint client
var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");
// Get a handle on the selected item.
IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID);
IFileFetcher thisFileFetcher = thisItemFetcher.ToFile();
var myFile = await thisFileFetcher.ExecuteAsync();
// Check that the selected item is a .txt file.
if (!myFile.Name.EndsWith(".txt") && !myFile.Name.EndsWith(".xml"))
{
results[0] = string.Empty;
results[1] = false;
return results;
}
Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File;
// Download the text file and put the results into a string. This results in a call to the service.
using (Stream stream = await file.DownloadAsync())
{
using (StreamReader reader = new StreamReader(stream))
{
results[0] = await reader.ReadToEndAsync();
results[1] = true;
}
}
}
catch (NullReferenceException)
{
results[1] = false;
}
catch (ArgumentException)
{
results[1] = false;
}
return results;
}
/// <summary>
/// Update the currently selected item by appending new text.
/// </summary>
/// <param name="_selectedFileObject">The file selected in the ListBox.</param>
/// <param name="fileText">The updated text contents of the file.</param>
/// <returns>A Boolean value that indicates whether the text file was successfully updated.</returns>
internal async Task<bool> UpdateTextFileAsync(string selectedItemID, string fileText)
{
Microsoft.Office365.SharePoint.FileServices.File file;
byte[] byteArray;
bool isSuccess = false;
try
{
// Make sure we have a reference to the SharePoint client
var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles");
// Get a handle on the selected item.
IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID);
IFileFetcher thisFileFetcher = thisItemFetcher.ToFile();
var myFile = await thisFileFetcher.ExecuteAsync();
file = myFile as Microsoft.Office365.SharePoint.FileServices.File;
string updateTime = "\n\r\n\rLast update at " + DateTime.Now.ToLocalTime().ToString();
byteArray = Encoding.UTF8.GetBytes(fileText + updateTime);
using (MemoryStream stream = new MemoryStream(byteArray))
{
// Update the file. This results in a call to the service.
await file.UploadAsync(stream);
isSuccess = true; // We've updated the file.
}
}
catch (ArgumentException)
{
isSuccess = false;
}
return isSuccess;
}
}
}