Sujeewa Ediriweera

February 18, 2012

List Join in SharePoint 2010 using CAML

Filed under: SharePoint — sujeewa.ediriweera @ 1:48 pm
Tags: , , , ,

Overview
As a typical .net developer I have used SQL table join operation frequently.  SQL gave us  very rich way of manipulating data and we felt the luxury of it. The real fun happened when we where dealing with SharePoint 2007. In SharePoint 2007 we didn’t have direct way to join list and filter data programmatically. Link list concept was there but we need to use SharePoint designer to do it. Basically CAML query didn’t support list join operation (SharePoint 2007). In SharePoint 2007 my desired way of doing list join operation was through LINQ . But we had some performance issues with it. At some point we used third party tool to do the join operation for us. E.g. Bamboo List Roll-up web part

But thanks to Microsoft in 2010 release of SharePoint, it supports CAML query list join operations with the help of SPQuery class.

Recently I came across a customer requirement where I need to bring three lists information to a single view and present it to user in SharePoint 2010.I knew SharePoint 2010 supports list join operations so I started researching on how we join SharePoint lists using CAML query.

In My example I have employee List and department list. Employee list has lookup column to department list.

Department List

Employee List

Solution

In SharePoint 2010 Use CAML query   and SPQuery class for list join operation.

SPQuery object
Query Filtering , order by commands
Joins Inner Join or Left join commands
ProjectedFields Lookup list fields
ViewFields Available fields in result SPListItem

Code:

Output :

Accessing secure store service programmatically – part 02 (Impersonation)

Filed under: SharePoint — sujeewa.ediriweera @ 3:41 am
Tags: , , , ,

Over view
Although we have the run with elevated privileges option , In some cases  we need to execute specific functions with specific  user account privileges .Impersonation is very common when we are dealing with SharePoint services.

Solution

Initiate site object with different user token and execute code under nitiated site. You can do following

  • Create impersonate SPWeb object inside impersonated site object.
  • Get impersonate SPContext using impersonated site object.
  • Get impersonate SPServiceContext using impersonated site object.

Prerequisites
You need to have a specific domain account to use as impersonate user. In my case I have setup a Secure Service Store Application key to retrieve impersonate user information

https://sujeewaediriweera.wordpress.com/2012/02/15/accessing-secure-store-service-programmatically-part-01/

Code

Code for Impersonation


February 12, 2012

Accessing InfoPath form data programmatically

Filed under: SharePoint — sujeewa.ediriweera @ 4:57 am
Tags: , , ,

Overview:

In my case I have InfoPath form to capture   First Name, Last Name, and Email of customers. I need to access these values programmatically.

Understanding:

Fields declared in custom InfoPath form are stored as ‘my:{fieldname}’ format .

Field name   Display name XML representation
txtFirstName First Name my:txtFirstName
txtLastName Last Name my:txtLastName
txtEmailAddress Email Address my:txtEmailAddress

Solution:

There are many ways to access InfoPath form.  In this solution we are using following steps to retrieve InfoPath form data.

  • Identify field names and their XML representation
  • Read InfoPath form  item from InfoPath document Library
  • Load  attached InfoPath form’s XML representation  to XmlTextReader
  • Read each node using XmlTextReader and extract user input  based on our requirement

Code:

Source Code : ReadFromInfoPathForm

Out put:

February 10, 2012

Template Based Code generation

Filed under: General Development — sujeewa.ediriweera @ 11:02 am
Tags: , ,

Code generation is a widely used concept when it comes to enterprise level software development. Generating code will help to increase the speed of software development while keeping the code consistent across. This is especially useful when it comes to distributed teams where you want to follow a general coding practices/guidelines across developers, code generation will become very handy. When it comes to smaller teams or individual developers code generation is being used to complete the repetitive tasks and save time, while the saved time allows more time to be spent on developing actual business logic.

While code generation can help developers, template based code generation will unleash the power of flexibility in generation code. Templates when used let the solution architects/developers to define the coding guidelines and then generate the code based on that. This approach takes the code generation to a next stage, where you are given a great amount of flexibility to stick to the coding guidelines which you are already put in to practice. So that the change is minimal and the results are maximum.

As you start using the templates and generate code, most of the build errors will be dissolved. As the project become mature, alterations to the code can be done at template level and apply across the code base. Unlike in other scenarios at this point you can trust the code which is generated as generated code has been tested iteratively.

When templates are used in a tired architecture most of the templates can be used across many projects to generate the low level tires. Having reusable templates will let the different project teams to follow the consistent coding guidelines and hence improve the manageability across projects. At any stage team members are distributed among different projects the learning curve will be minimal.

By using a code generation tool developer focus will mainly on business logic and rely on the code generation tool to be generating the code for the low level tires. Normally developers spend 40%-60% of the total time to write repetitive code, which by using code generation, can be cut down and re-invest in other areas to improve the quality of the final product.

Developers are human beings which by nature makes mistakes. As you move towards a code generation tool, the ratio for human errors will be minimal hence will save time with bug fixing cycles. This will result in producing a robust and accurate final product.

Code generation tool can be used to generate reusable set of classes and methods. This is highly depending on how you planned to generate code and how your template generates code. This will eventually set the world “reusable” in the developer’s thought process, hence will result in writing reusable components which can not only be re-used within the project but also can be re-used across many other projects. And this is where template based code generation will give fullest flexibility to developers to think through reusable code and write templates to generate reusable code rather than generating code which is less re-usable.

As discussed above template based code generation will improve the software development life cycle in many different spaces. Key improved areas will be productivity, quality and consistency. So it’s worth investing time on learning and putting template based code generation in to practice if you are keen on producing robust and high quality final software product.

SharePoint Auto Coder SPAC  http://www.youtube.com/watch?v=O9Eey1ipHwI

Create a free website or blog at WordPress.com.