internal bool IsValidServerVersion(Uri url) { using (var 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(StringResourceManager.GetResourceString("ValidateServerVersionCouldntConnectToUri", new object[0])); } if (!string.IsNullOrEmpty(str)) { string input = str.Split(new char[] { ',' })[0]; if (Version.TryParse(input, out Version version)) { return(version.Major >= 15); } } } return(false); }
public SPOService InstantiateSPOService(Uri url, string loginUrl, PSCredential credentials, string authenticationUrl = COMMON_AUTH_URL, PromptBehavior?behavior = null) { if (!IsValidServerVersion(url)) { throw new InvalidOperationException(StringResourceManager.GetResourceString( "ValidateServerVersionInvalidVersion", new object[0])); } var context = new CmdLetContext(url.ToString(), null, null); // this is where the site "sites/Clients" is set. if (credentials == null) { OAuthSession session; if (CTX.SP1 == null) { session = new OAuthSession(authenticationUrl); session.SignIn(loginUrl, behavior.Value); // the login Url is the base site though. } else { CTX.SP1.OAuthSession.EnsureValidAuthToken(); session = CTX.SP1.OAuthSession; } context.OAuthSession = session; } else { var credentials2 = new SharePointOnlineCredentials(credentials.UserName, credentials.Password); context.Credentials = credentials2; } return(new SPOService(context)); }
public CmdLetContext(string webFullUrl, CmdLetContext rootContext) : base(webFullUrl) { base.ClientTag = GetClientTag(); ApplicationName = GetUserAgent(); OAuthSession = rootContext.OAuthSession; base.ExecutingWebRequest += new EventHandler <WebRequestEventArgs>(this.CmdLetContext_ExecutingWebRequest); }
private static void Lae <T>(IEnumerable <T> objs, CmdLetContext ctx, bool andExecute, params Expression <Func <T, object> >[] retrievals) where T : ClientObject { var cObjs = objs.ToArray(); for (int i = 0; i < cObjs.Length; i++) { var obj = cObjs[i]; if (obj != null) { try { ctx.Load(obj, retrievals); if (andExecute) { ctx.ExecuteQuery(); } } catch (InvalidQueryExpressionException e) { if (e.Message.Contains("The query expression") && e.Message.Contains("is not supported")) { return; } else { throw new AggregateException("An invalid query was attempted.", e); } } } } }
internal bool IsTenantAdminSite(CmdLetContext context) { try { new Tenant(context); context.ExecuteQuery(); return(true); } catch (ServerUnauthorizedAccessException) { return(false); } }
public SPOService SwitchContext(string newWebUrl, CmdLetContext currentContext) { if (!newWebUrl.EndsWith("/")) { newWebUrl = newWebUrl + "/"; } OAuthSession oauth = currentContext.OAuthSession; var newContext = new CmdLetContext(newWebUrl, null, null); if (currentContext.Credentials == null) { newContext.OAuthSession = oauth; newContext.OAuthSession.EnsureValidAuthToken(); } else { newContext.Credentials = currentContext.Credentials; } return(new SPOService(newContext)); }
// Methods public SPOService(CmdLetContext context) => this.Context = context ?? throw new ArgumentNullException("context");