Skip to content

Commit

Permalink
Refactor File cmdlets to use PnP Core SDK, improved user profile cmdl…
Browse files Browse the repository at this point in the history
…ets connections (#4388)

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 5, 2024
1 parent 637d807 commit 32677fd
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- `New-PnPContainerType` will temporarily disable standard containers to be created due to changed being applied at Microsoft to allow this to be possible in the future [#4125](https://github.com/pnp/powershell/pull/4125)
- Renamed `Get-PnPLabel` cmdlet to `Get-PnPRetentionLabel`. [#4387](https://github.com/pnp/powershell/pull/4387)
- `Add-PnPNavigationNode` cmdlet updated to now support `-OpenInNewTab` parameter for different types of navigation. [#3969](https://github.com/pnp/powershell/pull/3969)
- `Remove-PnPFile` , `Rename-PnPFile`, `Set-PnPFileCheckedIn`, `Set-PnPFileCheckedOut` & `Undo-PnPFileCheckedOut` cmdlets now use PnP Core SDK behind the scenes.
- `Set-PnPFileCheckedIn` cmdlet now expects `CheckInType` to be of type `PnP.Core.Model.SharePoint.CheckinType` instead of the earlier one based on CSOM.

### Fixed

Expand All @@ -42,6 +44,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fix `Get-PnPSiteTemplate -PersistMultiLanguageResources` not working correctly for xml file types. [#4326](https://github.com/pnp/powershell/pull/4326)
- Fix `Add-PnPDataRowsToSiteTemplate` setting keyColumn to null when not passed. [#4325](https://github.com/pnp/powershell/pull/4325)
- Fix `Connect-PnPOnline` not working correctly when `-DeviceLogin` and `-LaunchBrowser` both are specified. It used to open it in a popup. Now it correctly launches the browser. [#4325](https://github.com/pnp/powershell/pull/4345)
- `Export-PnPUserInfo`, `Export-PnPUserProfile` and `Remove-PnPUserProfile` cmdlets now work properly with proper `-Connection` parameter if specified.

### Removed

Expand Down
20 changes: 10 additions & 10 deletions src/Commands/Files/RemoveFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.SharePoint.Client;
using PnP.Core.Model.SharePoint;
using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Model.SharePoint;
using System.Management.Automation;
Expand Down Expand Up @@ -30,25 +30,25 @@ protected override void ExecuteCmdlet()
{
if (ParameterSpecified(nameof(SiteRelativeUrl)))
{
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
ServerRelativeUrl = UrlUtility.Combine(webUrl, SiteRelativeUrl);
var pnpWeb = Connection.PnPContext.Web;
pnpWeb.EnsureProperties(w => w.ServerRelativeUrl);

ServerRelativeUrl = UrlUtility.Combine(pnpWeb.ServerRelativeUrl, SiteRelativeUrl);
}
var file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(ServerRelativeUrl));
ClientContext.Load(file, f => f.Name);
ClientContext.ExecuteQueryRetry();

IFile file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(ServerRelativeUrl);
file.EnsureProperties(f => f.Name);

if (Force || ShouldContinue(string.Format(Resources.Delete0, file.Name), Resources.Confirm))
{
if (Recycle)
{
var recycleResult = file.Recycle();
ClientContext.ExecuteQueryRetry();
WriteObject(new RecycleResult { RecycleBinItemId = recycleResult.Value });
WriteObject(new RecycleResult { RecycleBinItemId = recycleResult });
}
else
{
file.DeleteObject();
ClientContext.ExecuteQueryRetry();
file.Delete();
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/Commands/Files/RenameFile.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using Resources = PnP.PowerShell.Commands.Properties.Resources;
using PnP.Framework.Utilities;
using PnP.Core.Model.SharePoint;

namespace PnP.PowerShell.Commands.Files
{
Expand All @@ -28,20 +27,20 @@ protected override void ExecuteCmdlet()
{
if (ParameterSetName == "SITE")
{
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
ServerRelativeUrl = UrlUtility.Combine(webUrl, SiteRelativeUrl);
var pnpWeb = Connection.PnPContext.Web;
pnpWeb.EnsureProperties(w => w.ServerRelativeUrl);

ServerRelativeUrl = UrlUtility.Combine(pnpWeb.ServerRelativeUrl, SiteRelativeUrl);
}
var file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(ServerRelativeUrl));

ClientContext.Load(file, f => f.Name, f => f.ServerRelativePath);
ClientContext.ExecuteQueryRetry();
IFile file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(ServerRelativeUrl);
file.EnsureProperties(f => f.Name, f => f.ServerRelativeUrl);

if (Force || ShouldContinue(string.Format(Resources.RenameFile0To1, file.Name, TargetFileName), Resources.Confirm))
{
var targetPath = string.Concat(file.ServerRelativePath.DecodedUrl.Remove(file.ServerRelativePath.DecodedUrl.Length - file.Name.Length), TargetFileName);
file.MoveToUsingPath(ResourcePath.FromDecodedUrl(targetPath), OverwriteIfAlreadyExists ? MoveOperations.Overwrite : MoveOperations.None);
var targetPath = string.Concat(file.ServerRelativeUrl.Remove(file.ServerRelativeUrl.Length - file.Name.Length), TargetFileName);

ClientContext.ExecuteQueryRetry();
file.MoveTo(targetPath, OverwriteIfAlreadyExists ? MoveOperations.Overwrite : MoveOperations.None);
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/Commands/Files/SetFileCheckedIn.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.Core.Model.SharePoint;
using CheckinType = PnP.Core.Model.SharePoint.CheckinType;

namespace PnP.PowerShell.Commands.Files
{
[Cmdlet(VerbsCommon.Set, "PnPFileCheckedIn")]
public class SetFileCheckedIn : PnPWebCmdlet
{
[Parameter(Mandatory = true, Position=0, ValueFromPipeline=true)]
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public string Url = string.Empty;

[Parameter(Mandatory = false)]
Expand All @@ -23,10 +23,15 @@ protected override void ExecuteCmdlet()
{
// Remove URL decoding from the Url as that will not work. We will encode the + character specifically, because if that is part of the filename, it needs to stay and not be decoded.
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));

CurrentWeb.CheckInFile(Url, CheckinType, Comment);

IFile file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(Url);

file.Checkin(Comment, CheckinType);

if (Approve)
CurrentWeb.ApproveFile(Url, Comment);
{
file.Approve(Comment);
}
}
}
}
8 changes: 4 additions & 4 deletions src/Commands/Files/SetFileCheckedOut.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.Core.Model.SharePoint;

namespace PnP.PowerShell.Commands.Files
{
[Cmdlet(VerbsCommon.Set, "PnPFileCheckedOut")]
public class SetFileCheckedOut : PnPWebCmdlet
{
[Parameter(Mandatory = true, Position=0, ValueFromPipeline=true)]
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public string Url = string.Empty;

protected override void ExecuteCmdlet()
{
// Remove URL decoding from the Url as that will not work. We will encode the + character specifically, because if that is part of the filename, it needs to stay and not be decoded.
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));

CurrentWeb.CheckOutFile(Url);
IFile file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(Url);
file.Checkout();
}
}
}
7 changes: 4 additions & 3 deletions src/Commands/Files/UndoFileCheckedOut.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.Core.Model.SharePoint;

namespace PnP.PowerShell.Commands.Files
{
Expand All @@ -15,7 +14,9 @@ protected override void ExecuteCmdlet()
// Remove URL decoding from the Url as that will not work. We will encode the + character specifically, because if that is part of the filename, it needs to stay and not be decoded.
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));

CurrentWeb.UndoCheckOutFileAsync(Url);
IFile file = Connection.PnPContext.Web.GetFileByServerRelativeUrl(Url);
file.UndoCheckout();

}
}
}
2 changes: 1 addition & 1 deletion src/Commands/Principals/ExportUserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override void ExecuteCmdlet()
AdminContext.Load(site);
AdminContext.ExecuteQueryRetry();
var normalizedUserName = UrlUtilities.UrlEncode($"i:0#.f|membership|{LoginName}");
var results = RestHelper.Get<RestResultCollection<ExportEntity>>(this.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/GetSPUserInformation(accountName=@a,siteId=@b)?@a='{normalizedUserName}'&@b='{site.Id}'", AdminContext, false);
var results = RestHelper.Get<RestResultCollection<ExportEntity>>(Connection.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/GetSPUserInformation(accountName=@a,siteId=@b)?@a='{normalizedUserName}'&@b='{site.Id}'", AdminContext, false);
var record = new PSObject();
foreach (var item in results.Items)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/UserProfiles/ExportUserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override void ExecuteCmdlet()
hostUrl = hostUrl.Substring(0, hostUrl.Length - 1);
}
var normalizedUserName = UrlUtilities.UrlEncode($"i:0#.f|membership|{LoginName}");
var results = RestHelper.Get<RestResultCollection<ExportEntity>>(this.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/GetUserProfileProperties(accountName=@a)?@a='{normalizedUserName}'", AdminContext, false);
var results = RestHelper.Get<RestResultCollection<ExportEntity>>(Connection.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/GetUserProfileProperties(accountName=@a)?@a='{normalizedUserName}'", AdminContext, false);
var record = new PSObject();
foreach (var item in results.Items)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/UserProfiles/RemoveUserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ protected override void ExecuteCmdlet()

if (!ParameterSpecified(nameof(UserId)))
{
RestHelper.Post(this.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/HardDeleteUserProfile(accountName=@a)?@a='{normalizedUserName}'", AdminContext);
RestHelper.Post(Connection.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/HardDeleteUserProfile(accountName=@a)?@a='{normalizedUserName}'", AdminContext);
}
else
{
RestHelper.Post(this.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/HardDeleteUserProfile(accountName=@a,userId='{UserId}')?@a='{normalizedUserName}'", AdminContext);
RestHelper.Post(Connection.HttpClient, $"{hostUrl}/_api/sp.userprofiles.peoplemanager/HardDeleteUserProfile(accountName=@a,userId='{UserId}')?@a='{normalizedUserName}'", AdminContext);
}

WriteVerbose($"Completed deletion of user profile {LoginName}");
Expand Down

0 comments on commit 32677fd

Please sign in to comment.