Tuesday, 6 April 2010

Show traffic lights in SP List based on Checkbox(Yes\No) column

I had to create a SharePoint list with traffic lights as shown below.


The decision for color of traffic light is from a Checkbox field.

One calculated field for each traffic light has to be created with formula like
="< d i v >< i m g src="http://www.blogger.com/_layouts/images/KPIDefault-%22&IF([Gate%201]=TRUE,"0.GIF","2.GIF")&"" /> < / d i v >"

 

The idea is to use the existing SharePoint KPI images.

Since the formula is based on Checkbox, the following IF condition will work.
="< d i v >< i m g src="http://www.blogger.com/_layouts/images/KPIDefault-%22&IF([Gate%201]=TRUE,"0.GIF","2.GIF")&"" /> < / d i v >"
"


Did NOT work:
="< d i v >< i m g src="http://www.blogger.com/_layouts/images/KPIDefault-%22&IF([Gate%201]=Yes,"0.GIF","2.GIF")&"" /> < / d i v >"

="< d i v >< i m g src="http://www.blogger.com/_layouts/images/KPIDefault-%22&IF([Gate%201]=1,"0.GIF","2.GIF")&"" /> < / d i v >"


It will just set the HTML tags to the calculated field based on the checkbox value.
How to make the browser to interpret the HTML tags and display the image.

Here comes the handy script from http://blog.pathtosharepoint.com/2008/09/01/apply-color-coding-to-your-sharepoint-lists/

A CEWP has to be added at bottom of the page and include the following script in the CEWP, thats all.
It does magic for you...

< s c r i p t type="text/javascript">
//
// Text to HTML
// Feedback and questions: Christophe@PathToSharePoint.com
//
var theTDs = document.getElementsByTagName("TD");
var i=0;
var TDContent = " ";
while (i < theTDs.length) { try { TDContent = theTDs[i].innerText || theTDs[i].textContent; if ((TDContent.indexOf("") >= 0)) {
theTDs[i].innerHTML = TDContent;
}
}
catch(err){}
i=i+1;
}
//
// ExpGroupRenderData overwrites the default SharePoint function
// This part is needed for collapsed groupings
//
function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
var tbody=document.getElementById("tbod"+groupName+"_");
var wrapDiv=document.createElement("DIV");
wrapDiv.innerHTML="
<><><><>"+htmlToRender+"
";
var theTBODYTDs = wrapDiv.getElementsByTagName("TD"); var j=0; var TDContent = " ";
while (j < theTBODYTDs.length) { try { TDContent = theTBODYTDs[j].innerText || theTBODYTDs[j].textContent; if ((TDContent.indexOf("") >= 0)) {
theTBODYTDs[j].innerHTML = TDContent;
}
}
catch(err){}
j=j+1;
}
tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild,tbody);
}
< / s c r i p t >

Monday, 4 January 2010

ReportViewer in WebPart (SharePoint)

Using ReportViewer control inside SharePoint Webpart is bit tricky and challenging.
Below are the steps to make it happen.

1. web.config (of SharePoint) changes (Extra space has been included after > and < to make it appear in blog)

add below line to < httpHandlers > section

< add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" / >


and below line to < assemblies >

< add assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" / >

2. Add Report (.rdlc) and DataSet (.xsd) files to Solution


Sample Code

------------------------------------------------------------
using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace ReportWebpart
{
public class ReportsWP : WebPart
{
Label lblTime;
ReportViewer ReportViewer1;
TextBox tb1;
Button btn;

protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
lblTime = new Label();
lblTime.Text = "Enter Plant No.";

tb1 = new TextBox();

btn = new Button();
btn.Text = "Refresh Report";
btn.Click += new EventHandler(btn_Click);

ReportViewer1 = new ReportViewer();
ReportViewer1.Width = Unit.Percentage(80);
ReportViewer1.EnableViewState = true;
ReportViewer1.LocalReport.ReportPath = @".\Report1.rdlc"; // Copy the rdlc file to root of IIS
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.ID = "rprtViewer1";

this.Controls.Add(lblTime);
this.Controls.Add(tb1);
this.Controls.Add(btn);
this.Controls.Add(ReportViewer1);
}
catch (Exception ex)
{
string k = ex.Message;
}
}

void btn_Click(object sender, EventArgs e)
{
DataTable newDT = GetTable();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_Employee", newDT));//Name of Dataset (.xsd file)+ '_' + table name
ReportViewer1.ID = "rprtViewer1";
ReportViewer1.DocumentMapCollapsed = true;
ReportViewer1.LocalReport.Refresh();
}

