diff --git a/CHANGELOG.md b/CHANGELOG.md index 384597e54..0d22c4b91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `Get-PnPAzureADActivityReportSignIn` cmdlet to enable retrieving of Azure AD sign ins. [#2436](https://github.com/pnp/powershell/pull/2436) - Added support to remove the site collection app catalog by using Id of the site collection in `Remove-PnPSiteCollectionAppCatalog` cmdlet. [#2452](https://github.com/pnp/powershell/pull/2452) - Added support for `RestrictedAccessControl` parameter to `Set-PnPTenant` and `Set-PnPSite` cmdlets to restrict site access to members of a Microsoft 365 group. [#2462](https://github.com/pnp/powershell/pull/2462) +- Added `Set-PnPImageListItemColumn` cmdlet to support setting of the new image/thumbnail value for a SharePoint list item. ### Changed diff --git a/documentation/Set-PnPImageListItemColumn.md b/documentation/Set-PnPImageListItemColumn.md new file mode 100644 index 000000000..95f617f37 --- /dev/null +++ b/documentation/Set-PnPImageListItemColumn.md @@ -0,0 +1,135 @@ +--- +Module Name: PnP.PowerShell +title: Set-PnPImageListItemColumn +schema: 2.0.0 +applicable: SharePoint Online +external help file: PnP.PowerShell.dll-Help.xml +online version: https://pnp.github.io/powershell/cmdlets/Set-Set-PnPImageListItemColumn.html +--- + +# Set-PnPImageListItemColumn + +## SYNOPSIS + +Updates the image column value of a list item + +## SYNTAX + +```powershell +Set-PnPImageListItemColumn [-List ] -Identity [-Field ] + [-Path ] [-UpdateType ] [-Connection ] +``` + +## DESCRIPTION + +Allows setting the Image/Thumbnail column value of a list item. + +## EXAMPLES + +### EXAMPLE 1 + +```powershell +Set-PnPImageListItemColumn -List "Demo List" -Identity 1 -Field "Thumbnail" -Path "/sites/contoso/SiteAssets/test.png" +``` + +Sets the image/thumbnail field value in the list item with ID 1 in the "Demo List". Notice, use the internal names of fields. + +## PARAMETERS + +### -Connection + +Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Identity + +The ID of the listitem, or actual ListItem object + +```yaml +Type: ListItemPipeBind +Parameter Sets: (All) + +Required: True +Position: Named +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -List + +The ID, Title or Url of the list. + +```yaml +Type: ListPipeBind +Parameter Sets: (All) + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -Field + +The ID, Title or Internal name of the field. + +```yaml +Type: FieldPipeBind +Parameter Sets: (All) + +Required: True +Position: 0 +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -Path + +Use the server relative path of an existing image in your SharePoint document library. + +```yaml +Type: String +Parameter Sets: (All) + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -UpdateType + +Specifies the update type to use when updating the listitem. Possible values are "Update", "SystemUpdate", "UpdateOverwriteVersion". + +* Update: Sets field values and creates a new version if versioning is enabled for the list +* SystemUpdate: Sets field values and does not create a new version. Any events on the list will trigger. +* UpdateOverwriteVersion: Sets field values and does not create a new version. No events on the list will trigger. + +```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) diff --git a/src/Commands/Lists/SetImageListItem.cs b/src/Commands/Lists/SetImageListItem.cs new file mode 100644 index 000000000..f4ce68b20 --- /dev/null +++ b/src/Commands/Lists/SetImageListItem.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Management.Automation; +using System.Text.Json; +using Microsoft.SharePoint.Client; +using PnP.PowerShell.Commands.Base.PipeBinds; +using PnP.PowerShell.Commands.Enums; +using PnP.PowerShell.Commands.Utilities; + +namespace PnP.PowerShell.Commands.Lists +{ + [Cmdlet(VerbsCommon.Set, "PnPImageListItemColumn")] + [OutputType(typeof(ListItem))] + public class ImageListItemColumn : PnPWebCmdlet + { + [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)] + public ListPipeBind List; + + [Parameter(Mandatory = true, ValueFromPipeline = true)] + public ListItemPipeBind Identity; + + [Parameter(Mandatory = true, ValueFromPipeline = true)] + public FieldPipeBind Field = new FieldPipeBind(); + + [Parameter(Mandatory = true)] + public string Path; + + [Parameter(Mandatory = false)] + public ListItemUpdateType UpdateType; + + protected override void ExecuteCmdlet() + { + if (Identity == null || (Identity.Item == null && Identity.Id == 0)) + { + throw new PSArgumentException($"No -Identity has been provided specifying the item to update", nameof(Identity)); + } + + List list; + if (List != null) + { + list = List.GetList(CurrentWeb); + } + else + { + if (Identity.Item == null) + { + throw new PSArgumentException($"No -List has been provided specifying the list to update the item in", nameof(Identity)); + } + + list = Identity.Item.ParentList; + } + + var item = Identity.GetListItem(list) + ?? throw new PSArgumentException($"Provided -Identity is not valid.", nameof(Identity)); + + var f = Field.Field; + + if (f == null) + { + if (Field.Id != Guid.Empty) + { + f = list.Fields.GetById(Field.Id); + } + else if (!string.IsNullOrEmpty(Field.Name)) + { + f = list.Fields.GetByInternalNameOrTitle(Field.Name); + } + } + ClientContext.Load(f); + ClientContext.ExecuteQueryRetry(); + + if (f.FieldTypeKind != FieldType.Thumbnail) + { + throw new PSInvalidOperationException("Field Type is not Image, please use a valid field"); + } + + var tenantUri = new Uri(Connection.Url); + + var payload = new + { + type = "thumbnail", + fileName = System.IO.Path.GetFileName(Path), + nativeFile = new { }, + fieldName = f.InternalName, + serverUrl = "https://" + tenantUri.Host, + fieldId = f.Id, + serverRelativeUrl = Path, + id = Guid.NewGuid().ToString() + }; + + item[f.InternalName] = JsonSerializer.Serialize(payload); + + ListItemHelper.UpdateListItem(item, UpdateType); + + ClientContext.ExecuteQueryRetry(); + ClientContext.Load(item); + WriteObject(item); + } + } +} \ No newline at end of file