1

It's very easy. Just share some pieces of advice with me.

Example - I need to load a view of a list:

var view = _list.Views.GetByTitle("title");

//but returned view always is not null
if(view != null){
     //do smth
}

Regardless of existence this view GetByTitle() returns a non null object. So my question is how to determine is view loaded(existed) or not ?

1 Answer 1

3

You can use "ServerObjectIsNull" property to check if the server object is null

Ex:

if(!view.ServerObjectIsNull.HasValue)

Try using below function to validate the list view.

public bool IsViewExist(string siteUrl, string listTitle, string viewTitle)
    {
        bool isViewExist = false;
        ClientContext context = new ClientContext(siteUrl);

        List list = context.Web.Lists.GetByTitle(listTitle);
        context.Load(list);
        context.ExecuteQuery();

        Microsoft.SharePoint.Client.ViewCollection viewColl = list.Views;
        context.Load(viewColl,
            views => views.Include(
                view => view.Title));
        context.ExecuteQuery();
        foreach (Microsoft.SharePoint.Client.View view in viewColl)
        {
           if(view.Title.Equals(viewTitle))
           {
               isViewExist = true;
               break;
           }

        }

        return isViewExist;
    }
6
  • but after using this property i get error
    – isxaker
    Commented Oct 16, 2017 at 19:25
  • and i don't why this property gets me null regrdless that the object actually exists
    – isxaker
    Commented Oct 16, 2017 at 19:35
  • Please see the updated answer: This function could be useful to validate the list view. Commented Oct 16, 2017 at 22:05
  • @isxaker Did you execute context.load and context.executeQuery before checking ServerObjectIsNull? Commented Oct 17, 2017 at 7:07
  • Ok. Obviously it is a key point. I mean load and execute. Do I have to call these methods for a viewCollection or a view ? And thanks for updated answer, but I had almost the same code. And my main goal was to prevent using foreach =))
    – isxaker
    Commented Oct 17, 2017 at 8:41

Your Answer

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

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