/// <summary> /// Creates a package reader to read the specified package from SharePoint. This constructor /// optionally causes the file to be read from SharePoint using elevated permissions. /// </summary> /// <param name="cacheSettings">The settings to use for the caching of this package. /// A subdirectory will be created in the cacheSettings.CachePath location with a cached version of this package.</param> /// <param name="packageLocation">The location of the package to be read. Any changes to this SharePointFileLocation /// object after the SharePointPackageReader is created are not reflected in the behavior of this object.</param> /// <param name="runWithElevatedPrivileges">If true, files in SharePoint are accessed using elevated privileges. /// If false, the current user credentials are used to access SharePoint files.</param> /// <param name="file">The SPFile to read.</param> /// <remarks> /// <para> /// In addition to the exceptions listed below, this method may throw exceptions caused by the /// identity not having access to the <paramref name="cacheSettings"/> CachePath location. /// </para> /// <para> /// The contents of the package are not read in the constructor. The contents of the package are read /// only once when they are first needed. If the referenced SharePoint file does not contain a /// valid e-learning package, accessing other methods and properties on this object will result /// in an <c>InvalidPackageException</c>. /// </para> /// <para> /// If the <paramref name="cacheSettings"/> CacheInvalidPackageAsFile value is true, /// e-learning packages that do not contain basic package information are saved as /// files in the cache. In particular, this may increase performance in processing of zip files that do not contain /// e-learning content. If false, SharePointPackageReader will not cache zip files that are not e-learning /// content. In that case, an application that wants to cache this file would need to cache it as a /// CachedSharePointFile. Regardless of the value of this parameter, the SharePointPackageReader will not allow /// accessing files from within a package that is not e-learning content. /// </para> /// </remarks> /// <exception cref="ArgumentNullException">Thrown if any argument is null.</exception> /// <exception cref="DirectoryNotFoundException">Thrown if the CachePath property of <paramref name="cacheSettings"/> /// does not exist prior to calling this constructor.</exception> /// <exception cref="FileNotFoundException">Thrown if the requested file does not exist.</exception> /// <exception cref="UnauthorizedAccessException">Thrown if the identity doesn't have access to the CachePath provided in the /// cache settings.</exception> public SharePointPackageReader(SharePointCacheSettings cacheSettings, SharePointFileLocation packageLocation, SPFile file, bool runWithElevatedPrivileges) : base(packageLocation) { Resources.Culture = Thread.CurrentThread.CurrentCulture; Utilities.ValidateParameterNonNull("cacheSettings", cacheSettings); RunWithElevatedPrivileges useRequestedPrivileges; if (runWithElevatedPrivileges) { useRequestedPrivileges = SPSecurity.RunWithElevatedPrivileges; } else { useRequestedPrivileges = RunWithCurrentUserPrivileges; } CachedPackage.EnsureCache(cacheSettings); CheckFileExists(file, packageLocation); // Store variables. m_settings = cacheSettings.Clone(); CachedPackage cachedPackage = null; useRequestedPrivileges(delegate { cachedPackage = new CachedPackage(m_settings, Location, true); }); using (cachedPackage) { Initialize(new DirectoryInfo(cachedPackage.CacheDir), m_settings.ImpersonationBehavior); } }
/// <summary> /// Create a representation of a file in SharePoint, running with elevated privileges when /// accessing the SharePoint file. /// </summary> /// <param name="cacheSettings">The settings that determine how the file is cached.</param> /// <param name="location">The location of the file in SharePoint.</param> /// <param name="runWithElevatedPrivileges">If true, SharePoint file will be accessed using elevated privileges.</param> /// <remarks> /// This method does only verifies that the <c>settings.WindowsIdentity</c> has access to the /// <c>settings.CachePath</c>. It does not verify that the file exists in SharePoint. /// The contents of the package are read only once when they are first needed. /// </remarks> /// <exception cref="UnauthorizedAccessException">Thrown if the <c>settings.WindowsIdentity</c> /// does not have appropriate permissions to the <c>settings.CachePath</c> folder.</exception> public CachedSharePointFile(SharePointCacheSettings cacheSettings, SharePointFileLocation location, bool runWithElevatedPrivileges) { Resources.Culture = Thread.CurrentThread.CurrentCulture; Utilities.ValidateParameterNonNull("cacheSettings", cacheSettings); Utilities.ValidateParameterNonNull("location", location); m_settings = cacheSettings.Clone(); m_location = location; if (runWithElevatedPrivileges) { m_useRequestedPrivileges = SPSecurity.RunWithElevatedPrivileges; } else { m_useRequestedPrivileges = RunWithCurrentUserPrivileges; } CachedPackage.EnsureCache(cacheSettings); }