/
TeamsiteFile.cs
94 lines (77 loc) · 2.11 KB
/
TeamsiteFile.cs
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
#region Imported Namespaces
using System;
using System.IO;
using Microsoft.SharePoint;
using EA.Logger;
#endregion
namespace TeamSiteReports
{
/// <summary>
/// Contains some methods used for loading files into teamsite
/// </summary>
public class TeamSiteFile
{
public void UploadFile(string srcUrl, string destUrl)
{
if (! File.Exists(srcUrl))
{
FileErrorLogger _logger = new FileErrorLogger();
_logger.LogError(String.Format("{0} does not exist", srcUrl), ErrorLogSeverity.SeverityError,
ErrorType.TypeApplication, "TeamSiteReports.UploadFile()");
_logger = null;
}
SPWeb site = new SPSite(destUrl).OpenWeb();
try
{
FileStream fStream = File.OpenRead(srcUrl);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
site.Files.Add(destUrl, contents,true);
}
catch(SPException spe)
{
FileErrorLogger _logger = new FileErrorLogger();
_logger.LogError(spe.Message+srcUrl, ErrorLogSeverity.SeverityError,
ErrorType.TypeApplication, "TeamSiteReports.UploadFile()");
_logger = null;
}
catch(Exception e)
{
Object thisLock = new Object();
lock (thisLock)
{
FileErrorLogger _logger = new FileErrorLogger();
_logger.LogError(e.Message+srcUrl, ErrorLogSeverity.SeverityError,
ErrorType.TypeApplication, "TeamSiteReports.UploadFile()");
_logger = null;
}
}
finally
{
if (site !=null)
site.Dispose();
}
}
private string EnsureParentFolder(SPWeb parentSite, string destinUrl)
{
destinUrl = parentSite.GetFile(destinUrl).Url;
int index = destinUrl.LastIndexOf("/");
string parentFolderUrl = string.Empty;
if (index > -1)
{
parentFolderUrl = destinUrl.Substring(0, index);
SPFolder parentFolder = parentSite.GetFolder(parentFolderUrl);
if (! parentFolder.Exists)
{
SPFolder currentFolder = parentSite.RootFolder;
foreach(string folder in parentFolderUrl.Split('/'))
{
currentFolder = currentFolder.SubFolders.Add(folder);
}
}
}
return parentFolderUrl;
}
}
}