1

What's a good way to perform an Exists or Count on the client object model classes?

I have list with views. I want to check if a view already exists before creating it. Tried a couple of things, but the only thing which actually seems to work is loading the entire collection and using a foreach to check each views' title. I was hoping for a neater way using Linq for instance.

Tried:

  • Performing a Where on the ViewCollection and loading that with LoadQuery. But when you perform Count() on that collection, you get an error.
  • performing a Load on the entire Views collection and querying that; again an error.
  • performing a count and loading the result, seems to be unsupported

2 Answers 2

2

You could try using the ViewCollection.GetByTitle or List.GetView method to fetch the already existing view. If the view is present, it will be returned and you wont need to create the new view.

0

Just to add an example of it:

private static bool ListViewExists()
{
    ClientContext clientContext = new ClientContext("http://yoursharepointsiteurl");
    List list = clientContext.Web.Lists.GetByTitle("Test");
    ViewCollection views = list.Views;

    // Check if the view already exists
    View view = views.GetByTitle("All Items");

    try
    {
        clientContext.Load(view);
        clientContext.ExecuteQuery();

        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.