From 1b9aa73d55f5a7caaa0d5c90093d5301f717c62f Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sat, 12 Oct 2024 22:25:24 +0300 Subject: [PATCH] Refactor code to use Connection.PnPContext instead of PnPContext.Web --- src/Commands/Base/InvokeSPRestMethod.cs | 2 +- src/Commands/Branding/AddCustomAction.cs | 11 +++++------ src/Commands/Fields/RemoveField.cs | 15 ++++++++------- src/Commands/Files/ConvertFile.cs | 2 +- src/Commands/Files/GetFile.cs | 9 ++++----- src/Commands/Files/GetFileAnalyticsData.cs | 2 +- src/Commands/Files/GetFolderStorageMetric.cs | 18 +++++++++--------- 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/Commands/Base/InvokeSPRestMethod.cs b/src/Commands/Base/InvokeSPRestMethod.cs index 8ad164b6b..9ca54ee44 100644 --- a/src/Commands/Base/InvokeSPRestMethod.cs +++ b/src/Commands/Base/InvokeSPRestMethod.cs @@ -179,7 +179,7 @@ private void CallSingleRequest(HttpMethod method, string requestUrl) private void CallBatchRequest(HttpMethod method, string requestUrl) { - var web = PnPContext.Web; + var web = Connection.PnPContext.Web; string contentString = null; if (ParameterSpecified(nameof(Content))) { diff --git a/src/Commands/Branding/AddCustomAction.cs b/src/Commands/Branding/AddCustomAction.cs index 061b80d19..baf5997b4 100644 --- a/src/Commands/Branding/AddCustomAction.cs +++ b/src/Commands/Branding/AddCustomAction.cs @@ -1,8 +1,7 @@ -using System.Management.Automation; -using PnP.Framework.Entities; -using System; +using PnP.Core.Model.SharePoint; using PnP.PowerShell.Commands.Enums; -using PnP.Core.Model.SharePoint; +using System; +using System.Management.Automation; namespace PnP.PowerShell.Commands.Branding { @@ -120,12 +119,12 @@ protected override void ExecuteCmdlet() { case CustomActionScope.Web: { - PnPContext.Web.UserCustomActions.Add(ca); + Connection.PnPContext.Web.UserCustomActions.Add(ca); break; } case CustomActionScope.Site: { - PnPContext.Site.UserCustomActions.Add(ca); + Connection.PnPContext.Site.UserCustomActions.Add(ca); break; } case CustomActionScope.All: diff --git a/src/Commands/Fields/RemoveField.cs b/src/Commands/Fields/RemoveField.cs index ab2c7fde2..19d2ef916 100644 --- a/src/Commands/Fields/RemoveField.cs +++ b/src/Commands/Fields/RemoveField.cs @@ -1,11 +1,11 @@ -using System; -using System.Linq; -using System.Management.Automation; -using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Client; using PnP.Core.Model.SharePoint; using PnP.Core.QueryModel; using PnP.PowerShell.Commands.Base.PipeBinds; using PnP.PowerShell.Commands.Model; +using System; +using System.Linq; +using System.Management.Automation; namespace PnP.PowerShell.Commands.Fields { @@ -24,7 +24,7 @@ public class RemoveField : PnPWebCmdlet [Parameter(Mandatory = false)] public PnPBatch Batch; - + protected override void ExecuteCmdlet() { if (ParameterSpecified(nameof(Batch))) @@ -77,8 +77,9 @@ private void RemoveFieldBatch() else { var f = Identity.Field; - PnPContext.Web.EnsureProperties(w => w.Fields); - var fieldCollection = PnPContext.Web.Fields.AsRequested(); + var pnpWeb = Connection.PnPContext.Web; + pnpWeb.EnsureProperties(w => w.Fields); + var fieldCollection = pnpWeb.Fields.AsRequested(); IField pnpField = null; if (f == null) { diff --git a/src/Commands/Files/ConvertFile.cs b/src/Commands/Files/ConvertFile.cs index f5784db80..4a7f3397c 100644 --- a/src/Commands/Files/ConvertFile.cs +++ b/src/Commands/Files/ConvertFile.cs @@ -64,7 +64,7 @@ protected override void ExecuteCmdlet() serverRelativeUrl = Url; } - IFile sourceFile = PnPContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl, p => p.VroomDriveID, p => p.VroomItemID); + IFile sourceFile = Connection.PnPContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl, p => p.VroomDriveID, p => p.VroomItemID); WriteVerbose("Converting file to the specified format"); var convertedFile = sourceFile.ConvertTo(new ConvertToOptions { Format = ConvertToFormat }); diff --git a/src/Commands/Files/GetFile.cs b/src/Commands/Files/GetFile.cs index 1b5edbba8..316e86147 100644 --- a/src/Commands/Files/GetFile.cs +++ b/src/Commands/Files/GetFile.cs @@ -1,6 +1,5 @@ using Microsoft.SharePoint.Client; using PnP.Core.Model.SharePoint; -using PnP.Core.Services; using PnP.Framework.Utilities; using System.IO; using System.Management.Automation; @@ -86,7 +85,7 @@ protected override void ExecuteCmdlet() case URLTOPATH: // Get a reference to the file to download - IFile fileToDownload = PnPContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl); + IFile fileToDownload = Connection.PnPContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl); string fileToDownloadName = !string.IsNullOrEmpty(Filename) ? Filename : fileToDownload.Name; string fileOut = System.IO.Path.Combine(Path, fileToDownloadName); @@ -142,13 +141,13 @@ protected override void ExecuteCmdlet() try { - fileMemoryStream = PnPContext.Web.GetFileByServerRelativeUrl(ResourcePath.FromDecodedUrl(serverRelativeUrl).DecodedUrl, f => f.Author, f => f.Length, f => f.ModifiedBy, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title); + fileMemoryStream = Connection.PnPContext.Web.GetFileByServerRelativeUrl(ResourcePath.FromDecodedUrl(serverRelativeUrl).DecodedUrl, f => f.Author, f => f.Length, f => f.ModifiedBy, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title); } catch (ServerException) { // Assume the cause of the exception is that a principal cannot be found and try again without: // Fallback in case the creator or person having last modified the file no longer exists in the environment such that the file can still be downloaded - fileMemoryStream = PnPContext.Web.GetFileByServerRelativeUrl(ResourcePath.FromDecodedUrl(serverRelativeUrl).DecodedUrl, f => f.Length, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title); + fileMemoryStream = Connection.PnPContext.Web.GetFileByServerRelativeUrl(ResourcePath.FromDecodedUrl(serverRelativeUrl).DecodedUrl, f => f.Length, f => f.Name, f => f.TimeCreated, f => f.TimeLastModified, f => f.Title); } var stream = new System.IO.MemoryStream(fileMemoryStream.GetContentBytes()); @@ -175,7 +174,7 @@ private static async Task SaveFileToLocal(IFile fileToDownload, string filePath) content.Write(buffer, 0, read); } } - } + } } } } diff --git a/src/Commands/Files/GetFileAnalyticsData.cs b/src/Commands/Files/GetFileAnalyticsData.cs index 5366d04e5..f81187bcc 100644 --- a/src/Commands/Files/GetFileAnalyticsData.cs +++ b/src/Commands/Files/GetFileAnalyticsData.cs @@ -54,7 +54,7 @@ protected override void ExecuteCmdlet() serverRelativeUrl = Url; } - IFile analyticsFile = PnPContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl, p => p.VroomItemID, p => p.VroomDriveID); + IFile analyticsFile = Connection.PnPContext.Web.GetFileByServerRelativeUrl(serverRelativeUrl, p => p.VroomItemID, p => p.VroomDriveID); switch (ParameterSetName) { diff --git a/src/Commands/Files/GetFolderStorageMetric.cs b/src/Commands/Files/GetFolderStorageMetric.cs index 445bfaf26..556559cc7 100644 --- a/src/Commands/Files/GetFolderStorageMetric.cs +++ b/src/Commands/Files/GetFolderStorageMetric.cs @@ -1,16 +1,16 @@ -using System; -using System.Management.Automation; -using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Client; using PnP.Core.Model.SharePoint; using PnP.Framework.Utilities; using PnP.PowerShell.Commands.Base.PipeBinds; +using System; +using System.Management.Automation; namespace PnP.PowerShell.Commands.Files { [Cmdlet(VerbsCommon.Get, "PnPFolderStorageMetric", DefaultParameterSetName = ParameterSet_BYSITRERELATIVEURL)] [OutputType(typeof(Model.SharePoint.FolderStorageMetric))] public class GetFolderStorageMetric : PnPWebCmdlet - { + { private const string ParameterSet_BYSITRERELATIVEURL = "Folder via site relative URL"; private const string ParameterSet_BYLIST = "Folder via list"; private const string ParameterSet_BYFOLDER = "Folder via pipebind"; @@ -46,7 +46,7 @@ protected override void ExecuteCmdlet() string serverRelativeUrl = null; if (!string.IsNullOrEmpty(FolderSiteRelativeUrl)) { - if(FolderSiteRelativeUrl == "Microsoft.SharePoint.Client.Folder") + if (FolderSiteRelativeUrl == "Microsoft.SharePoint.Client.Folder") { throw new PSArgumentException($"Please pass in a Folder instance using the -{nameof(Identity)} parameter instead of piping it to this cmdlet"); } @@ -59,7 +59,7 @@ protected override void ExecuteCmdlet() targetFolder = (string.IsNullOrEmpty(FolderSiteRelativeUrl)) ? CurrentWeb.RootFolder : CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl)); break; - + default: throw new NotImplementedException($"Parameter set {ParameterSetName} not implemented"); } @@ -71,7 +71,7 @@ protected override void ExecuteCmdlet() ClientContext.Load(targetFolder, t => t.ServerRelativeUrl); ClientContext.ExecuteQueryRetry(); } - catch(Microsoft.SharePoint.Client.ServerException e) + catch (Microsoft.SharePoint.Client.ServerException e) { if (e.ServerErrorTypeName == "System.IO.FileNotFoundException") { @@ -83,7 +83,7 @@ protected override void ExecuteCmdlet() } } - IFolder folderWithStorageMetrics = PnPContext.Web.GetFolderByServerRelativeUrl(targetFolder.ServerRelativeUrl, f => f.StorageMetrics); + IFolder folderWithStorageMetrics = Connection.PnPContext.Web.GetFolderByServerRelativeUrl(targetFolder.ServerRelativeUrl, f => f.StorageMetrics); var storageMetrics = folderWithStorageMetrics.StorageMetrics; WriteObject(new Model.SharePoint.FolderStorageMetric @@ -98,6 +98,6 @@ protected override void ExecuteCmdlet() { throw new PSArgumentException($"Can't find the specified folder."); } - } + } } } \ No newline at end of file