Wednesday, December 30, 2009

LINQ to SharePoint [Concurrency Problem] and how to Resolve .

What is Concurrency ?

The situation in which two or more users at the same time try to update the same database row, here in SharePoint row as list item column value . Like LINQ to SQL  LINQ to SharePoint also supports optimistic concurrency control.

In the LINQ to SQL object model, an optimistic concurrency conflict occurs when both of the following conditions are true:

  • The client Apps tries to submit changes to the database( In SharePoint list) .

  • One or more update-check values have been updated in the database since the client last read them.

Here we are going to test this issue and see how we can solve this conflicts.

1) Create a console application  and add Generated DataContext Class which is created using SPMETAL.EXE.

2) Now using LINQ to SharePoint query the custom list  where Title is Manager and update a Title column value as user inputted (Read from console apps) .

3) Now execute the two instance of same console App and test it .

image 

image

Execute two instance of the above code.

image

First press enter in above App Instance 1 this will update the SharePoint list as shown below(see selected list ).

image

Now Press Enter in above App Instance 2 . Now you will get the following Exception .

image

Let’s modify the Console application to handle the above exception.

image

image

 

image

RefreshMode.KeepCurrentValues will overwrite the entity values in the list with the data stored in your current entity in memory, including the original value that you didn’t change; an alternative solution would have been to merge your changes with the current values of the database by using RefreshMode.KeepChanges.

 

http://msdn.microsoft.com/en-us/library/bb399373.aspx refer this URL for Optimistic concurrency overview. 

Nandri(Thanks)

SreenivasaRagavan.

How to Display CAML Query Generated by LINQ to SharePoint Providers

When we write LINQ to SharePoint Query basically this query will be converted to CAML Query by SPLINQ Providers. Here i am going to show  when we execute the LINQ To SharePoint query  how to get Equivalent of CAML Query for LINQ query.

CAML – Collaborative Application Markup Language.

image

CAML Query

image

LINQ Query

image

EQ  CAML Query

<View><Query><Where><BeginsWith><FieldRef Name="ContentTypeId" /><ValueType="ContentTypeId">0x0100</Value>
</BeginsWith></Where></Query>
<ViewFields><FieldRef Name="Fname" /><FieldRef Name="Lname" /><FieldRef Name="Email" /><FieldRef Name="Phone" />
<FieldRef Name="ID" />
<FieldRef Name="owshiddenversion" /><FieldRef Name="FileDirRef" /><FieldRef Name="Title" />
</ViewFields><RowLimit Paged="TRUE">2147483647</RowLimit>
</View>

LINQ Query

image

EQ CAML Query

<View><Query><Where><And><BeginsWith><FieldRef Name="ContentTypeId" /><Value Type="ContentTypeId">0x0100</Value></BeginsWith><Eq><FieldRef Name="Title" /><Value Type="Text">Manager</Value></Eq></And></Where></Query><ViewFields><FieldRef Name=
"Fname" /><FieldRef Name="Lname" /><FieldRef Name="Email" /><FieldRef Name="Phone" /><FieldRef Name="ID" /><FieldRef Name="owshiddenversion" /><FieldRef Name="FileDirRef" /><FieldRef Name="Title" /></ViewFields><RowLimit Paged="TRUE">21474836
47</RowLimit></View>

Result


Manager Chaz  P  2323232322
Manager Jaycy  Shuchi  232322222222
Manager Ganesh  S  3333333333
Press any key to continue . . .

This is one-way to get/Generate  CAML query.[ without pain we can gain here]  :)

Nandri (Thanks)

SreenivasaRagavan.

LINQ to SharePoint –PART-II

In this Part-II i am going to show you how to perform CURD Operations on SharePoint custom list using LINQ to SharePoint provider. Here i am going to use the custom list which we used in Part-I.

DataContext class is the main class which will keep tracks of Entity changes. DataContext class has SubmitChanges method using this we can save the List item .

image

Datacontext Class SubmitChanges() method:

  image

Code snippet to create new Item and saves to SharePoint List

image

List Items Before Inserting new Item

image

List Items after Inserting new Item

image

Same way we can use DataContect object to do Update , Delete Item on List as well using LINQ to SharePoint.

Nandri(Thanks)

SreenivasaRagavan

LINQ TO SharePoint [SPMETAL.EXE]-PART-I

In this blog post i am going to show how to query SharePoint custom list  using LINQ to SharePoint providers. To query SharePoint list using LINQ to SharePoint first we need to create strongly typed classes using SPMETAL.EXE then we can able to program against SharePoint list.

LINQ to SharePoint provides

1) Strongly-typed queries and Intelligence Help while building Query.

2) Compile-time Check.

and LINQ to SharePoint going to get rid of CAML ( Collaborative Application Markup Language ) .

