/// <summary> /// Converts ClientContext into PnPClientContext /// </summary> /// <param name="clientContext">A SharePoint ClientContext for resource operations</param> /// <param name="retryCount">Maximum amount of retries before giving up</param> /// <param name="delay">Initial delay in milliseconds</param> /// <returns></returns> public static PnPClientContext ConvertFrom(ClientContext clientContext, int retryCount = 10, int delay = 500) { var context = new PnPClientContext(clientContext.Url, retryCount, delay); context.AuthenticationMode = clientContext.AuthenticationMode; // In case of using networkcredentials in on premises or SharePointOnlineCredentials in Office 365 if (clientContext.Credentials != null) { context.Credentials = clientContext.Credentials; } else { //Take over the form digest handling setting context.FormDigestHandlingEnabled = clientContext.FormDigestHandlingEnabled; // In case of app only or SAML context.ExecutingWebRequest += delegate(object oSender, WebRequestEventArgs webRequestEventArgs) { // Call the ExecutingWebRequest delegate method from the original ClientContext object, but pass along the webRequestEventArgs of // the new delegate method MethodInfo methodInfo = clientContext.GetType().GetMethod("OnExecutingWebRequest", BindingFlags.Instance | BindingFlags.NonPublic); object[] parametersArray = new object[] { webRequestEventArgs }; methodInfo.Invoke(clientContext, parametersArray); }; } return(context); }
public static PnPClientContext ConvertFrom(ClientContext clientContext, int retryCount = 10, int delay = 500) { var context = new PnPClientContext(clientContext.Url, retryCount, delay); context.AuthenticationMode = clientContext.AuthenticationMode; // In case of using networkcredentials in on premises or SharePointOnlineCredentials in Office 365 if (clientContext.Credentials != null) { context.Credentials = clientContext.Credentials; } else { //Take over the form digest handling setting context.FormDigestHandlingEnabled = clientContext.FormDigestHandlingEnabled; // In case of app only or SAML context.ExecutingWebRequest += delegate (object oSender, WebRequestEventArgs webRequestEventArgs) { // Call the ExecutingWebRequest delegate method from the original ClientContext object, but pass along the webRequestEventArgs of // the new delegate method MethodInfo methodInfo = clientContext.GetType().GetMethod("OnExecutingWebRequest", BindingFlags.Instance | BindingFlags.NonPublic); object[] parametersArray = new object[] { webRequestEventArgs }; methodInfo.Invoke(clientContext, parametersArray); }; } return context; }
/// <summary> /// Converts ClientContext into PnPClientContext /// </summary> /// <param name="clientContext">A SharePoint ClientContext for resource operations</param> /// <param name="retryCount">Maximum amount of retries before giving up</param> /// <param name="delay">Initial delay in milliseconds</param> /// <returns></returns> public static PnPClientContext ConvertFrom(ClientContext clientContext, int retryCount = 10, int delay = 500) { var pnpContext = new PnPClientContext(clientContext.Url, retryCount, delay); clientContext.Clone(pnpContext, new Uri(clientContext.Url)); return(pnpContext); }
/// <summary> /// Clones a PnPClientContext object while "taking over" the security context of the existing PnPClientContext instance /// </summary> /// <param name="siteUri">Site URL to be used for cloned ClientContext</param> /// <returns>A PnPClientContext object created for the passed site URL</returns> public PnPClientContext Clone(Uri siteUri) { if (siteUri == null) { throw new ArgumentException(CoreResources.ClientContextExtensions_Clone_Url_of_the_site_is_required_, nameof(siteUri)); } var clonedClientContext = new PnPClientContext(siteUri) { RetryCount = this.RetryCount, Delay = this.Delay, AuthenticationMode = this.AuthenticationMode }; // In case of using networkcredentials in on premises or SharePointOnlineCredentials in Office 365 if (this.Credentials != null) { clonedClientContext.Credentials = this.Credentials; } else { //Take over the form digest handling setting clonedClientContext.FormDigestHandlingEnabled = this.FormDigestHandlingEnabled; // In case of app only or SAML clonedClientContext.ExecutingWebRequest += delegate(object oSender, WebRequestEventArgs webRequestEventArgs) { // Call the ExecutingWebRequest delegate method from the original ClientContext object, but pass along the webRequestEventArgs of // the new delegate method var methodInfo = this.GetType().GetMethod("OnExecutingWebRequest", BindingFlags.Instance | BindingFlags.NonPublic); var parametersArray = new object[] { webRequestEventArgs }; methodInfo.Invoke(this, parametersArray); }; } return(clonedClientContext); }
internal static SPOnlineConnection InstantiateSPOnlineConnection(Uri url, PSCredential credentials, PSHost host, bool currentCredentials, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, bool skipAdminCheck = false) { var context = new PnPClientContext(url.AbsoluteUri); context.RetryCount = retryCount; context.Delay = retryWait * 1000; context.ApplicationName = Properties.Resources.ApplicationName; context.RequestTimeout = requestTimeout; if (!currentCredentials) { try { SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(credentials.UserName, credentials.Password); context.Credentials = onlineCredentials; try { context.ExecuteQueryRetry(); } catch (ClientRequestException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } catch (ServerException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } catch (ArgumentException) { // OnPrem? context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); try { context.ExecuteQueryRetry(); } catch (ClientRequestException ex) { throw new Exception("Error establishing a connection", ex); } catch (ServerException ex) { throw new Exception("Error establishing a connection", ex); } } } else { if (credentials != null) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } var connectionType = ConnectionType.OnPrem; if (url.Host.ToUpperInvariant().EndsWith("SHAREPOINT.COM")) { connectionType = ConnectionType.O365; } if (skipAdminCheck == false) { if (IsTenantAdminSite(context)) { connectionType = ConnectionType.TenantAdmin; } } return new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, credentials, url.ToString()); }
/// <summary> /// Clones a PnPClientContext object while "taking over" the security context of the existing PnPClientContext instance /// </summary> /// <param name="siteUri">Site url to be used for cloned ClientContext</param> /// <returns>A PnPClientContext object created for the passed site url</returns> public PnPClientContext Clone(Uri siteUri) { if (siteUri == null) { throw new ArgumentException(CoreResources.ClientContextExtensions_Clone_Url_of_the_site_is_required_, nameof(siteUri)); } var clonedClientContext = new PnPClientContext(siteUri) { RetryCount = this.RetryCount, Delay = this.Delay, AuthenticationMode = this.AuthenticationMode }; // In case of using networkcredentials in on premises or SharePointOnlineCredentials in Office 365 if (this.Credentials != null) { clonedClientContext.Credentials = this.Credentials; } else { //Take over the form digest handling setting clonedClientContext.FormDigestHandlingEnabled = this.FormDigestHandlingEnabled; // In case of app only or SAML clonedClientContext.ExecutingWebRequest += delegate (object oSender, WebRequestEventArgs webRequestEventArgs) { // Call the ExecutingWebRequest delegate method from the original ClientContext object, but pass along the webRequestEventArgs of // the new delegate method var methodInfo = this.GetType().GetMethod("OnExecutingWebRequest", BindingFlags.Instance | BindingFlags.NonPublic); var parametersArray = new object[] { webRequestEventArgs }; methodInfo.Invoke(this, parametersArray); }; } return clonedClientContext; }
internal static SPOnlineConnection InstantiateSPOnlineConnection(Uri url, PSCredential credentials, PSHost host, bool currentCredentials, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, bool skipAdminCheck = false, ClientAuthenticationMode authenticationMode = ClientAuthenticationMode.Default) { var context = new PnPClientContext(url.AbsoluteUri); context.RetryCount = retryCount; context.Delay = retryWait * 1000; context.ApplicationName = Properties.Resources.ApplicationName; #if !ONPREMISES context.DisableReturnValueCache = true; #elif SP2016 context.DisableReturnValueCache = true; #endif context.RequestTimeout = requestTimeout; context.AuthenticationMode = authenticationMode; if (authenticationMode == ClientAuthenticationMode.FormsAuthentication) { var formsAuthInfo = new FormsAuthenticationLoginInfo(credentials.UserName, EncryptionUtility.ToInsecureString(credentials.Password)); context.FormsAuthenticationLoginInfo = formsAuthInfo; } if (!currentCredentials) { try { SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(credentials.UserName, credentials.Password); context.Credentials = onlineCredentials; try { context.ExecuteQueryRetry(); } catch (ClientRequestException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } catch (ServerException) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } catch (ArgumentException) { // OnPrem? context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); try { context.ExecuteQueryRetry(); } catch (ClientRequestException ex) { throw new Exception("Error establishing a connection", ex); } catch (ServerException ex) { throw new Exception("Error establishing a connection", ex); } } } else { if (credentials != null) { context.Credentials = new NetworkCredential(credentials.UserName, credentials.Password); } } var connectionType = ConnectionType.OnPrem; if (url.Host.ToUpperInvariant().EndsWith("SHAREPOINT.COM")) { connectionType = ConnectionType.O365; } if (skipAdminCheck == false) { if (IsTenantAdminSite(context)) { connectionType = ConnectionType.TenantAdmin; } } return new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, credentials, url.ToString(), tenantAdminUrl, PnPPSVersionTag); }