SharePoint 2013 – GetListByName vs SPWeb.GetList vs SPListCollection.GetList vs SPListCollection.TryGetList

My problem

In my PowerShell script I’m doing the following:

$list =  $web.Lists[“Mylist”]

This generates the following Unexpected error in my SharePoint logs:

8ca3ea94-3288-49cd-99fd-1b6eab6cb52d Stack trace: at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException) at CallSite.Target(Closure , CallSite , Object , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)

Am I doing something wrong. Not really as I’m getting my list object from SharePoint and all seems to be working.

I don’t like unexpected error as they tend to point towards something that isn’t handled properly.

How about using the GetList method?

I changed my code to:

$ulsList = $web.GetList($web.Url + “/lists/” + $ULSListName)

One Unexpected is now replaced with another one:

c50dac63-ae98-4054-9cb9-615373cfd553 Stack trace: at Microsoft.SharePoint.SPWeb.GetList(String strUrl) at CallSite.Target(Closure , CallSite , Object , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0) at System.Management.Automation.DlrScriptCom…

hmm, So what could be the issue?

 

My Solution

Then I tried another option

$ulsList = $web.Lists.TryGetList($ULSListName)

No more errors.

How many options can there be to get a list???

 

 

 

Advertisements

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.