Steps to follow to do LINQ to SharePoint

1) Pass a SharePoint site or site collection URL  as parameter to SPMETAL.EXE command like utility.  you can find this utility  at 14 hives dir

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

2) SPMETAL will Generates Strongly typed entity classes for all  items ( TASKS, LIST, CALNDER etc..)  and emits DataContext   class.

3) Refer this DataContext class in any project and reference Microsoft.SharePoint.Linq.dll. now you can start doing LINQ to SharePoint

SPMETAL.EXE  Parameter Syntax

SPMETAL /web <SiteUrl> /namespace:<NamespaceName>  /code:<outputfilename.cs>

Here is the my site which has custom list

image

SPMETAL /web:http://localhost:44119/ /namespace:SreeniContacts  /code:SreeniLinqCtx.cs 

image

LINQ to SharePoint Query  (  Console application project )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SreeniContacts;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SreeniLinqCtxDataContext ctx = new SreeniLinqCtxDataContext("http://localhost:44119/");

var query = from contact in ctx.Sreenicontacts
where contact.Title =="Manager"
select contact;
foreach (var item in query)
{
Console.WriteLine(item.Title +" "+ item.Fname +" "+ item.Lname +" "+ item.Phone );
}
}
}
}
NOTE: choose Any CPU as your Platform targets otherwise you get the following error. 
image 
Right click the project select properties set Platform Targets as Any CPU. 
image 
 
image 
DataContext object will keep tracks of entity changes. using DataContext object we can do CURD operations on the list.
Part-II we will see how we can add Items to custom list using DataContext object.
Nandri(Thanks)
SreenivasaRagavan.

Tuesday, December 29, 2009

ADO.NET programming against Windows SQL CE 3.5 part-VI

In this Part-VI blog  series we are going to use ADO.NET to program against SQL CE.  SQL CE Database is a file based DB not like  Desktop MS-SQL server (Windows Service),so to access DB File In windows mobile there is no such thing as a current directory (like C:\). for example If your app folder is Program Files\SreeniMobileApps, I'd expect  my contact Database is located  @ "Data Source=Program Files\SreeniMobileApps\Contacts.sdf".

To get Database file path use the following code snippet

private static stringInitializeConnectionString()
    {
           string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
           return "Data Source=" + Path.Combine(appPath, "Contacts.sdf") + " ;pwd=;";
       }

Now let’s connect Contacts DB and query  via ADO.NET API.  (Note: The contacts  Database we were created  in part-V ).

I have Menu option 5 as view Contact in my mobile application when user selects that options it will shows all the contacts in DataGrid.

image

Here is code snippet which connect to Contacts DB and Query the Contact Table .

private void ViewContact_Load(object sender, EventArgs e)
       {
           string selectQuery = "Select * from Contact";
           using (SqlCeConnection conn = new SqlCeConnection(InitializeConnectionString()))
           {
             
try
              
{
                   SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(selectQuery, conn);
                   conn.Open();

                   DataTable dataTble = new DataTable("ContactData");
                   dataAdapter.Fill(dataTble);
                   dataGrid1.DataSource = dataTble;
                   
               }
               catch (SqlCeException ex)
               {
                   
               }

           }
       }
   }

Result

image

Nandri(Thanks)

SreenivasaRagavan.

How to create SQL CE database Part -V

In this part-V blog series we are going to see how to create Simple SQL Compact Edition DB .

1) Start Visual studio  and then  create or open existing  Smart device project.

Click Data in the VS.NET IDE  menu and select "Add New Data Source..." to see the following dialog window

image

Select Database and click Next .

Now we need to select MS SQL CE 3.5 data source and click OK.

image

Click Create and provide the File name for the SQL CE DB.  you can provide Encryption password but in my case i am going to leave blank.

NOTE: SQL CE edition DB have  .SDF extension .

image

Now open the Database and create new table and create the Columns for the newly created table

image

 

image

Now build the project and deploy to the Emulator .

image

Open the DB from emulator and you can view the DB as shown.

In Query Analyzer, expand the tree under the Objects tab.  Query Analyzer allows you to explore and manage SQL Mobile databases from within the PDA environment.

image

image

 

Nandri(Thanks)

SreenivasaRagavan.

Windows Mobile Application Development Part-IV [.NET CF]

In this Part-IV blog series we are going to build and Packaging a Smart Device Solution for Deployment .

To deploy hand held application we need to build CAB file project. In Hand held device world applications are build as CAB project file which contains application output file , resource file etc..

image

image

image

Now you can follow the next screen and add other files to CAB project like Database file , Resource file etc..

clip_image002

image

Now build the CAB project and move output files to Hand held device via Cradle, its always good to create new folder on device and move to Hand held device.

