Skip to content

Commit

Permalink
Refactor code to use Connection.PnPContext instead of PnPContext.Web (#…
Browse files Browse the repository at this point in the history
…4433)

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 12, 2024
1 parent 723897e commit e7a102d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Commands/Base/InvokeSPRestMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
{
Expand Down
11 changes: 5 additions & 6 deletions src/Commands/Branding/AddCustomAction.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 8 additions & 7 deletions src/Commands/Fields/RemoveField.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -24,7 +24,7 @@ public class RemoveField : PnPWebCmdlet

[Parameter(Mandatory = false)]
public PnPBatch Batch;

protected override void ExecuteCmdlet()
{
if (ParameterSpecified(nameof(Batch)))
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Files/ConvertFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
9 changes: 4 additions & 5 deletions src/Commands/Files/GetFile.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand All @@ -175,7 +174,7 @@ private static async Task SaveFileToLocal(IFile fileToDownload, string filePath)
content.Write(buffer, 0, read);
}
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Commands/Files/GetFileAnalyticsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
18 changes: 9 additions & 9 deletions src/Commands/Files/GetFolderStorageMetric.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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")
{
Expand All @@ -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
Expand All @@ -98,6 +98,6 @@ protected override void ExecuteCmdlet()
{
throw new PSArgumentException($"Can't find the specified folder.");
}
}
}
}
}

0 comments on commit e7a102d

Please sign in to comment.