Skip to content

Commit

Permalink
Added RenameFolder methods on IFolder to make it easier to rename…
Browse files Browse the repository at this point in the history
… a folder #1080
  • Loading branch information
jansenbe committed Jan 27, 2023
1 parent c763f6b commit 3a6d2ac
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Bulk user validation method `ValidateUsersAsync` and `ValidateAndEnsureUsersAsync` on `IWeb` [jansenbe - Bert Jansen]
- When using the `GetSiteCollectionsWithDetailsAsync` method you now can choose to also return the site collections linked to private and shared Teams channels #1076 [jansenbe - Bert Jansen]
- Fix URL encoding for use in SPO REST ...Path methods (e.g. `getFolderByServerRelativePath`) #1077 [jansenbe - Bert Jansen]
- Added `RenameFolder` methods on `IFolder` to make it easier to rename a folder #1080 [jansenbe - Bert Jansen]

### Changed

Expand Down
32 changes: 32 additions & 0 deletions src/sdk/PnP.Core.Test/SharePoint/FoldersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,38 @@ public async Task EnsureListFolderInExistingHiarchyTest()
}
#endregion

[TestMethod]
public async Task RenameFolderTest()
{
//TestCommon.Instance.Mocking = false;
using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
IFolder addedFolder = null;
try
{
IFolder parentFolder = (await context.Web.Lists.GetByTitleAsync("Site Pages", p => p.RootFolder)).RootFolder;

addedFolder = await parentFolder.EnsureFolderAsync("sub1");
Assert.IsTrue(addedFolder != null);
Assert.IsTrue(addedFolder.Name == "sub1");

// rename the added folder
addedFolder.RenameFolder("newsub1");

Assert.IsTrue(addedFolder.Name == "newsub1");

// Get the folder again
IFolder addedFolder2 = await context.Web.GetFolderByIdAsync(addedFolder.UniqueId);

Assert.IsTrue(addedFolder2.Name == addedFolder.Name);
}
finally
{
await addedFolder.DeleteAsync();
}
}
}

