Eamon Barker's eb.NET

Not quite vb.NET, but getting close!

Filter by APML
RSS Feed

Search

Profiles/Groups

Google Ads

Top Posts

Tags

Categories

Archive

Calendar

<<  November 2008  >>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Blogroll

Disclaimer

All postings are provided AS IS with no warranties, and confer no rights.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright Eamon Barker 2008

CC License

Visitor Map

Locations of visitors to this page
Welcome to eb.NET... a place that I can keep the things that help me in my day, that might help you in your's!

SharePoint Development Tip: Using LINQ to query a SharePoint List

Do you want to use LINQ to query SharePoint information? Read on!

Over the past couple of days I have been playing with an internal project, so I thought I would play and query the lists with LINQ... here is what I came up with!

Because a SPListItemCollection inherits from ICollection, we can attack it with a LINQ query... and that we shall!

A bit of background, I am using a Web Part to render the information to the user. The Web Part has a several properties for the user to supply information to configure it (they will appear in the "Miscellaneous" section of the Web Part Properties Pane). The properties are as follows, the CAML property is optional, but it gives an easy way to filter information from the list without going into the code:

   1: Private _siteurl As String = ""
   2: <WebBrowsable(True), _
   3: WebDisplayName("Web Site URL *"), _
   4: WebDescription("The site the list is located."), _
   5: Personalizable(PersonalizationScope.User)> _
   6: Public Property SiteURL() As String
   7:     Get
   8:         Return Me._siteurl
   9:     End Get
  10:     Set(ByVal value As String)
  11:         Me._siteurl = value
  12:     End Set
  13: End Property
  14:  
  15: Private _listGuid As String = ""
  16: <WebBrowsable(True), _
  17: WebDisplayName("Test Management List GUID *"), _
  18: WebDescription("Enter the GUID for the List."), _
  19: Personalizable(PersonalizationScope.User)> _
  20: Public Property ListGUID() As String
  21:     Get
  22:         Return Me._listGuid
  23:     End Get
  24:     Set(ByVal value As String)
  25:         Me._listGuid = value
  26:     End Set
  27: End Property
  28:  
  29: Private _camlQuery As String = ""
  30: <WebBrowsable(True), _
  31: WebDisplayName("CAML query for the webpart"), _
  32: WebDescription("Enter the CAML for the List."), _
  33: Personalizable(PersonalizationScope.User)> _
  34: Public Property CAML() As String
  35:     Get
  36:         Return Me._camlQuery
  37:     End Get
  38:     Set(ByVal value As String)
  39:         Me._camlQuery = value
  40:     End Set
  41: End Property

The first thing we need to do in the process is attach to the site and then web that we want we want to query. To do this I do the following:

   1: ' Create a Web object to use through the routine
   2: Dim Web as SPWeb
   3: ' Crate a local SPWeb Object
   4: Dim _thisweb As SPWeb
   5: ' Open the site with the URL supplied by the user
   6: Dim SiteColl As SPSite = New SPSite(SiteURL)
   7: _thisweb = SiteColl.OpenWeb
   8: ' Write the SPWeb Object to the property
   9: Web = _thisweb
  10: ' Clean up the SPWeb Object
  11: _thisweb.Dispose()
  12: _thisweb.Close()

This gives us an SPWeb object to play with. In the next snippet of code we attach to the list and create a CAML query object (SPQuery) that we will apply if it was supplied:

   1: ' Attach to the list
   2: Dim thisListGUID As Guid = New Guid(_listGuid)
   3: Dim list As SPList = Web.Lists(thisListGUID)
   4:  
   5: ' Define the Query
   6: Dim query As SPQuery = New SPQuery(list.DefaultView)
   7: If CAML <> "" Then
   8:     query.Query = CAML
   9: End If

Now we have all of that set up, we can make the LINQ query. in this query we are simply retrieving the data, but it is possible to sort, group by etc. as well.

   1: Dim linqQuery = From Z As SPListItem In list.GetItems(query) _
   2:                                   Select Z

Now we have the information, we can loop through it and do what we want with it. Below I have shown we can use a Lambda Query to group SharePoint information. In the example list I have Title, Category and Status fields and I want to group the information by the Category and then do something depending on the status, it may be some conditional formatting depending on other properties or maybe assign a task... anything really.

The reason for the substring is that SharePoint formats fields with an ID and then the Human Readable value, e.g. 1;#Status. We only want what is after the #.

   1: Dim CategoryTitle as String = ""
   2: For Each i In linqQuery.GroupBy(Function(a As SPListItem) a("Category"))
   3:     CategoryTitle = i.Key.ToString.Substring(i.Key.ToString.LastIndexOf("#") + 1)
   4:     For Each item In i
   5:         Select Case item("Status").ToString
   6:             Case "Open"
   7:                 ' Do Something
   8:             Case "Closed"
   9:                 ' Do Something
  10:             Case Else
  11:                 ' Do Something else
  12:         End Select
  13:     Next
  14: Next 

Hopefully that is a fairly quick and easy demo of how to get information from SharePoint as objects and use them!

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


System Administration Tools for SharePoint

A list of things I use to keep on top of SharePoint and its day to day running.

To follow on from my Development Tools for SharePoint post, I thought I would throw together a list of things I use to keep on top of SharePoint for its day to day running and making sure it is going to be able to cope after an install/upgrade.

Tool's Name Description
STSADM This would be the tool that I use the most when administering SharePoint.
STSADM is the command line utility that allows you to do any configuration/install/migration/anything task that you need to perform on your deployment. STSADM is more powerful than the Web UI and can perform some tasks that the Web UI can't.
Want to schedule a nightly SP back up? Using the STSADM command in a batch file attached to a scheduled task will give you that functionality!
A really good source for all the STSADM commands is Jose Barreto's blog on all the operations and parameters for STSADM
stsadmWin This was suggested in another post of mine, and I have had to add this to the list of tools I use to manage SP. STSADM is cool, this makes it a lot cooler! It is a must for any SP admin!
SharePoint Capacity Planning Tool This isn't a tool for monitoring a SharePoint deployment, but it is fantastic for planning a deployment and as such is a must in the tool kit! MSFT have done a good job on this tool and is something you should run if there is ever a company expansion or a shift in the way you use SP.
Best Practices Analyzer for SharePoint Where would we be without best practice's? This tool runs over your farm, applies best practice rules and produces a report giving you a heads up on where to improve the performance of your deployment. There is more information about the BPA on the SharePoint Team's blog.
SharePoint SUSHI

SUSHI = SharePoint Utility with a Smart, Helpful Interface

SUSHI is a powerful, user-friendly SharePoint application enabling you to accomplish common SharePoint administrative and development tasks. You can think of SUSHI as a Swiss army knife for SharePoint.

Microsoft SQL Server Manager I use this to "take care" of the SharePoint Databases. I use MSSM to manage the size of the database's for the various sites in an given farm.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5