/// <summary> /// When using REST batch requests the URL needs to be correctly cased, so we're loading the web url while doing an interactive request. /// Also loading the default needed properties to save additional loads for missing key properties /// </summary> /// <param name="context">PnPContext being initialized</param> /// <param name="options">Options for the initialization of this context</param> /// <returns></returns> internal static async Task InitializeContextAsync(PnPContext context, PnPContextOptions options) { // Set environment if not yet set if (!context.Environment.HasValue) { context.Environment = CloudManager.GetEnvironmentFromUri(context.Uri); // Ensure the Microsoft Graph URL is set depending on the used cloud environment context.GraphClient.UpdateBaseAddress(CloudManager.GetMicrosoftGraphAuthority(context.Environment.Value)); } // Store the provided options, needed for context cloning context.LocalContextOptions = options; // IMPORTANT: this first call is an interactive call by design as that allows us set the // web URL using the correct casing. Correct casing is required in REST batching // IMPORTANT: if you change this logic by adding more initialization data you also need // to update the CopyContextInitialization method! // Combine the default properties to load with optional additional properties var(siteProps, webProps) = GetDefaultPropertiesToLoad(options); // Use the query client to build the correct initialization query for the given Web properties BaseDataModel <IWeb> concreteEntity = EntityManager.GetEntityConcreteInstance(typeof(IWeb), context.Web, context) as BaseDataModel <IWeb>; var entityInfo = EntityManager.GetClassInfo(concreteEntity.GetType(), concreteEntity, null, webProps.ToArray()); var apiCallRequest = await QueryClient.BuildGetAPICallAsync(concreteEntity, entityInfo, new ApiCall($"_api/Web", ApiType.SPORest), true).ConfigureAwait(false); // Load required web properties var api = new ApiCall(apiCallRequest.ApiCall.Request, ApiType.SPORest) { Interactive = true }; await(context.Web as Web).RequestAsync(api, HttpMethod.Get, "Get").ConfigureAwait(false); // Replace the context URI with the value using the correct casing context.Uri = context.Web.Url; // Request the site properties await context.Site.LoadAsync(siteProps.ToArray()).ConfigureAwait(false); // Ensure the Graph ID is set once and only once if (context.Web is IMetadataExtensible me) { if (!me.Metadata.ContainsKey(PnPConstants.MetaDataGraphId)) { me.Metadata.Add(PnPConstants.MetaDataGraphId, $"{context.Uri.DnsSafeHost},{context.Site.Id},{context.Web.Id}"); } } // If the GroupId is available ensure it's also correctly set on the Group metadata so that calls via that // model can work if (context.Site.IsPropertyAvailable(p => p.GroupId) && context.Site.GroupId != Guid.Empty) { if (context.Group is IMetadataExtensible groupMetaData) { if (!groupMetaData.Metadata.ContainsKey(PnPConstants.MetaDataGraphId)) { groupMetaData.Metadata.Add(PnPConstants.MetaDataGraphId, context.Site.GroupId.ToString()); } } } }
private static (IEnumerable <Expression <Func <ISite, object> > >, IEnumerable <Expression <Func <IWeb, object> > >) GetDefaultPropertiesToLoad(PnPContextOptions options) { IEnumerable <Expression <Func <ISite, object> > > siteProps = defaultSiteProps; IEnumerable <Expression <Func <IWeb, object> > > webProps = defaultWebProps; if (options != null) { if (options.AdditionalSitePropertiesOnCreate != null) { siteProps = siteProps.Union(options.AdditionalSitePropertiesOnCreate); } if (options.AdditionalWebPropertiesOnCreate != null) { webProps = webProps.Union(options.AdditionalWebPropertiesOnCreate); } } return(siteProps, webProps); }
/// <summary> /// Creates a new instance of PnPContext based on a provided group and using the default Authentication Provider /// </summary> /// <param name="groupId">The id of an Microsoft 365 group</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public virtual PnPContext Create(Guid groupId, PnPContextOptions options = null) { return(CreateAsync(groupId, options).GetAwaiter().GetResult()); }
/// <summary> /// Creates a new instance of PnPContext based on a provided group and using the default Authentication Provider /// </summary> /// <param name="groupId">The id of an Microsoft 365 group</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public async virtual Task <PnPContext> CreateAsync(Guid groupId, PnPContextOptions options = null) { return(await CreateAsync(groupId, ContextOptions.DefaultAuthenticationProvider, options).ConfigureAwait(false)); }
/// <summary> /// Creates a new instance of PnPContext based on a provided group and Authentication Provider instance /// </summary> /// <param name="groupId">The id of an Microsoft 365 group</param> /// <param name="authenticationProvider">The Authentication Provider to use to authenticate within the PnPContext</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public virtual PnPContext Create(Guid groupId, IAuthenticationProvider authenticationProvider, PnPContextOptions options = null) { return(CreateAsync(groupId, authenticationProvider, options).GetAwaiter().GetResult()); }
/// <summary> /// Creates a new instance of PnPContext based on a provided group and Authentication Provider instance /// </summary> /// <param name="groupId">The id of an Microsoft 365 group</param> /// <param name="authenticationProvider">The Authentication Provider to use to authenticate within the PnPContext</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public async virtual Task <PnPContext> CreateAsync(Guid groupId, IAuthenticationProvider authenticationProvider, PnPContextOptions options = null) { if (groupId == Guid.Empty) { throw new ArgumentException(PnPCoreResources.Exception_CreatePnPContext_GroupId, nameof(groupId)); } if (authenticationProvider == null) { throw new ArgumentNullException(nameof(authenticationProvider), PnPCoreResources.Exception_CreatePnPContext_AuthenticationProvider); } // Use the provided settings to create a new instance of PnPContext var context = new PnPContext(Log, authenticationProvider, SharePointRestClient, MicrosoftGraphClient, ContextOptions, GlobalOptions, TelemetryManager); // Configure telemetry, if enabled await ConfigureTelemetry(context).ConfigureAwait(false); // Load the group to find out what the SharePoint site url is await ConfigureForGroup(context, groupId).ConfigureAwait(false); // Perform context initialization await InitializeContextAsync(context, options).ConfigureAwait(false); return(context); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="url">The URL of the PnPContext as a URI</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public async virtual Task <PnPContext> CreateAsync(Uri url, PnPContextOptions options = null) { // Use the default settings to create a new instance of PnPContext return(await CreateAsync(url, ContextOptions.DefaultAuthenticationProvider, options).ConfigureAwait(false)); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="url">The URL of the PnPContext as a URI</param> /// <param name="authenticationProvider">The Authentication Provider to use to authenticate within the PnPContext</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public async virtual Task <PnPContext> CreateAsync(Uri url, IAuthenticationProvider authenticationProvider, PnPContextOptions options = null) { if (url == null) { throw new ArgumentNullException(nameof(url)); } if (authenticationProvider == null) { throw new ArgumentNullException(nameof(authenticationProvider), PnPCoreResources.Exception_CreatePnPContext_AuthenticationProvider); } // Use the provided settings to create a new instance of PnPContext var context = new PnPContext(Log, authenticationProvider, SharePointRestClient, MicrosoftGraphClient, ContextOptions, GlobalOptions, TelemetryManager) { Uri = url }; // Configure telemetry, if enabled await ConfigureTelemetry(context).ConfigureAwait(false); // Perform context initialization await InitializeContextAsync(context, options).ConfigureAwait(false); // Configure the global Microsoft Graph settings context.GraphFirst = ContextOptions.GraphFirst; context.GraphCanUseBeta = ContextOptions.GraphCanUseBeta; context.GraphAlwaysUseBeta = ContextOptions.GraphAlwaysUseBeta; return(context); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="url">The URL of the PnPContext as a URI</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public virtual PnPContext Create(Uri url, PnPContextOptions options = null) { return(CreateAsync(url, options).GetAwaiter().GetResult()); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="name">The name of the PnPContext configuration to use</param> /// <param name="initializeAuthenticationProvider">The function to initialize the authentication provider</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public async virtual Task <PnPContext> CreateAsync(string name, Action <IAuthenticationProvider> initializeAuthenticationProvider, PnPContextOptions options = null) { // Search for the provided configuration var configuration = ContextOptions.Configurations.FirstOrDefault(c => c.Name == name); if (configuration == null) { throw new ClientException(ErrorType.ConfigurationError, string.Format(PnPCoreResources.Exception_ConfigurationError_InvalidPnPContextConfigurationName, name)); } // Process the Authentication Provider initialization code, if any initializeAuthenticationProvider?.Invoke(configuration.AuthenticationProvider); return(await CreateAsync(configuration.SiteUrl, configuration.AuthenticationProvider, options).ConfigureAwait(false)); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="name">The name of the PnPContext configuration to use</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public async virtual Task <PnPContext> CreateAsync(string name, PnPContextOptions options = null) { return(await CreateAsync(name, null, options).ConfigureAwait(false)); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="name">The name of the PnPContext configuration to use</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public virtual PnPContext Create(string name, PnPContextOptions options = null) { return(CreateAsync(name, options).GetAwaiter().GetResult()); }
/// <summary> /// Creates a new instance of PnPContext based on a provided configuration name /// </summary> /// <param name="name">The name of the PnPContext configuration to use</param> /// <param name="initializeAuthenticationProvider">The function to initialize the authentication provider</param> /// <param name="options">Options used to configure the created context</param> /// <returns>A PnPContext object based on the provided configuration name</returns> public virtual PnPContext Create(string name, Action <IAuthenticationProvider> initializeAuthenticationProvider, PnPContextOptions options = null) { return(CreateAsync(name, initializeAuthenticationProvider, options).GetAwaiter().GetResult()); }