Skip to content

Commit

Permalink
Merge pull request #2468 from gautamdsheth/feature/setimagelistitem
Browse files Browse the repository at this point in the history
Feature: Set-PnPImageListItemColumn to update image list item
  • Loading branch information
gautamdsheth committed Oct 14, 2022
2 parents 61e02c1 + 061b6d1 commit a9ac64f
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
135 changes: 135 additions & 0 deletions documentation/Set-PnPImageListItemColumn.md
Original file line number Diff line number Diff line change
@@ -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 <ListPipeBind>] -Identity <ListItemPipeBind> [-Field <FieldPipeBind>]
[-Path <string>] [-UpdateType <UpdateType>] [-Connection <PnPConnection>]
```

## 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)
102 changes: 102 additions & 0 deletions src/Commands/Lists/SetImageListItem.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit a9ac64f

Please sign in to comment.