[TestMethod]
public async Task UpdateFolderTest()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"ae6d90a0-c034-6000-1002-9f592fc18612","SPClientServiceRequestDuration":"155","X-SharePointHealthScore":"2","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":"ae6d90a0-7053-6000-1002-90f98bf4fbdc","SPClientServiceRequestDuration":"9","X-SharePointHealthScore":"0","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":"ae6d90a0-505a-6000-1002-921ef828acbb","SPClientServiceRequestDuration":"13","X-SharePointHealthScore":"1","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022RootFolder\u0022:{\u0022Exists\u0022:true,\u0022IsWOPIEnabled\u0022:false,\u0022ItemCount\u0022:2,\u0022Name\u0022:\u0022SitePages\u0022,\u0022ProgID\u0022:null,\u0022ServerRelativeUrl\u0022:\u0022/sites/prov-2/SitePages\u0022,\u0022TimeCreated\u0022:\u00222021-09-11T23:42:33Z\u0022,\u0022TimeLastModified\u0022:\u00222023-01-27T19:19:08Z\u0022,\u0022UniqueId\u0022:\u0022714761dc-83f7-428a-ae9b-7801d9c4d235\u0022,\u0022WelcomePage\u0022:\u0022\u0022},\u0022Id\u0022:\u0022874451ce-e1b4-4c46-85fd-a6fb0298e1a0\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":false,"StatusCode":404,"Headers":{"SPRequestGuid":"ae6d90a0-d062-6000-1002-91e64acc499e","SPClientServiceRequestDuration":"17","X-SharePointHealthScore":"0","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022odata.error\u0022:{\u0022code\u0022:\u0022-2147024894, System.IO.FileNotFoundException\u0022,\u0022message\u0022:{\u0022lang\u0022:\u0022en-US\u0022,\u0022value\u0022:\u0022File Not Found.\u0022}}}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"ae6d90a0-107a-6000-1002-92462dd9cd0f","SPClientServiceRequestDuration":"57","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Exists\u0022:true,\u0022IsWOPIEnabled\u0022:false,\u0022ItemCount\u0022:0,\u0022Name\u0022:\u0022sub1\u0022,\u0022ProgID\u0022:null,\u0022ServerRelativeUrl\u0022:\u0022/sites/prov-2/SitePages/sub1\u0022,\u0022TimeCreated\u0022:\u00222023-01-27T19:22:06Z\u0022,\u0022TimeLastModified\u0022:\u00222023-01-27T19:22:06Z\u0022,\u0022UniqueId\u0022:\u0022fdfe1e85-4689-4602-8b3d-ec785effe137\u0022,\u0022WelcomePage\u0022:\u0022\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":"5e8b4976-d39a-49d6-9356-4287b553636e","client-request-id":"5e8b4976-d39a-49d6-9356-4287b553636e","x-ms-ags-diagnostic":"{\u0022ServerInfo\u0022:{\u0022DataCenter\u0022:\u0022North Europe\u0022,\u0022Slice\u0022:\u0022E\u0022,\u0022Ring\u0022:\u00224\u0022,\u0022ScaleUnit\u0022:\u0022000\u0022,\u0022RoleInstance\u0022:\u0022DU6PEPF00002DB7\u0022}}","OData-Version":"4.0","Date":"Fri, 27 Jan 2023 19:22:10 GMT"},"Response":"{\[email protected]\u0022:\u0022https://graph.microsoft.com/v1.0/$metadata#sites(\u0027f92f9e40-1110-43ef-aa0e-0822e13fb7ba\u0027)/drives(\u0027b%21QJ4v-RAR70OqDggi4T-3uoakmSzJ1ktKjW-p-qNkySzOUUSHtOFGTIX9pvsCmOGg\u0027)/items/$entity\u0022,\u0022createdDateTime\u0022:\u00222023-01-27T19:22:06Z\u0022,\u0022eTag\u0022:\u0022\\\u0022{FDFE1E85-4689-4602-8B3D-EC785EFFE137},2\\\u0022\u0022,\u0022id\u0022:\u002201O2ADSGMFD37P3CKGAJDIWPPMPBPP7YJX\u0022,\u0022lastModifiedDateTime\u0022:\u00222023-01-27T19:22:11Z\u0022,\u0022name\u0022:\u0022newsub1\u0022,\u0022webUrl\u0022:\u0022https://bertonline.sharepoint.com/sites/prov-2/SitePages/newsub1\u0022,\u0022cTag\u0022:\u0022\\\u0022c:{FDFE1E85-4689-4602-8B3D-EC785EFFE137},0\\\u0022\u0022,\u0022size\u0022:\u00220\u0022,\u0022createdBy\u0022:{\u0022application\u0022:{\u0022id\u0022:\u0022c545f9ce-1c11-440b-812b-0b35217d9e83\u0022,\u0022displayName\u0022:\u0022PnpCoreTestApp\u0022},\u0022user\u0022:{\u0022email\u0022:\[email protected]\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:\[email protected]\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-qNkySzOUUSHtOFGTIX9pvsCmOGg\u0022,\u0022id\u0022:\u002201O2ADSGN6Y2GOVW7725BZO354PWSELRRZ\u0022,\u0022path\u0022:\u0022/drives/b!QJ4v-RAR70OqDggi4T-3uoakmSzJ1ktKjW-p-qNkySzOUUSHtOFGTIX9pvsCmOGg/root:\u0022},\u0022fileSystemInfo\u0022:{\u0022createdDateTime\u0022:\u00222023-01-27T19:22:06Z\u0022,\u0022lastModifiedDateTime\u0022:\u00222023-01-27T19:22:11Z\u0022},\u0022folder\u0022:{\u0022childCount\u0022:0}}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"b26d90a0-5011-6000-2d7c-0f79af7cdcce","SPClientServiceRequestDuration":"12","X-SharePointHealthScore":"2","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":"{\u0022Exists\u0022:true,\u0022IsWOPIEnabled\u0022:false,\u0022ItemCount\u0022:0,\u0022Name\u0022:\u0022newsub1\u0022,\u0022ProgID\u0022:null,\u0022ServerRelativeUrl\u0022:\u0022/sites/prov-2/SitePages/newsub1\u0022,\u0022TimeCreated\u0022:\u00222023-01-27T19:22:06Z\u0022,\u0022TimeLastModified\u0022:\u00222023-01-27T19:22:11Z\u0022,\u0022UniqueId\u0022:\u0022fdfe1e85-4689-4602-8b3d-ec785effe137\u0022,\u0022WelcomePage\u0022:\u0022\u0022}"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IsSuccessStatusCode":true,"StatusCode":200,"Headers":{"SPRequestGuid":"b26d90a0-101a-6000-1002-9c343df36bd7","SPClientServiceRequestDuration":"136","X-SharePointHealthScore":"3","X-SP-SERVERSTATE":"ReadOnly=0"},"Response":""}
22 changes: 22 additions & 0 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Internal/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,28 @@ public IFolder EnsureFolder(string folderRelativeUrl)
}
#endregion

#region Rename folder
public async Task RenameFolderAsync(string name)
{
var (driveId, driveItemId) = await GetGraphIdsAsync().ConfigureAwait(false);

dynamic body = new
{
name
};

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

public void RenameFolder(string name)
{
RenameFolderAsync(name).GetAwaiter().GetResult();
}
#endregion

#region Get Changes

public async Task<IList<IChange>> GetChangesAsync(ChangeQueryOptions query)
Expand Down
16 changes: 16 additions & 0 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Public/IFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ public interface IFolder : IDataModel<IFolder>, IDataModelGet<IFolder>, IDataMod
public IFolder EnsureFolder(string folderRelativeUrl);
#endregion

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

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

#region Syntex support
/// <summary>
/// Classifies and extracts all unprocessed files in this folder and it's sub folders via the Syntex off-peak queue
Expand Down

0 comments on commit 3a6d2ac

Please sign in to comment.