private DataTable GetTable()
{
//FRAME THE QUERY AS NEEDED. THE FIELDS RETURN SHOULD MATCH WITH REPORT'S DESIGN TIME FIELDS
}
}
}
----------------------------------------------------------

NOTE: If you are getting following error while accessing the WebPart page containing ReportViewer

Microsoft.SharePoint.Portal.Analytics.UI.ReportViewerMessages, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c does not implement IReportViewerMessages or could not be found


Try either of following to fix it

1In web.config of SharePoint, comment the following line under
1. <appsettings >
< ! -- < add key="ReportViewerMessages" value="Microsoft.SharePoint.Portal.Analytics...... / > -- >

or

2.  Add  following to < appsettings >
< remove key="ReportViewerMessages" >


------------------------------------------------------

Tuesday, 22 December 2009

LinkButton in SharePoint - Not firing Click Event

I had a sitution to stream a file from SQL Server.
ASP.Net LinkButton was used in the ITemplate to use in GridView and Click event was defined with statements like

response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
response.AddHeader("Content-Length", blob.Length.ToString());
response.ContentType = "Application/x-msexcel";
response.BinaryWrite(blob);



Everything was working perfect in WebApplication. When the webpart was deployed to SharePoint, the server side click event fired only once. After that the link became irresponsive. If the page is refreshed, the link worked once again for 1st time.

After several googling, found that SharePoint stores time-stamp on client side for security reasons and avoids multiple post-backs for the same control.

There are 2 ways to over-come this issue. Just add an appropriate line as below

1. Generic way

imageButton.OnClientClick = "this.form.onsubmit = function() {return true;}";
or
hyperLink.OnClientClick = "this.form.onsubmit = function() {return true;}";
or
linkButton.OnClientClick = "document.getElementsByTagName(\'form\')[0].onsubmit = function() {return true;}";



2.Specific to SharePoint

linkButton.OnClientClick = "_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;";

After adding single line (one of the above), the webpart worked like a charm.

Friday, 18 September 2009

Read RSS and Atom feed in C#

I had a requirement to read RSS/Atom feeds from blogs and store in SharePoint List.
I remember we had to go for separate framework to read the feeds and parse it. In that case we need to refer the framework in our project and install that assembly in Bin or GAC.

Now with .Net 3.5, life has become much easier.

Yes, with the namespace 'System.ServiceModel.Syndication' it is just few lines of code to read and parse the feeds either RSS or Atom.

Just add reference to 'System.ServiceModel.Web' to your project and include above namespace in your class, thats it.

Below is the code i used.
XmlReader reader = XmlReader.Create(curUrl);
SyndicationFeed feed = SyndicationFeed.Load(reader);
string title = String.Empty;
string link = String.Emplty;
DateTime dateTime = null;
foreach (var feeditem in feed.Items)
{
title = feeditem.Title.Text;
dateTime = feeditem.PublishDate.LocalDateTime;
link = feeditem.Links[feeditem.Links.Count -1].Uri.OriginalString;

//Do your tasks here
}

Easy to deploy and maintain too.
Happy Coding
வாழ்க வளமுடன் :)

Useful SharePoint Links

Some Useful SharePoint links


Accessing Data from Workflow Association and Initiation Forms in Windows SharePoint Services 3.0
Best Practices: Common Coding Issues When Using the SharePoint Object Model
Best Practices: Using Disposable Windows SharePoint Services Objects
Checklist for Creating SharePoint Web Parts
Creating Custom Timer Jobs in Windows SharePoint Services 3.0
Creating a Windows SharePoint Services 3.0 Custom Field by Using the EntityPicker
Creating a Windows SharePoint Services 3.0 Web Part Using Visual Studio 2005 Extensions
Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0 (Part 1 of 2)
Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0 (Part 2 of 2)
Securing Application Pages in Windows SharePoint Services 3.0
Selective Content Migration in Windows SharePoint Services 3.0
Understanding and Creating Customized and Uncustomized Files in Windows SharePoint Services 3.0
Understanding the Administrative Object Model of Windows SharePoint Services 3.0
Usage Event Logging in Windows SharePoint Services 3.0
Using Solution Packages to Deploy Features and Content in Windows SharePoint Services 3.0
Workflow Scalability and Performance in Windows SharePoint Services 3.0
Working with ASP.NET 2.0 Web Parts and Windows SharePoint Services 3.0

Wednesday, 22 July 2009

Building CAML Query

Found this useful site details about building CAML query.

http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

Thanks Boske.