jump to navigation

LINQ and REST in Sharepoint 2010: Live from SPC09 Las Vegas October 21, 2009

Posted by willhlaw in SharePoint, SPC09.
Tags: , , ,
trackback

LINQ and REST in Sharepoint 2010.

I am blogging live from the Sharepoint 2010 Conference in Las Vegas. This is a session on LINQ and REST web services. I have only written about the highlights.

Overview of new architectural features when dealing with querying lists in SharePoint 2010

  • Query Throttling
    • Site collection administrator can put a limit (i.e. 5000) on the query block.
    • Developer can override the querythrottlingblock to retrieve more items from the server.
    • This prevents end users from swamping the server.
  • Resource Throttling
    • Pagination plays a distinct role so that the data is filtered on the server, not on the client side anymore. First time audience clapped.
  • Allows list options to be setup so that child references are setup for delete through. This means that if the parent is deleted, then the children go into the Recycle Bin as well. Other options prevent the user from deleting a parent item that has child items.
  • Demo is in the Javascript Client Object Model
    • If you can do it in javascript, you can do it in the client.
    • The Javascript OM requires you to be more explicit with every step.
    • Custom page is called /_layouts/data/clientom.aspx
    • Uses the Yahoo YUI library script files.
    • Example had a var viewXML which was a predefined CAML query with {1} to act as placeholders for variable replacements later.

REST

  • REST-style ListData.svc web service
  • Work with data via (Representational State Transfer)
  • SharePoint list data
  • Other data sources as well
    • Excel and Azure cloud
  • Powered by ADO.NET Dadta Services “Astoria”
    • REST Protocols: Atom, AtomPub, and RSS
  • Syntax:
    • /_vti_bin/ListData.svc/{Entity}[({identifier})]/[{Property}]
  • Example to get budget hours for Project $4:
    • /_vti_bin/ListData.svc/Projects(4)/BudgetHours
  • Example to get Projects for Clients in Chicago:
    • /_vti_bin/ListData.svc//Projects?$filter=Client/City eq ‘Chicago’
  • Example to get a Project and its related Client:
    • /_vti_bin/ListData.svc/Projects?$expand=Client
    • Shows me parent that is associated with this XML
  • QueryString parameters for REST
    • $filter
    • $expand
    • $orderby
    • $skip
    • $top
    • $metadata (will bring back all the XML metadata about the object. Think of it like WSDL for your REST call)
    • You can stack these parameters
  • Demo, manipulating REST data.
    • Javscript Client Object Model does not allow XSS (cross site) scripting so you must use relative URLs.
      • Tip: To guard against CSRF (Cross Site Reference Forgery) attacks using JSON, the array or JSON object in a response is always wrapped by a single JSON object with a singler member named “d” whose value includes the array or object representing the entity or entities being returned.”
      • http://msdn.microsoft.com/en-us/library/cc907912.aspx
    • Tip: IE8 -> Internet Options -> Feed and Web Slice Settings -> uncheck Turn on feed reading view in order to view XML in the browser.
    • Code snippet from example demo using Rest in javascript.
      • function PostData(url,data, callback)
        • var wRequest = new sys.Net.WebRequest()
        • wRequest.set_url(url)
        • wRequest.get_headers()
        • wRequest.set_httpverb()

LINQ to SharePoint

  • Strongly-typed access to List Data on Server
  • Entity based programming on the server
    • SPMetal Code Generation Utility (command line utility can be used for automatic builds)
    • LINQ Queries
    • Strong Types
  • Translates LINQ queries into CAML queries
  • Can be used in
    • Web Parts, Event Receivers, Sandboxed code
    • Sample: Orginal LINQ query

      From p in dadta.Projects

      Where

      p.Cleint.City – “Chicago”

      select new {

      Name = p.tTiel,

      clientName = p.Client.Title,

      Budget = p.BudgetHours

      };

    • Sample in CAML was shown and it looked ridiculous to code out. Everybody clapped because LINQ is SO much easier.
  • Demo, accessing relational SharePoint data using LINQ to SharePoint.
    • This example is on coded on the server
    • The first demo was rebuilt using the ASP.NET treeview rather than the YUIi treeview.
    • There is a property in the SPConfListDataContext called log.
    • Of course, with a server side solution in Visual Studio 2010, you can set a breakpoint in the code and break to it once that code is reached after a button press, for example, on the web page. Remember, in 2010, there are sandboxed solutions to help develop solutions before deploying to production.
    • The LINQ query is simple and ends up generating CAML query in the background.

Data Access Technologies

  • SharePoint 2010 List Data model
    • You should be using the Server OM and LINQ to SharePoint when you are developing on the server.
    • You should be using the Client OM and REST when running the code on a remote computer.
    • REST and LINQ are not giving you SharePoint objects. It is only giving you XML. You should use one of the Object Models.
    • If you want to work with relationships between items, REST and LINQ are much easier to use than the OM.
    • OM and REST can benefit from the Callback Calling Pattern. Remember in the Javascript Client Object Model, the .executeQuery(successCallBack, failureCallBack).
  • Managed Client OMs vs. Javascript Client OM
    • Full URL Context vs Server-relative Context Only
    • String Collection vs String[] Array
    • Null, infinity vs. Nan, positive/negative infinity
    • Explicit FBA support vs Context FBA support
    • Create, Update, Commit vs Create, Commit, Update
    • No FormsDigest Required vs Needs <SharePoint:FormsDigest>
    • Standard Server OM Identity vs No RoleDefinitionBindingCollection Identity
    • SPWeb Locale for Comparisons vs Invariant Culture for Comparisons.

This was a very detailed presentation with a lot of code talk-throughs. There are many details to follow up on. My post here only acts as a quick overview of the session and certainly not a definitive guide. The session slides will be posted after the Conference and I will update this blog post with links once they are.

Advertisements

Comments»

1. Mikhail Bondarevskiy - November 25, 2009

What about linq to sharepoint in Silverlight ?

2. A Busy Week in the SharePoint World « The SharePoint Mechanic - March 2, 2010

[…] SPC09 – LINQ and REST in SharePoint 2010 – Courtesy of Will Lawrence […]

3. chaminda somathilaka - January 27, 2011

I tried using this to add a list item with a lookup field (multiple allowed). But i failed get values added to the lookup field on the list. Also when I retrieve the list for items that have manually added lookup values, the REST service returned nothing.

Any idea how I can get this to work ?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: