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

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

By:

Date:

Getting a list with CSOM

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?

 

Get a list using TryGetList

Then I tried another option

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

No more errors.

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

 

 

 

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There’s a better way — and I can show you exactly what it looks like. I’m the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I’ve been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. SharePains is not just any blog run by a Microsoft MVP. Have you ever used Try-Catch in Power Automate? The original post about Try-Catch in Power Automate can still be found on this site, https://staging.sharepains.com/2018/02/07/try-catch-finally-in-power-automate-flow/ Or have you ever used the Pieter’s method to avoid variables and speed up your flows? https://staging.sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/ You can contact me using contact@staging.sharepains.com

Leave a Reply

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