-
Notifications
You must be signed in to change notification settings - Fork 11
/
SPOSiteHelper.cs
105 lines (98 loc) · 3.57 KB
/
SPOSiteHelper.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
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.Win32;
using System.Net;
namespace Lapointe.SharePointOnline.PowerShell
{
internal class SPOSiteHelper
{
internal static string HEADER_SHAREPOINT_VERSION = "MicrosoftSharePointTeamServices";
internal static SPOSiteContext InstantiateSPOSiteContext(Uri url, PSCredential credentials, PSHost host)
{
return InstantiateSPOSiteContext(url, credentials, host, false);
}
internal static SPOSiteContext InstantiateSPOSiteContext(Uri url, PSCredential credentials, PSHost host, bool isNetworkCredentials)
{
if (!IsValidServerVersion(url))
{
throw new InvalidOperationException("Could not connect to SharePoint Online: unsupported version of service.");
}
CmdletContext context = new CmdletContext(url.AbsoluteUri, host);
if (isNetworkCredentials)
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = credentials.GetNetworkCredential();
}
else
{
context.Credentials = new SharePointOnlineCredentials(credentials.UserName, credentials.Password);
}
if (context.HasPendingRequest)
context.ExecuteQuery();
return new SPOSiteContext(context);
}
internal static bool IsValidServerVersion(Uri url)
{
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = CmdletContext.GetUserAgent();
string str = null;
try
{
client.DownloadData(url);
str = client.ResponseHeaders[HEADER_SHAREPOINT_VERSION];
}
catch (WebException exception)
{
if ((exception != null) && (exception.Response != null))
{
str = exception.Response.Headers[HEADER_SHAREPOINT_VERSION];
}
}
if (str == null)
{
throw new InvalidOperationException("Could not connect to SharePoint Online.");
}
if (!string.IsNullOrEmpty(str))
{
Version version;
string str2 = str.Split(new char[] { ',' })[0];
if (Version.TryParse(str2, out version))
{
return (version.Major >= 15);
}
}
}
return false;
}
public static void ValidateUri(string name, Uri uri)
{
if ((string.IsNullOrEmpty(name) || (uri == null)) || ((uri.Scheme != Uri.UriSchemeHttp) && (uri.Scheme != Uri.UriSchemeHttps)))
{
throw new ArgumentException(name);
}
}
public static void ValidateUrl(string name, string url)
{
Uri uri;
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(name);
}
try
{
uri = new Uri(url);
}
catch (UriFormatException)
{
throw new ArgumentException(name);
}
ValidateUri(name, uri);
}
}
}