SharePoint Events

  7/22 - Webcast: Enterprise Document Security, Tracking and Collaboration in SharePoint
  7/24 - Webcast: Enhance Collaboration and Mitigate Risk with Email and Document Management in SharePoint
  7/29 - Webcast: Regulatory and Corporate Risk Protection in SharePoint for Financial Services
  7/31 - Webcast: Workflow throughout the Enterprise for Legal Firms and Departments

 SharePoint Videos

  Why SharePoint 2013
  Keys to Successful SharePoint Initiatives and User Adoption
  Out of the Box Business Intelligence and Reporting in SharePoint 2013
  Driving the Business Case and User Adoption for SharePoint
  Automate Business Processes with SharePoint 2013 and Business Connectivity Services
  SharePoint and Office 365 Migration Made Easy
  Full Lifecycle Content Management with SharePoint 2013
  Compliant Document Generation and Assembly in SharePoint
  Application of Content Security and Corporate Risk Protection in SharePoint
  How to Redline, Markup, Collaborate and Review Content in SharePoint
  Advanced Content Lifecycle and Records Management in SharePoint
  Advanced Workflow Made Easy with SharePoint 2013 and Nintex
  Full Lifecycle Content Management with SharePoint 2013
  SharePoint 2013 Solutions for the Financial Services Industry
  ProjectReady 2013: SharePoint Solutions built for the AEC
  SharePoint 2013 Solutions for the Legal Industry
  Driving the Business Case and User Adoption for SharePoint
  Investment Considerations for SharePoint 2010 and SharePoint 2013
  SharePoint 2013 and Enterprise Content Management
  SharePoint 2013 Launch
(More Links...)

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Default List Views in SharePoint Client Object Model

By: Neil Barkhina

The SharePoint client object model is very versatile at interacting with the SharePoint API from a remote location. Though almost everything can be done with the client object model, some things that are very easy to do in the native API takes a little bit more leg work. One of those examples is the way you interact with lists and libraries. All functions that collect data must be queued up and executed as a batch using the ExecuteQuery function. In this post we will look at the way you would get a Default ListView.

In the native API, iterating through list items from the default view can be done by calling SPList.GetItems(SPList.DefaultView). Unfortunately the client object model doesn’t have a DefaultView property on lists, but they can still be accessed by doing the following.

Authentication

First you need to authenticate against the site by creating a ClientContext Object and passing in the URL of your SharePoint site

ClientContext clientContext1 = new ClientContext("https://mysite.test.com ");

            clientContext1.AuthenticationMode = ClientAuthenticationMode.Default;

            NetworkCredential credentials = new NetworkCredential("test", "test", "gig-werks");

            clientContext1.Credentials = credentials;

 

If you want to use a specific account, you can pass it in by creating a NetworkCredential object and setting it to the Credentials property of the ClientContext.

Getting Lists and Views

Next you need to queue up some objects such as the site, list, view.

ClientContext clientContext1;

       Web rootweb;

       List splist;

rootweb = clientContext1.Web;

       splist = rootweb.Lists.GetByTitle("Tasks");

       clientContext1.Load(rootweb);

       clientContext1.Load(rootweb.Lists);

       clientContext1.Load(splist);

       clientContext1.Load(splist.Views);

       clientContext1.ExecuteQuery();

 

If you don’t load splist.Views we won’t be able to access the views collection of the List. Then call ExecuteQuery to load the objects.

CAML Query

The next part is getting the actual items. This is done by constructing a CAML Query. The trick here is that we are using the ViewQuery property of the ListView which contains the CAML that we need to pass into the ViewXml of our query.

     CamlQuery camlQuery = new CamlQuery();

            camlQuery.ViewXml = "<View><Query>" + splist.Views[0].ViewQuery + "</Query></View>";

            ListItemCollection listItems = splist.GetItems(camlQuery);

            clientContext1.Load(listItems);

            clientContext1.ExecuteQuery();

 

            foreach (ListItem item in listItems)

            {

                string taskname = safeReadString(item, "Title");

            }

 

Then when you iterate through your items you will see that the sort and filter properties that were on your view come through to your ListItemCollection.

Default_List_Views_in_SharePoint_Client_Object_Model

Safely Reading ListItems

In this example I also included the following function to safely read the property and handle null conditions, and then you should be good to go!

private string safeReadString(ListItem itm, string column)

        {

            try

            {

                if (itm == null || itm[column] == null || itm[column].ToString() == "")

                    return "";

 

                return itm[column].ToString();

            }

            catch (Exception ex)

            {

                return "";

            }

        }

 

By: Neil Barkhina

        

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy