This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 658
/
Copy pathGetList.cs
65 lines (59 loc) · 2.94 KB
/
GetList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Linq.Expressions;
using System;
using Resources = PnP.PowerShell.Commands.Properties.Resources;
namespace PnP.PowerShell.Commands.Lists
{
[Cmdlet(VerbsCommon.Get, "PnPList")]
[CmdletHelp("Returns lists from SharePoint",
Category = CmdletHelpCategory.Lists,
OutputType = typeof(List),
OutputTypeLink = "https://docs.microsoft.com/previous-versions/office/sharepoint-server/ee540626(v=office.15)")]
[CmdletExample(
Code = "PS:> Get-PnPList",
Remarks = "Returns all lists in the current web",
SortOrder = 1)]
[CmdletExample(
Code = "PS:> Get-PnPList -Identity 99a00f6e-fb81-4dc7-8eac-e09c6f9132fe",
Remarks = "Returns a list with the given id",
SortOrder = 2)]
[CmdletExample(
Code = "PS:> Get-PnPList -Identity Lists/Announcements",
Remarks = "Returns a list with the given url",
SortOrder = 3)]
[CmdletExample(
Code = @"PS:> Get-PnPList | Where-Object {$_.RootFolder.ServerRelativeUrl -like ""/lists/*""}",
Remarks = @"This examples shows how to do wildcard searches on the list URL. It returns all lists whose URL starts with ""/lists/"" This could also be used to search for strings inside of the URL.",
SortOrder = 4)]
public class GetList : PnPWebRetrievalsCmdlet<List>
{
[Parameter(Mandatory = false, ValueFromPipeline = true, Position = 0, HelpMessage = "The ID, name or Url (Lists/MyList) of the list")]
public ListPipeBind Identity;
[Parameter(Mandatory = false, HelpMessage = "Switch parameter if an exception should be thrown if the requested list does not exist (true) or if omitted, nothing will be returned in case the list does not exist")]
public SwitchParameter ThrowExceptionIfListNotFound;
protected override void ExecuteCmdlet()
{
DefaultRetrievalExpressions = new Expression<Func<List, object>>[] { l => l.Id, l => l.BaseTemplate, l => l.OnQuickLaunch, l => l.DefaultViewUrl, l => l.Title, l => l.Hidden, l => l.RootFolder.ServerRelativeUrl };
if (Identity != null)
{
var list = Identity.GetList(SelectedWeb);
if (ThrowExceptionIfListNotFound && list == null)
{
throw new PSArgumentException(string.Format(Resources.ListNotFound, Identity), nameof(Identity));
}
list?.EnsureProperties(RetrievalExpressions);
WriteObject(list);
}
else
{
var query = SelectedWeb.Lists.IncludeWithDefaultProperties(RetrievalExpressions);
var lists = ClientContext.LoadQuery(query);
ClientContext.ExecuteQueryRetry();
WriteObject(lists, true);
}
}
}
}