-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2468 from gautamdsheth/feature/setimagelistitem
Feature: Set-PnPImageListItemColumn to update image list item
- Loading branch information
Showing
3 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |