2

I have developed a code in CSOM(Client Side Object Module) using c# where before performing any operation on the list I want to check whether the list is null or not? In SSOM we can check that by

.TryGetList() method

how I can achieve the same in CSOM.

if(list!= null){....} is not working as it always goes into the if condition and throws the error in context.Update()

Is there any other way to achieve this without putting the

if(list!= null){....} into try-catch.

1

Nope, it doesn't exist in native CSOM, but the PnP Core library has this extension method:

 if (ctx.Web.ListExists("My List")){
            //do something
 }

https://github.com/SharePoint/PnP-Sites-Core

1

You can check the list collection count to see if the list exists in the site as shown below:

           ListCollection listCollection = clientContext.Web.Lists; 
           clientContext.Load(listCollection, lists => lists.Include(list => list.Title).Where(list => list.Title == "ListName"));

            clientContext.ExecuteQuery();

            if (listCollection.Count > 0)
            {
                Response.Write("List  exists...");
            }
            else
            {
                Response.Write("List does not exist");
            }
  • It's not happening. Still its goes into the loop and gives error. – Dikesh Gandhi Aug 3 '17 at 4:02

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.