Pages

Tuesday, August 17, 2010

Linq with SharePoint 2010 - Part I

Today I have decided to start working on my web part that will be interacting with SharePoint 2010 through LINQ, after spending a few R&D through the web, I have started my development of Visual Web Part with SharePoint LINQ (Of course, using Visual Studio 2010!).

Before that, let me create a list named "Quote of the Week" which a few columns.

1. Start Visual Studio 2010 (Of course!)

2. Create a Visual Web Part Project, which will automatically generate the files required.

3. Now, i need to really get my hands on the SharePoint Linq experience, thus, i will be going to my web part folder, and run the SPMetal.exe command from your command prompt.

4. A class file will be generated, basically this class file will be wrapping all your lists definition, so that you can access STRONG TYPED list objects (Yeah!!!)



5. Add this class file to your project NOW!

6. Wait, i need a field, internal field for created date, but, it is not in my generated class. So, what i will do is to replicate a property (It will returned the field value based on the properties you set for the list!).


6. Now, i have my object models for SharePoint Linq done, let's put this into practical, note the following code snippet. This is the page load event that query SharePoint list, using Linq of course!

NOTE: This piece of code query the Quote of the Week list, search for quote items where Start Date <> and End Date > Today, and it will be sorted by the created date, in descending order.

7. Yes! My Quote of the Week web part is now DONE!

Easy? Now, let's rock-and-roll NOW!

Note : If you are changing the namespace and class name, you need to change the Safe Control registration, see the following URL,

http://www.andrewconnell.com/blog/archive/2010/07/01/renaming-web-parts-for-sharepoint-2010-with-visual-studio-2010.aspx

No comments:

Post a Comment