Wednesday, April 24, 2013

Sharepoint : LINQ Examples

In this post I am going to show some of the LINQ to SharePoint examples which will be useful in performing CRUD operations.

Before that lets think about SQL, CAML and LINQ

In SharePoint all the data will be stored in SQL databases. SharePoint List will be an front end for doing CRUD operation. Since the users does not have DB access List will be used as an interface to perform CRUD operation.

Suppose if we want to get few records from SharePoint list based on some condition then we need to write CAML query to get the records.

Writing CAML query is little bit tedious even though we have excellent CAML Query builder. To overcome this problem we have LINQ for SharePoint which will internally run CAML to perform CRUD operations.

So LINQ is a front end for CAML, internally CAML is front end to SQL.

Here I am showing how to use LINQ to SharePoint using SPMetal.exe

Lets create a SharePoint List called EmployeeDetails with following fields.

EmployeeID - Integer FirstName - Single Line Text LastName - Single Line Text DOB - Date field Phone - Number Email - Single Line Text

Create some dummy records in the employee list.

Requirement:

Lets assume we have one timer job which will send an EMail to the employee whose birthday is today :).

1) Open SPMetal.exe from CommondPrompt

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN



Run the following command from window.

c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN>SPMETAL.exe /web:http://sharepoint2010 /namespace:SharePointConsoleApplication1 /code:ListEntities.cs (This will create the entity class with the namespace.



or

c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN>SPMETAL.exe /web:http://sharepoint2010 /code:ListEntities.cs (This will create Entity class without namespace)



Add ListEntities.cs file from 14 hive folder to the project and verify the namespace(make sure Microsoft.Sharepoint.Linq dll reference has been added in the project)

The structure of the Project should be as below after adding the entity class into the project.



Read Operation:

I have pasted below code which will display Employee Name whose birthday is today. (Its like an Select statement in SQL)

using System;
using System.Linq;
using Microsoft.SharePoint;
namespace SharePointConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Open the List Entities Data context file and pass the Site url as parameter
using (ListEntitiesDataContext list = new ListEntitiesDataContext("http://sharepoint2010"))
{
//Once data context is ready its simple Linq statement to fetch the records based on the condition
var spItems = from a in list.EmployeeDetails where a.DOB == System.DateTime.Now select a;
//Loop through all the items
foreach (var item in spItems)
{
//Here we can write our business logic just displaying the employess first name
Console.WriteLine(item.FirstName);
}
}
}
}
}

Will continue :)

Friday, April 19, 2013

List of Sharepoint 2010 Developer Tools

Below are some of the Tools/application which will be handy during development/debugging phase of the projects.

1) WSP builder
http://wspbuilder.codeplex.com/releases/view/30858
The WSP builder will create .wsp file extension for the sharepoint, event though Visual Studio provide OOB package builder still this tool will be handy.

2) Caml
http://www.u2u.be/res/Tools/files/CamlBuilderSetupv4.0.0.0.zip Using this tool we can easily build complex query to fetch data from SharePoint list.

3) TFS power tool
http://visualstudiogallery.msdn.microsoft.com/c255a1e4-04ba-4f68-8f4e-cd473d6b971f It's a plugin for Visual Studio and provides great flexibility to work with TFS.

4) Powershell
http://www.powergui.org/downloads.jspa Provides GUI for writing Powershell scripts to perform all sharepoint related administrative/developer task.

5) SPManger
http://spm.codeplex.com/releases/view/51438 The SharePoint Manager is UI based SharePoint object model explorer.

6) UlsViewer
http://archive.msdn.microsoft.com/ULSViewer This tool provides the sharepoint ULS log information where we can search or filtering the data based on category etc.

7) IE/Firefox developer tool bar
Out of the box tool where we can do style changes or debugging javascript code.

8) SPMetal
http://linqtosharepoint.codeplex.com/wikipage?title=SPMetal

9) CCleaner
http://download.cnet.com/CCleaner/3000-18512_4-10315544.html To clear cookies/browser history and this will be helpful when we use cookies related operation for storing data.

10) ILSpy
http://sourceforge.net/projects/sharpdevelop/files/ILSpy/2.0/ILSpy_Master_2.1.0.1603_RTW_Binaries.zip/download
This tool allows to decompile and browse the content of any .NET assembly

11) Fiddler
http://fiddler2.com/
Fiddler is a free web debugging tool which logs all HTTP(S) traffic between your computer and the Internet

12) CKS Visual Studio Extension
SPServer
http://visualstudiogallery.msdn.microsoft.com/en-us/ee876627-962c-4c35-a4a6-a4d89bfb61dc
Foundation
http://visualstudiogallery.msdn.microsoft.com/a346880f-2d29-47a6-84a2-f2d568dd6997/
This tool extends the Visual Studio 2010 SharePoint project system with advanced templates

13) Sharepoint Installer
http://autospinstaller.codeplex.com/downloads/get/100843

14) SPDisposecheck
http://archive.msdn.microsoft.com/SPDisposeCheck
SPDisposeCheck is a tool that helps developers and administrators check custom SharePoint solutions that use the SharePoint Object Model helping measure against known Microsoft dispose best practices

Thursday, April 18, 2013

Sharepoint Delegate control - Notification bar in sharepoint

In this post I am going to show how to add a notification bar in the sharepoint site(Notification bar is used to display notification to the users about the site maintenance activity)
Step 1: Override the delegate control in the master page
Step 2: Associate this delegate control as sharepoint feature(This will be usefull for the site admin, whenever its required they will be activate and deactivate this feature)
Open Visual Studio and create new project called NotificationFeature
Add new sharepoint Feature Called NotificationFeature and set the scope to Site level.
Add new user control called NotificationControl(Map to sharepoint control template folder at the 14 hive)
Open NotificationControl.ascx file and add the below code to display notification bar.
Here I have added jQuery reference and javascript function which set the div background color and div tag to display the message. (Adding jQuery reference and validate method is optional we can set the background color for the div tag directly)
Add New Empty element to the project and name it as NotificationControl.
Open Element.xml file and add below code.
<Control Id="AdditionalPageHead" ControlSrc="~/_CONTROLTEMPLATES/NotificationFeature/NotificationControl.ascx" Sequence="15100" /> Finally our Element.xml file looks like below.

Add jQuery file to the layouts folder. This step is optional since I am applying style for the div element through jQuery I am using this else we can skip this step.
Final structure of our project:
Build, Package and deploy the solution. And activate the Notification Feature at the site level.
Now our notification bar will be displayed when the user open the site.