Skip to content

Commit

Permalink
Merge pull request #1921 from gautamdsheth/feature/1360
Browse files Browse the repository at this point in the history
Feature #1360 - fetch Content type info for list items
  • Loading branch information
KoenZomers authored Jun 9, 2022
2 parents 5f759ff + 519fb58 commit ece04e1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 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 `Restart-PnPFlowRun` which allows for a failed Power Automate flow run to be retried [#1915](https://github.com/pnp/powershell/pull/1915)
- Added optional `-Connection` parameter to `Get-PnPConnection`, `Get-PnPContext` and `Set-PnPContext` which allows for using any of these for a specific connection [#1919](https://github.com/pnp/powershell/pull/1919)
- Added `-IncludeDeprecated` parameter to `Get-PnPTerm` cmdlet to fetch deprecated terms if specified [#1903](https://github.com/pnp/powershell/pull/1903)
- Added `-IncludeContentType` parameter, which if specified will retrieve content type information of the list items. [#1921](https://github.com/pnp/powershell/pull/1921)
- Added optional `-ValidateConnection` to `Connect-PnPOnline` which will check if the site you are connecting to exists and if not, will throw an exception [#1924](https://github.com/pnp/powershell/pull/1924)
- Added `Add-PnPListItemAttachment` cmdlet to provide ability to upload a file as an attachment to a SharePoint list item. [#1932](https://github.com/pnp/powershell/pull/1932)
- Added `Remove-PnPListItemAttachment` cmdlet to provide ability to delete a list item attachment. [#1932](https://github.com/pnp/powershell/pull/1932)
Expand Down
28 changes: 23 additions & 5 deletions documentation/Get-PnPListItem.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,25 @@ Retrieves list items
### All Items (Default)
```powershell
Get-PnPListItem [-List] <ListPipeBind> [-FolderServerRelativeUrl <String>] [-Fields <String[]>]
[-PageSize <Int32>] [-ScriptBlock <ScriptBlock>] [-Connection <PnPConnection>]
[-PageSize <Int32>] [-ScriptBlock <ScriptBlock>][-IncludeContentType <SwitchParameter>][-Connection <PnPConnection>]
[<CommonParameters>]
```

### By Id
```powershell
Get-PnPListItem [-List] <ListPipeBind> [-Id <Int32>] [-Fields <String[]>]
Get-PnPListItem [-List] <ListPipeBind> [-Id <Int32>] [-Fields <String[]>] [-IncludeContentType <SwitchParameter>]
[-Connection <PnPConnection>] [<CommonParameters>]
```

### By Unique Id
```powershell
Get-PnPListItem [-List] <ListPipeBind> [-UniqueId <Guid>] [-Fields <String[]>]
[-Connection <PnPConnection>] [<CommonParameters>]
Get-PnPListItem [-List] <ListPipeBind> [-UniqueId <Guid>] [-Fields <String[]>] [-IncludeContentType <SwitchParameter>] [-Connection <PnPConnection>] [<CommonParameters>]
```

### By Query
```powershell
Get-PnPListItem [-List] <ListPipeBind> [-Query <String>] [-FolderServerRelativeUrl <String>]
[-PageSize <Int32>] [-ScriptBlock <ScriptBlock>] [-Connection <PnPConnection>]
[-PageSize <Int32>] [-IncludeContentType <SwitchParameter>] [-ScriptBlock <ScriptBlock>] [-Connection <PnPConnection>]
[<CommonParameters>]
```

Expand Down Expand Up @@ -119,6 +118,13 @@ Id Filename

Retrieves all list items from the Shared Documents and shows each item's ID and Filename

### EXAMPLE 11
```powershell
Get-PnPListItem -List Tasks -Id 1 -IncludeContentType
```

Retrieves the list item with ID 1 from the Tasks list along with its content type information.

## PARAMETERS

### -Connection
Expand Down Expand Up @@ -247,7 +253,19 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -IncludeContentType
If specified, it will retrieve the content type information of the list item(s).
```yaml
Type: Switch Parameter
Parameter Sets: All

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
Expand Down
40 changes: 29 additions & 11 deletions src/Commands/Lists/GetListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ public class GetListItem : PnPWebCmdlet
public string[] Fields;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALLITEMS)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYQUERY)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYQUERY)]
public int PageSize = -1;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALLITEMS)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYQUERY)]
public ScriptBlock ScriptBlock;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALLITEMS)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYQUERY)]
public ScriptBlock ScriptBlock;

protected override void ExecuteCmdlet()
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALLITEMS)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYID)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYUNIQUEID)]
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYQUERY)]
public SwitchParameter IncludeContentType;

protected override void ExecuteCmdlet()
{
var list = List.GetList(CurrentWeb);
if (list == null)
Expand All @@ -66,6 +72,10 @@ protected override void ExecuteCmdlet()
{
ClientContext.Load(listItem);
}
if (IncludeContentType)
{
ClientContext.Load(listItem, l => l.ContentType, l => l.ContentType.Name, l => l.ContentType.Id, l => l.ContentType.StringId, l => l.ContentType.Description);
}
ClientContext.ExecuteQueryRetry();
WriteObject(listItem);
}
Expand All @@ -83,18 +93,22 @@ protected override void ExecuteCmdlet()
viewFieldsStringBuilder.Append("</ViewFields>");
}
query.ViewXml = $"<View Scope='RecursiveAll'><Query><Where><Or><Eq><FieldRef Name='GUID'/><Value Type='Guid'>{UniqueId}</Value></Eq><Eq><FieldRef Name='UniqueId' /><Value Type='Guid'>{UniqueId}</Value></Eq></Or></Where></Query>{viewFieldsStringBuilder}</View>";

var listItem = list.GetItems(query);
ClientContext.Load(listItem);
if (IncludeContentType)
{
ClientContext.Load(listItem, l => l.Include(a => a.ContentType, a => a.ContentType.Id, a => a.ContentType.Name, a => a.ContentType.Description, a => a.ContentType.StringId));
}
ClientContext.ExecuteQueryRetry();
WriteObject(listItem);
}
else
{
CamlQuery query = HasCamlQuery() ? new CamlQuery { ViewXml = Query } : CamlQuery.CreateAllItemsQuery();
CamlQuery query = HasCamlQuery() ? new CamlQuery { ViewXml = Query } : CamlQuery.CreateAllItemsQuery();
query.FolderServerRelativeUrl = FolderServerRelativeUrl;

if (Fields != null)
if (Fields != null)
{
var queryElement = XElement.Parse(query.ViewXml);

Expand Down Expand Up @@ -143,16 +157,20 @@ protected override void ExecuteCmdlet()
{
var listItems = list.GetItems(query);
ClientContext.Load(listItems);
if (IncludeContentType)
{
ClientContext.Load(listItems, l => l.Include(a => a.ContentType, a => a.ContentType.Id, a => a.ContentType.Name, a => a.ContentType.Description, a => a.ContentType.StringId));
}
ClientContext.ExecuteQueryRetry();

WriteObject(listItems, true);

if (ScriptBlock != null)
{
ScriptBlock.Invoke(listItems);
}
ScriptBlock.Invoke(listItems);
}

query.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
query.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
} while (query.ListItemCollectionPosition != null);
}
}
Expand Down

0 comments on commit ece04e1

Please sign in to comment.