2

I am trying to get a list using PowerShell, so I am executing this code:

 $list = $web.GetList($web.Url +"lists/TestList")

It's giving me:

Exception calling "GetList" with "1" argument(s): "0x80070002" At line:1 char:1 + $list= $web.GetList($web.Url +"lists/TestList") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FileNotFoundException

How do I get a list by its Title, if I run this:

$list = $web.Lists | ? {$_.Title -eq "TestList"}

Wouldn't this loop through all lists in the site which will be bad for performance? Thanks for any hint to guide me to the right direction.

  • Can you supply the values you are returning. What is in $web.url? If you run an output of all your lists and their URL's is /testlist returned? It's possibly using something different maybe test%20Lists, or something else? – Jesus Shelby Mar 16 '15 at 23:26
  • The returned value from $web.lists + "/lists/TestList" is : server1:9999/sites/finance/lists/testlist .. but it's giving an error stated above. My list doesn't have spaces between the letters. – Shang Hui Mar 16 '15 at 23:28
5

Inspect the result of your request string

 $list = $web.GetList($web.Url +"lists/TestList")

my guess is that you get something like https://weburllists/testlist - try adding the leading slash in your query.

$list = $web.GetList($web.Url +"/lists/TestList")

Another way is to use the Lists method.

$list = $web.Lists["listname"]
  • I tried this, gave me same error.. – Shang Hui Mar 16 '15 at 22:55
4

I believe GetList method requires server relative url as a parameter. Please check this thread for details. You can try to use web.ServerRelativeUrl instead of web.Url but getting list by title is also not a bad idea.

$list = $web.Lists["ListTitle"]

It will enumerate all lists on your site, but it's pretty fast and you can ignore this little impact in a script.

4

You can also use

 $list = $web.Lists.TryGetList("Title of list")

This will never throw an error, but $list will be null if no list with that title is found in the web

0

Yes, what I do is make use of the Measure-Command

Measure-Command {
$list = $web.Lists | ? {$_.Title -eq "TestList"}
}

Specifying an itemid, url, and such is always better than a where clause.

  • But aren't you using a where clause here? – Shang Hui Mar 16 '15 at 22:55
  • yes, but it just measure's the time/efficiency of the command instead. The point of Measure-Command is to measure the efficiency of whatever command is in the block, so you can choose one or the other. It's only meant for testing. – Mike Mar 17 '15 at 14:27
0

For googlers searching for "Get-SPWeb GetList MethodInvokationException":

In my case we had a script that used

$web = Get-SPWeb -Site $url

Here, the root web and all subwebs of the site collection will be returned and stored in the $web variable - which is equivalent to the C# version

using (var site = new SPSite(url))
using (var web = site.OpenWeb())
{ ... }

to get the SPSite's root web if and only if the site collection contains only the root web. But loading a list via $web.GetList("foo") where $web is actually a collection of SPWebs fails with a MethodInvokationException.

So you might want to check if your site collection contains subwebs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.