Skip to content

Commit

Permalink
Added Rename methods on IFile to make it easier to rename a file #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenbe committed Feb 24, 2023
1 parent 5e5df4b commit 02fb506
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/using-the-sdk/files-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ testDocument["myPropertyKey"] = "Some value";
await testDocument.Properties.UpdateAsync();
```

## Renaming files

To rename a file you can either use the `Rename` methods or use the `MoveTo` methods.

```csharp
string documentUrl = $"{context.Uri.PathAndQuery}/Shared Documents/document.docx";

// Get a reference to the file, load the file property bag
IFile testDocument = await context.Web.GetFileByServerRelativeUrlAsync(documentUrl);

// Option A: Use the Rename methods
await testDocument.RenameAsync("renamed document.docx");

// Option B: Move the file to rename it
await testDocument.MoveToAsync($"{context.Uri.PathAndQuery}/Shared Documents/renamed document.docx");
```

## Publishing and un-publishing files

Publishing a file will move the file from draft into published status and increase it's major version by one. Publishing can be done using the [PublishAsync method](https://pnp.github.io/pnpcore/api/PnP.Core.Model.SharePoint.IFile.html#PnP_Core_Model_SharePoint_IFile_PublishAsync_System_String_), un-publishing a file will bring the file back to draft status and can be done using the [UnPublishAsync method](https://pnp.github.io/pnpcore/api/PnP.Core.Model.SharePoint.IFile.html#PnP_Core_Model_SharePoint_IFile_UnpublishAsync_System_String_).
Expand Down
1 change: 1 addition & 0 deletions src/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Support for reading and cloning News Digest pages #1086 [jansenbe - Bert Jansen]
- Add capabilities to grant or revoke permissions to existing sharing link #1094 [MathijsVerbeeck - Mathijs Verbeeck]
- Support for listing, approving and denying api access requests after installing an app #1100 [mloitzl - Martin Loitzl]
- Added `Rename` methods on `IFile` to make it easier to rename a file #1109 [jansenbe - Bert Jansen]

### Changed

Expand Down
35 changes: 35 additions & 0 deletions src/sdk/PnP.Core.Test/SharePoint/FilesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3497,5 +3497,40 @@ public async Task GetFilePreviewIncludingPageAndZoomAsyncTest()
}

#endregion

[TestMethod]
public async Task RenameFileTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IFile addedFile = null;
try
{
IFolder parentFolder = await context.Web.Folders.FirstOrDefaultAsync(f => f.Name == "SiteAssets");

string fileName = TestCommon.GetPnPSdkTestAssetName("test_added.docx");
addedFile = await parentFolder.Files.AddAsync(fileName, System.IO.File.OpenRead($".{Path.DirectorySeparatorChar}TestAssets{Path.DirectorySeparatorChar}test.docx"));

// Test the created object
Assert.IsNotNull(addedFile);
Assert.AreEqual(fileName, addedFile.Name);
Assert.AreNotEqual(default, addedFile.UniqueId);

// Rename file
addedFile.Rename("rename.docx");

// Get the file again
IFile foundDocument = await context.Web.GetFileByServerRelativeUrlAsync($"{parentFolder.ServerRelativeUrl}/rename.docx");

Assert.IsTrue(foundDocument != null);
}
finally
{
// Cleanup the added file
await addedFile.DeleteAsync();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"534b99a0-e0cf-6000-3b3d-96ec439d34ed","SPClientServiceRequestDuration":"10","X-SharePointHealthScore":"0","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RegionalSettings\u0022:{\u0022TimeZone\u0022:{\u0022Description\u0022:\u0022(UTC-08:00) Pacific Time (US and Canada)\u0022,\u0022Id\u0022:13,\u0022Information\u0022:{\u0022Bias\u0022:480,\u0022DaylightBias\u0022:-60,\u0022StandardBias\u0022:0}},\u0022AdjustHijriDays\u0022:0,\u0022AlternateCalendarType\u0022:0,\u0022AM\u0022:\u0022AM\u0022,\u0022CalendarType\u0022:1,\u0022Collation\u0022:25,\u0022CollationLCID\u0022:2070,\u0022DateFormat\u0022:0,\u0022DateSeparator\u0022:\u0022/\u0022,\u0022DecimalSeparator\u0022:\u0022.\u0022,\u0022DigitGrouping\u0022:\u00223;0\u0022,\u0022FirstDayOfWeek\u0022:0,\u0022FirstWeekOfYear\u0022:0,\u0022IsEastAsia\u0022:false,\u0022IsRightToLeft\u0022:false,\u0022IsUIRightToLeft\u0022:false,\u0022ListSeparator\u0022:\u0022,\u0022,\u0022LocaleId\u0022:1033,\u0022NegativeSign\u0022:\u0022-\u0022,\u0022NegNumberMode\u0022:1,\u0022PM\u0022:\u0022PM\u0022,\u0022PositiveSign\u0022:\u0022\u0022,\u0022ShowWeeks\u0022:false,\u0022ThousandSeparator\u0022:\u0022,\u0022,\u0022Time24\u0022:false,\u0022TimeMarkerPosition\u0022:0,\u0022TimeSeparator\u0022:\u0022:\u0022,\u0022WorkDayEndHour\u0022:1020,\u0022WorkDays\u0022:62,\u0022WorkDayStartHour\u0022:480},\u0022Id\u0022:\u00222c99a486-d6c9-4a4b-8d6f-a9faa364c92c\u0022,\u0022Url\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"534b99a0-00ef-6000-4823-8646013bfa88","SPClientServiceRequestDuration":"16","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022GroupId\u0022:\u0022d40d729b-df60-4b57-ac8f-102595090e0a\u0022,\u0022Id\u0022:\u0022f92f9e40-1110-43ef-aa0e-0822e13fb7ba\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"544b99a0-d065-6000-4823-87055992fa67","SPClientServiceRequestDuration":"18","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022value\u0022:[{\u0022Exists\u0022:true,\u0022IsWOPIEnabled\u0022:false,\u0022ItemCount\u0022:2,\u0022Name\u0022:\u0022SiteAssets\u0022,\u0022ProgID\u0022:null,\u0022ServerRelativeUrl\u0022:\u0022/sites/prov-2/SiteAssets\u0022,\u0022TimeCreated\u0022:\u00222021-10-04T07:41:22Z\u0022,\u0022TimeLastModified\u0022:\u00222023-02-23T19:33:19Z\u0022,\u0022UniqueId\u0022:\u002241f6e832-6aa3-41e1-9754-c56290074888\u0022,\u0022WelcomePage\u0022:\u0022\u0022}]}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"544b99a0-b0a9-6000-3b3d-9da3ad36ba16","SPClientServiceRequestDuration":"243","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022CheckInComment\u0022:\u0022\u0022,\u0022CheckOutType\u0022:2,\u0022ContentTag\u0022:\u0022{7F549591-DE9B-4818-98C1-8F15904260C4},1,2\u0022,\u0022CustomizedPageStatus\u0022:0,\u0022ETag\u0022:\u0022\\\u0022{7F549591-DE9B-4818-98C1-8F15904260C4},1\\\u0022\u0022,\u0022Exists\u0022:true,\u0022IrmEnabled\u0022:false,\u0022Length\u0022:\u002220161\u0022,\u0022Level\u0022:1,\u0022LinkingUri\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/SiteAssets/PNP_SDK_TEST_test_added.docx?d=w7f549591de9b481898c18f15904260c4\u0022,\u0022LinkingUrl\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/SiteAssets/PNP_SDK_TEST_test_added.docx?d=w7f549591de9b481898c18f15904260c4\u0022,\u0022MajorVersion\u0022:1,\u0022MinorVersion\u0022:0,\u0022Name\u0022:\u0022PNP_SDK_TEST_test_added.docx\u0022,\u0022ServerRelativeUrl\u0022:\u0022/sites/prov-2/SiteAssets/PNP_SDK_TEST_test_added.docx\u0022,\u0022TimeCreated\u0022:\u00222023-02-24T08:27:06Z\u0022,\u0022TimeLastModified\u0022:\u00222023-02-24T08:27:06Z\u0022,\u0022Title\u0022:\u0022\u0022,\u0022UIVersion\u0022:512,\u0022UIVersionLabel\u0022:\u00221.0\u0022,\u0022UniqueId\u0022:\u00227f549591-de9b-4818-98c1-8f15904260c4\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"554b99a0-a0f3-6000-4823-8fe268f73301","SPClientServiceRequestDuration":"211","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022UniqueId\u0022:\u00227f549591-de9b-4818-98c1-8f15904260c4\u0022,\u0022VroomDriveID\u0022:\u0022b!QJ4v-RAR70OqDggi4T-3uoakmSzJ1ktKjW-p-qNkySzw-nOsY6jmQaVlbJ9wXy2E\u0022,\u0022VroomItemID\u0022:\u002201O2ADSGMRSVKH7G66DBEJRQMPCWIEEYGE\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"Cache-Control":"no-store, no-cache","Transfer-Encoding":"chunked","Vary":"Accept-Encoding","Strict-Transport-Security":"max-age=31536000","request-id":"0ee28c0e-7d2a-4dee-bea9-10d36a90972a","client-request-id":"0ee28c0e-7d2a-4dee-bea9-10d36a90972a","x-ms-ags-diagnostic":"{\u0022ServerInfo\u0022:{\u0022DataCenter\u0022:\u0022West Europe\u0022,\u0022Slice\u0022:\u0022E\u0022,\u0022Ring\u0022:\u00225\u0022,\u0022ScaleUnit\u0022:\u0022004\u0022,\u0022RoleInstance\u0022:\u0022AM2PEPF0000BE68\u0022}}","OData-Version":"4.0","Date":"Fri, 24 Feb 2023 08:27:13 GMT"},"Response":"{\u0022@odata.context\u0022:\u0022https://graph.microsoft.com/v1.0/$metadata#sites(\u0027f92f9e40-1110-43ef-aa0e-0822e13fb7ba\u0027)/drives(\u0027b%21QJ4v-RAR70OqDggi4T-3uoakmSzJ1ktKjW-p-qNkySzw-nOsY6jmQaVlbJ9wXy2E\u0027)/items/$entity\u0022,\u0022@microsoft.graph.downloadUrl\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/_layouts/15/download.aspx?UniqueId=7f549591-de9b-4818-98c1-8f15904260c4\u0026Translate=false\u0026tempauth=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvYmVydG9ubGluZS5zaGFyZXBvaW50LmNvbUBkODYyM2M5ZS0zMGM3LTQ3M2EtODNiYy1kOTA3ZGY0NGEyNmUiLCJpc3MiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAiLCJuYmYiOiIxNjc3MjI3MjM0IiwiZXhwIjoiMTY3NzIzMDgzNCIsImVuZHBvaW50dXJsIjoiem85ZTFieEd6VkpHbXlmMEZLOUZGRU9WQThBTm9NU05FbVM5WkFVSWtBVT0iLCJlbmRwb2ludHVybExlbmd0aCI6IjEzNCIsImlzbG9vcGJhY2siOiJUcnVlIiwiY2lkIjoiTUdWbE1qaGpNR1V0TjJReVlTMDBaR1ZsTFdKbFlUa3RNVEJrTXpaaE9UQTVOekpoIiwidmVyIjoiaGFzaGVkcHJvb2Z0b2tlbiIsInNpdGVpZCI6Ilpqa3laamxsTkRBdE1URXhNQzAwTTJWbUxXRmhNR1V0TURneU1tVXhNMlppTjJKaCIsImFwcF9kaXNwbGF5bmFtZSI6IlBucENvcmVUZXN0QXBwIiwiZ2l2ZW5fbmFtZSI6IkJlcnQiLCJmYW1pbHlfbmFtZSI6IkphbnNlbiIsImFwcGlkIjoiYzU0NWY5Y2UtMWMxMS00NDBiLTgxMmItMGIzNTIxN2Q5ZTgzIiwidGlkIjoiZDg2MjNjOWUtMzBjNy00NzNhLTgzYmMtZDkwN2RmNDRhMjZlIiwidXBuIjoiYmVydC5qYW5zZW5AYmVydG9ubGluZS5vbm1pY3Jvc29mdC5jb20iLCJwdWlkIjoiMTAwMzAwMDA4NEU0RkY2MSIsImNhY2hla2V5IjoiMGguZnxtZW1iZXJzaGlwfDEwMDMwMDAwODRlNGZmNjFAbGl2ZS5jb20iLCJzY3AiOiJhbGxzaXRlcy5mdWxsY29udHJvbCBncm91cC53cml0ZSBzaGFyZXBvaW50dGVuYW50c2V0dGluZ3MucmVhZHdyaXRlLmFsbCBhbGxzaXRlcy5mdWxsY29udHJvbCBhbGxzaXRlcy5yZWFkIGFsbHByb2ZpbGVzLnJlYWQgdGVybXN0b3JlLndyaXRlIiwidHQiOiIyIiwidXNlUGVyc2lzdGVudENvb2tpZSI6bnVsbCwiaXBhZGRyIjoiNDAuMTI2LjMyLjE2MCJ9.RUVPeVBRRVFSaWNJbnpCWDdtc2ZoeGFOQUVkaW5Zc2pZaEZWQUl6WmZ5VT0\u0026ApiVersion=2.0\u0022,\u0022createdDateTime\u0022:\u00222023-02-24T08:27:06Z\u0022,\u0022eTag\u0022:\u0022\\\u0022{7F549591-DE9B-4818-98C1-8F15904260C4},3\\\u0022\u0022,\u0022id\u0022:\u002201O2ADSGMRSVKH7G66DBEJRQMPCWIEEYGE\u0022,\u0022lastModifiedDateTime\u0022:\u00222023-02-24T08:27:14Z\u0022,\u0022name\u0022:\u0022rename.docx\u0022,\u0022webUrl\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/_layouts/15/Doc.aspx?sourcedoc=%7B7F549591-DE9B-4818-98C1-8F15904260C4%7D\u0026file=rename.docx\u0026action=default\u0026mobileredirect=true\u0022,\u0022cTag\u0022:\u0022\\\u0022c:{7F549591-DE9B-4818-98C1-8F15904260C4},3\\\u0022\u0022,\u0022size\u0022:\u002220161\u0022,\u0022createdBy\u0022:{\u0022application\u0022:{\u0022id\u0022:\u0022c545f9ce-1c11-440b-812b-0b35217d9e83\u0022,\u0022displayName\u0022:\u0022PnpCoreTestApp\u0022},\u0022user\u0022:{\u0022email\u0022:\u0022bert.jansen@bertonline.onmicrosoft.com\u0022,\u0022id\u0022:\u002233aca310-a489-4121-b853-663d0327fe08\u0022,\u0022displayName\u0022:\u0022Bert Jansen (Cloud)\u0022}},\u0022lastModifiedBy\u0022:{\u0022application\u0022:{\u0022id\u0022:\u0022c545f9ce-1c11-440b-812b-0b35217d9e83\u0022,\u0022displayName\u0022:\u0022PnpCoreTestApp\u0022},\u0022user\u0022:{\u0022email\u0022:\u0022bert.jansen@bertonline.onmicrosoft.com\u0022,\u0022id\u0022:\u002233aca310-a489-4121-b853-663d0327fe08\u0022,\u0022displayName\u0022:\u0022Bert Jansen (Cloud)\u0022}},\u0022parentReference\u0022:{\u0022driveType\u0022:\u0022documentLibrary\u0022,\u0022driveId\u0022:\u0022b!QJ4v-RAR70OqDggi4T-3uoakmSzJ1ktKjW-p-qNkySzw-nOsY6jmQaVlbJ9wXy2E\u0022,\u0022id\u0022:\u002201O2ADSGN6Y2GOVW7725BZO354PWSELRRZ\u0022,\u0022path\u0022:\u0022/drives/b!QJ4v-RAR70OqDggi4T-3uoakmSzJ1ktKjW-p-qNkySzw-nOsY6jmQaVlbJ9wXy2E/root:\u0022},\u0022file\u0022:{\u0022mimeType\u0022:\u0022application/vnd.openxmlformats-officedocument.wordprocessingml.document\u0022,\u0022hashes\u0022:{\u0022quickXorHash\u0022:\u0022pBKjUBwpkixtiqTK87cZTDalPo8=\u0022}},\u0022fileSystemInfo\u0022:{\u0022createdDateTime\u0022:\u00222023-02-24T08:27:06Z\u0022,\u0022lastModifiedDateTime\u0022:\u00222023-02-24T08:27:14Z\u0022},\u0022shared\u0022:{\u0022scope\u0022:\u0022users\u0022}}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"594b99a0-b085-6000-4823-8fea9b83b98d","SPClientServiceRequestDuration":"13","X-SharePointHealthScore":"0","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022CheckInComment\u0022:\u0022\u0022,\u0022CheckOutType\u0022:2,\u0022ContentTag\u0022:\u0022{7F549591-DE9B-4818-98C1-8F15904260C4},3,3\u0022,\u0022CustomizedPageStatus\u0022:0,\u0022ETag\u0022:\u0022\\\u0022{7F549591-DE9B-4818-98C1-8F15904260C4},3\\\u0022\u0022,\u0022Exists\u0022:true,\u0022IrmEnabled\u0022:false,\u0022Length\u0022:\u002220161\u0022,\u0022Level\u0022:1,\u0022LinkingUri\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/SiteAssets/rename.docx?d=w7f549591de9b481898c18f15904260c4\u0022,\u0022LinkingUrl\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/SiteAssets/rename.docx?d=w7f549591de9b481898c18f15904260c4\u0022,\u0022MajorVersion\u0022:1,\u0022MinorVersion\u0022:0,\u0022Name\u0022:\u0022rename.docx\u0022,\u0022ServerRelativeUrl\u0022:\u0022/sites/prov-2/SiteAssets/rename.docx\u0022,\u0022TimeCreated\u0022:\u00222023-02-24T08:27:06Z\u0022,\u0022TimeLastModified\u0022:\u00222023-02-24T08:27:14Z\u0022,\u0022Title\u0022:\u0022\u0022,\u0022UIVersion\u0022:512,\u0022UIVersionLabel\u0022:\u00221.0\u0022,\u0022UniqueId\u0022:\u00227f549591-de9b-4818-98c1-8f15904260c4\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"5a4b99a0-a0fe-6000-4823-8e6d87622397","SPClientServiceRequestDuration":"180","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":""}
22 changes: 22 additions & 0 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Internal/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,28 @@ private static IFilePreview GetPreviewFromResponse(string json)

#endregion

#region Rename File
public async Task RenameAsync(string name)
{
await EnsurePropertiesAsync(y => y.VroomItemID, y => y.VroomDriveID).ConfigureAwait(false);

dynamic body = new
{
name
};

var apiCall = new ApiCall($"sites/{PnPContext.Site.Id}/drives/{VroomDriveID}/items/{VroomItemID}", ApiType.Graph, JsonSerializer.Serialize(body, PnPConstants.JsonSerializer_IgnoreNullValues));
await RequestAsync(apiCall, new HttpMethod("PATCH")).ConfigureAwait(false);
// Update the Name property without marking the file as changed
SetSystemValue(name, nameof(Name));
}

public void Rename(string name)
{
RenameAsync(name).GetAwaiter().GetResult();
}
#endregion

#endregion

#region Helper methods
Expand Down
16 changes: 16 additions & 0 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Public/IFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,5 +877,21 @@ public interface IFile : IDataModel<IFile>, IDataModelGet<IFile>, IDataModelLoad
IFilePreview GetPreview(PreviewOptions options = null);

#endregion

#region Rename
/// <summary>
/// Renames a file
/// </summary>
/// <param name="name">New file name</param>
/// <returns></returns>
public Task RenameAsync(string name);

/// <summary>
/// Renames a file
/// </summary>
/// <param name="name">New file name</param>
/// <returns></returns>
public void Rename(string name);
#endregion
}
}

0 comments on commit 02fb506

Please sign in to comment.