Skip to content

Commit

Permalink
Merge pull request #2888 from ChrisRo89/MoveFileWarning
Browse files Browse the repository at this point in the history
Added warning in the catch block
  • Loading branch information
KoenZomers authored Mar 20, 2023
2 parents 0a71b93 + aae2401 commit 4c9962b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `-OpenDocumentsMode` option to `Set-PnPList` which allows configuring if documents should be opened in the browser or in the local client [#2873](https://github.com/pnp/powershell/pull/2873)
- Added `-Properties` parameter to `Get-PnPUserProfileProperty` cmdlet which allows retrieval of specific properties if specified. [#2840](https://github.com/pnp/powershell/pull/2840)
- Added support for specifying the `-ContentUrl` configuration in `Add-PnPTeamsTab` cmdlet when trying to add a Planner as a tab in Teams channel. [#2850](https://github.com/pnp/powershell/pull/2850)
- Added support for `-Verbose` in `Move-PnPFile` which will show if it has problems determining if the destination location is a folder or a file [#2888](https://github.com/pnp/powershell/pull/2888)

### Changed

Expand Down Expand Up @@ -95,6 +96,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Contributors

- Chris R. [ChrisRo89]
- Aimery Thomas [a1mery]
- Ganesh Sanap [ganesh-sanap]
- Markus Hanisch [m-hanisch]
Expand Down
19 changes: 16 additions & 3 deletions documentation/Move-PnPFile.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Moves a file or folder to a different location
## SYNTAX

```powershell
Move-PnPFile [-SourceUrl] <String> [-TargetUrl] <String> [-Overwrite] [-AllowSchemaMismatch] [-AllowSmallerVersionLimitOnDestination] [-IgnoreVersionHistory] [-NoWait] [-Force] [-Connection <PnPConnection>]
Move-PnPFile [-SourceUrl] <String> [-TargetUrl] <String> [-Overwrite] [-AllowSchemaMismatch] [-AllowSmallerVersionLimitOnDestination] [-IgnoreVersionHistory] [-NoWait] [-Force] [-Connection <PnPConnection>] [-Verbose]
```

## DESCRIPTION
Expand Down Expand Up @@ -193,7 +193,20 @@ Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
### -Verbose
When provided, additional debug statements might be shown while executing the cmdlet.
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
```yaml
Type: SwitchParameter
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
15 changes: 8 additions & 7 deletions src/Commands/Files/MoveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MoveFile : PnPWebCmdlet
private const string ParameterSet_SERVER = "Server Relative";
private const string ParameterSet_SITE = "Site Relative";
private const string ParameterSet_OTHERSITE = "Other Site Collection";

[Parameter(Mandatory = true)]
[Alias("ServerRelativeUrl", "SiteRelativeUrl")]
public string SourceUrl = string.Empty;
Expand All @@ -38,7 +38,7 @@ public class MoveFile : PnPWebCmdlet
[Parameter(Mandatory = false)]
public SwitchParameter Force;

[Parameter(Mandatory = false)]
[Parameter(Mandatory = false)]
public SwitchParameter NoWait;

protected override void ExecuteCmdlet()
Expand All @@ -64,9 +64,9 @@ protected override void ExecuteCmdlet()

Uri currentContextUri = new Uri(ClientContext.Url);
Uri sourceUri = new Uri(currentContextUri, EncodePath(sourceFolder));
Uri sourceWebUri = Microsoft.SharePoint.Client.Web.WebUrlFromFolderUrlDirect(ClientContext, sourceUri);
Uri sourceWebUri = Web.WebUrlFromFolderUrlDirect(ClientContext, sourceUri);
Uri targetUri = new Uri(currentContextUri, EncodePath(targetFolder));
Uri targetWebUri = Microsoft.SharePoint.Client.Web.WebUrlFromFolderUrlDirect(ClientContext, targetUri);
Uri targetWebUri = Web.WebUrlFromFolderUrlDirect(ClientContext, targetUri);

if (Force || ShouldContinue(string.Format(Resources.MoveFile0To1, SourceUrl, TargetUrl), Resources.Confirm))
{
Expand All @@ -83,23 +83,24 @@ protected override void ExecuteCmdlet()
var folderServerRelativePath = folder.EnsureProperty(f => f.ServerRelativePath);
isFolder = folderServerRelativePath.DecodedUrl == ResourcePath.FromDecodedUrl(TargetUrl).DecodedUrl;
}
catch
catch (Exception ex)
{
WriteVerbose($"Error occurred while trying to check if the target URL {TargetUrl} is a folder. This could happen if the target folder does not exist yet. It may still work well. Exception: {ex.Message}");
}
if (isFolder)
{
WriteVerbose($"Moving file or folder from {sourceUri} to {targetUri}");
Move(currentContextUri, sourceUri, targetUri, SourceUrl, TargetUrl, true);
}
else
{

WriteVerbose($"Moving file or folder from {SourceUrl} to {TargetUrl}");
var file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(SourceUrl));
file.MoveToUsingPath(ResourcePath.FromDecodedUrl(TargetUrl), Overwrite ? MoveOperations.Overwrite : MoveOperations.None);
ClientContext.ExecuteQueryRetry();
}
}
}

}

private string EncodePath(string path)
Expand Down

0 comments on commit 4c9962b

Please sign in to comment.