Skip to content

Commit

Permalink
Merge of #1146. Thanks @mloitzl
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenbe committed Mar 27, 2023
1 parent 8490f63 commit c26a882
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
38 changes: 38 additions & 0 deletions docs/using-the-sdk/listitems-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,44 @@ foreach (var listItem in myList.Items.AsRequested())
await context.ExecuteAsync();
```

## Adding a list folder

To add a folder to a list the list first must be configured to allow content types (`ContentTypesEnabled`) and allow folders (`EnableFolderCreation`). Once that's done use one of the `AddListFolder` methods to add a folder.

``` csharp
list.ContentTypesEnabled = true;
list.EnableFolderCreation = true;
await list.UpdateAsync();

// Option A: Add folder Test
await list.AddListFolderAsync("Test");


// Option B: Create path 'folderA/subfolderA'
string path = new[] {"folderA", "subfolderA" }.Aggregate(
"",
(aggregate, element) =>
{
IListItem addedFolder = list.AddListFolder(element, aggregate);
return $"{aggregate}/{element}";
}
);
```

## Moving a list item

You can move a list item to another folder inside it's list using one of the `MoveTo` methods:

```csharp
var myList = await context.Web.Lists.GetByTitleAsync("My List");

// Load list item with id 1
var first = await myList.Items.GetByIdAsync(1, li => li.All, li => li.Versions);

// Move to folder folderA/subfolderA inside this list
await first.MoveToAsync("folderA/subfolderA");
```

## Sharing a list item

A list can be shared with your organization, with specific users or with everyone (anonymous), obviously all depending on how the sharing configuration of your tenant and site collection. Check out the [PnP Core SDK Sharing APIs](./sharing-listitems.md) to learn more on how you can share a list item.
Expand Down
1 change: 1 addition & 0 deletions src/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Method to ensure the 'Everyone except external users' user for any site language #1127 [plamber - Patrick Lamber]
- Added support for listing, adding and revoking permission grants for a SharePoint service principal #1132 [mloitzl - Martin Loitzl]
- Admin library: added support for listing Azure ACS principals and SharePoint AddIns [jansenbe - Bert Jansen]
- Support for moving a ListItem to a sub folder #1146 [mloitzl - Martin Loitzl]

### Changed

Expand Down
16 changes: 8 additions & 8 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Internal/ListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public async Task<bool> IsFileAsync()
{
if (!Values.ContainsKey("ContentTypeId"))
{
await LoadKeyListItemProperties().ConfigureAwait(false);
await LoadKeyListItemPropertiesAsync().ConfigureAwait(false);
}

return Values["ContentTypeId"].ToString().StartsWith("0x0101", StringComparison.InvariantCultureIgnoreCase);
Expand All @@ -340,7 +340,7 @@ public async Task<bool> IsFolderAsync()
{
if (!Values.ContainsKey("ContentTypeId"))
{
await LoadKeyListItemProperties().ConfigureAwait(false);
await LoadKeyListItemPropertiesAsync().ConfigureAwait(false);
}

return Values["ContentTypeId"].ToString().StartsWith("0x0120", StringComparison.InvariantCultureIgnoreCase);
Expand All @@ -355,7 +355,7 @@ public async Task<IFolder> GetParentFolderAsync()
{
if (!Values.ContainsKey("FileDirRef"))
{
await LoadKeyListItemProperties().ConfigureAwait(false);
await LoadKeyListItemPropertiesAsync().ConfigureAwait(false);
}

return await PnPContext.Web.GetFolderByServerRelativeUrlAsync(Values["FileDirRef"].ToString()).ConfigureAwait(false);
Expand Down Expand Up @@ -394,15 +394,15 @@ await EnsurePropertiesAsync(item =>

if (!Values.ContainsKey("FileRef"))
{
LoadKeyListItemProperties().GetAwaiter().GetResult();
await LoadKeyListItemPropertiesAsync().ConfigureAwait(false);
}

var filename = Path.GetFileName(Values["FileRef"].ToString());

string destinationUrl =
$"{UrlUtility.EnsureAbsoluteUrl(PnPContext.Uri, UrlUtility.EnsureTrailingSlash(folder.ServerRelativeUrl))}{filename}";

ApiCall apiCall = GetMoveToApiCall(destinationUrl);
ApiCall apiCall = await GetMoveToApiCallAsync(destinationUrl).ConfigureAwait(false);
await RawRequestAsync(apiCall, HttpMethod.Post).ConfigureAwait(false);
}

Expand All @@ -411,15 +411,15 @@ public void MoveTo(string destinationFolderUrl)
MoveToAsync(destinationFolderUrl).GetAwaiter().GetResult();
}

private ApiCall GetMoveToApiCall(string destinationUrl)
private async Task<ApiCall> GetMoveToApiCallAsync(string destinationUrl)
{
MoveCopyOptions options = new();

string destUrl = UrlUtility.EnsureAbsoluteUrl(PnPContext.Uri, destinationUrl).ToString();

if (!Values.ContainsKey("FileRef"))
{
LoadKeyListItemProperties().GetAwaiter().GetResult();
await LoadKeyListItemPropertiesAsync().ConfigureAwait(false);
}

string srcUrl = UrlUtility.EnsureAbsoluteUrl(PnPContext.Uri, Values["FileRef"].ToString()).ToString();
Expand All @@ -444,7 +444,7 @@ private ApiCall GetMoveToApiCall(string destinationUrl)
return new ApiCall(copyToEndpointUrl, ApiType.SPORest, body);
}

private async Task LoadKeyListItemProperties()
private async Task LoadKeyListItemPropertiesAsync()
{
ApiCall apiCall = new ApiCall($"{GetItemUri()}?$select=ContentTypeId,FileDirRef,FileRef", ApiType.SPORest);
await RequestAsync(apiCall, HttpMethod.Get).ConfigureAwait(false);
Expand Down
8 changes: 4 additions & 4 deletions src/sdk/PnP.Core/Model/SharePoint/Core/Public/IListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ public interface IListItem : IDataModel<IListItem>, IDataModelGet<IListItem>, ID
#region MoveTo

/// <summary>
/// Moves ListItem to the destination folder URL.
/// Moves this ListItem to another folder in this list
/// </summary>
/// <param name="destinationFolderUrl">folder path within the list, e.g. 'subfolder1/subfolder2'</param>
/// <param name="destinationFolderUrl">Folder path within this list, e.g. 'subfolder1/subfolder2'</param>
Task MoveToAsync(string destinationFolderUrl);

/// <summary>
/// Moves ListItem to the destination folder Folder.
/// Moves this ListItem to another folder in this list
/// </summary>
/// <param name="destinationFolderUrl">folder path within the list, e.g. 'subfolder1/subfolder2'</param>
/// <param name="destinationFolderUrl">Folder path within this list, e.g. 'subfolder1/subfolder2'</param>
void MoveTo(string destinationFolderUrl);

#endregion
Expand Down

0 comments on commit c26a882

Please sign in to comment.