0

I want to load fields of the default view for Sharepoint list through client object model (I am using Silverlight). Here are some relevant things I've found (on msdn here):

  • class List has property DefaultViewUrl [of type string]
  • class List has method GetView(Guid)
  • class List has property Views [of type ViewCollection]
  • class ViewCollection has method GetById(Guid)
  • class ViewCollection has method GetByTitle(string)
  • class View has property DefaultView [of type bool]

That's everything I was able to find. As you can see there is no direct way of getting DefaultView (there is missing DefaultViewId property on List or GetByUrl(string) method on ViewCollection).

Seems to me as the only solution is to iterate through List.Views collection and check DefaultView property on each View. Which is kind of...well, inefficient...

Did I miss something? Anyone see some straigh solition? Thanks for ideas.

jumbo
  • 4,670
  • 5
  • 39
  • 44
  • What are you looking for? The name of the default view so you can display it? I believe by default it will retrieve the fields for the default view as defined by the user. – Philipp Schmid Aug 08 '11 at 22:36
  • AFAIK `List.Fields` contains **all** fields. Class `View` has property `ViewFields` which should contain only subset of all fields. – jumbo Aug 09 '11 at 05:13

2 Answers2

1

Try LoadQuery using a LINQ statement

For Example:

private IEnumerable<View> viewQuery = null;
public void LoadDefaultView()
{
    using (ClientContext ctx = ClientContext.Current)
    {
        list = ctx.Web.Lists.GetByTitle("YourList");

        viewQuery = ctx.LoadQuery(list.Views
                   .Include(v => v.Title) // include more lamda statements here to populate View Properties
                   .Where(v => v.DefaultView == true));

        ctx.ExecuteQueryAsync(LoadDefaultViewSuccess, LoadDefaultViewFailure);
    }
}
private void LoadDefaultViewSuccess(object sender, ClientRequestSucceededEventArgs args)
{
    // should only be one View in views
    View defaultView = viewQuery.FirstOrDefault();

    // use default.Title here
}
private void LoadDefaultViewFailure(object sender, ClientRequestFailedEventArgs args)
{
    // handle failure here
}

MSDN SharePoint 2010 Silverlight COM article here http://msdn.microsoft.com/en-us/library/ee538971.aspx

0

What about SPList.DefaultView? The SPList DefaultView member is an SPView object (not bool)

MikeM
  • 27,227
  • 4
  • 64
  • 80
  • 1
    I am talking about **Client** Object Model. Not standart Object model of Sharepoint. I am using Silverlight. (I added some mentions to the post to be more clear) – jumbo Aug 08 '11 at 16:09
  • Gotcha, haven't used the Client Object Model yet, based on the MSDN documentation I'm concluding the same thing you have, no `GetByUrl` method for instantiating a `View` and no method for getting the `DefaultView` from a `List` without iterating over the `Views`. – MikeM Aug 08 '11 at 16:29
  • Thank you for rechecking the documentation. I leave the question open, just in case someone will come up with something... – jumbo Aug 08 '11 at 16:38