Now you browse folder on hand held device  where you copied the files now run the CAB project choose Device options and installed it .

Nandri(Thanks)

SreenivasaRagavan.

Sunday, December 20, 2009

Windows Mobile Application Development [.NET CF] Part III

In this part-III series i am going to show how to enable SIP – Soft Input Panel keyboard when user Inputs. To enable SIP we need to add Microsoft.WindowsCE.Forms and use InputPanel class for working with SIP Keyboard interface.

I have created a User Interface for  adding a new contact. whenever user clicks on First Name text box the SIP keyboard will pops up. In other words when ever text box receives  textBox1_GotFocus(object sender, EventArgs e) event the SIP keyboard will pops up, if it receives textBox1_LostFocus(object sender, EventArgs e) event then SIP keyboard will disappear.

image

Now i clicked on First Name Texas box i got the SIP keyboard Pop-Up.

image

Here is the code snippet to enable Input panel

image 

Nandri(Thanks)

SreenivasaRagavan.

Windows Mobile Application Development [.NET CF] Part-II

In this part –II  series I am going to show you how to add or create a Menu items in Windows Mobile application .

First Create a new Smart device project and select  Device application template.

Second Make sure that you have selected right Target platform and CF version (3.5).

Now open up the Form1.cs in design mode Click the bottom of Form1.cs   as shown in below and add /Create new menu items.

image   image 

NOTE:

Windows Mobile Development (Compact Framework) != Windows Forms Development. For Example Windows mobile form could have only one "minimize" button in the upper right corner of a window. There are two types of that button depending on MinimizeBox property of a form.

if  (MinimizeBox is set to True)

           " System will Show X” . when you  click on X this does not closes the window(from) it sends to back.

image

if ( MinimizeBox is set to False)

    "System will Show OK” . when you  click on OK  this  closes the window(from) and application will shut down.

image

 

 

Now Compile and deploy the application into Windows 6.5 emulator. 

 image

Nandri(Thanks)

SreenivasaRagavan.

Windows Mobile Application Development [.NET CF]- Part-I

Past 5 month i was not regular or not blogging often because I have been working on a Windows Mobile development project.  For me this is a new  technology, but i am able to do understand  Mobile SDLC very easily within no time .

This  windows mobile application developed for a big chemical company were their truck drivers go out to the filed and deliver/Treat the well and capture the Well info , what chemical they delivered, How much (GALLON) etc.. per day they may treat 60 to 70 wells approx. At the End of the day truck driver comes to warehouse  and connect to Company network (Wireless) and Sync or Upload data to MS-SQL Server and  then push to back Oracle Backend system via Linked servers.   This well treatments and pipeline solutions designed to enhance production and to meet out the business process and Real time Inventory maintenance.

Here are sample screen shots of the windows mobile application i developed.

Delivery /Treatment Input screen

image 

Synchronization Options  from Device (SQL CE) to MS-SQL Server [Sync options automatically checked (selected) based on device connections]. Right now windows mobile device

is Cradle.

image

I wanted to write  series of blog on how to create a complete Windows Mobile application, targeting both Windows Mobile 6.1 and 6.5 Standard and Windows Mobile 6 Professional Devices. 

Now let see how we can create simple Windows Mobile Application and deploy to Emulator  and test it before pushing to actual device.

Step 1: What are development tools needed to build windows Mobile Applications

1) VS.NET 2008 ( you can use 2005 too)

2) Windows Mobile 6 Professional and Standard Software Development Kits Refresh ( you can download it from :http://www.microsoft.com/downloads/details.aspx?FamilyID=06111a3a-a651-4745-88ef-3d48091a390b&displaylang=en)

3) Windows Mobile 6.1 Emulator Images ( you can download it from :http://www.microsoft.com/downloads/details.aspx?familyid=3D6F581E-C093-4B15-AB0C-A2CE5BFFDB47&displaylang=en)

4) You need ActiveSync or Windows Mobile Device Center. You will need to have ActiveSync or Windows Mobile Device Center in order to deploy applications to a Windows Mobile device or to an emulator.

    1. If you development machine is Windows XP then you need to have  ActiveSync
    2. If your development machine is Vista/Windows 7 you need to have Windows Mobile Device Center.

You can download it from http://www.microsoft.com/windowsmobile/en-us/downloads/microsoft/activesync-download.mspx

Step 2 : Create windows Mobile Project , Deploy and Test it.

image

image

image

image

 

private void button1_Click(object sender, EventArgs e)
       {
           MessageBox.Show("Windows Mobile Button Contorl.");
       }

Press Ctrl+F5 and choose where to deploy. Here i am going to deploy this application in windows 6.5 Pro VGA Emulator.

image

Device Emulator. (Windows 6.5)

image

image image

 

Nandri(Thanks)

SreenivasaRagavan