Thursday 20 December 2007

Custom Styles appear in edit page tool bar in Sharepoint

To add custom styles to appear in the Styles dropdown in a tool bar while editing a page in Sharepoint add the following to the controls.css file that can be found under Style Library/en-us/Core Styles/controls.css in sharepoint designer

.ms-rteCustom-AnyName1, .AnyName1
{
font-weight: bold;
font-family: Arial;
font-size: 14pt;
color: #8C0000;
text-transform: capitalize;
}

.ms-rteCustom-AnyName2, .AnyName2
{
font-weight: bold;
font-family: Arial;
font-size: 14pt;
color: #8C0000;
text-transform: capitalize;
}

Tuesday 4 December 2007

System.Runtime.InteropServices.COMException (0x8001010A): Creating an instance of the COM component with CLSID {00020820-0000-0000-C000-000000000046}

System.Runtime.InteropServices.COMException (0x8001010A): Creating an instance of the COM component with CLSID {00020820-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 8001010a.

I got this error when working on an Excel Automation Process. Excel throws this error message which is not very useful at all. The same code was working on other servers but on 1 particular server I kept getting this error.

Solutions:

Reboot the server! To be more precise I killed the Process EXCEL in task manager and the application started working again. The error code 8001010a usually means that the application is busy so had a look on the task manager.

Monday 19 November 2007

Visual Studio shows Dots for spaces

Ctrl-R followed by Ctrl-W will enable/disable the feature in Visual studio which will show dots for spaces, very annoying.

Thanks thekua for the solution http://www.thekua.com/atwork/2007/03/21/visual-studio-2005-annoyances/

Thursday 15 November 2007

Pound Symbol £ does not appear correctly

When typing in the £ symbol in the HTML editor it might not appear as expetced in the browser. For me it appeared as a box. There are 2 options around this..

1. Convert the value into a string with formatter currency
eg. double val = 5.5; val.ToString("c");

2. If hardcoding the html than use this £ for the Pound symbol.

Tuesday 13 November 2007

Request Failed: Unable to connect to remote server" error

When using the System.net.webclient class I recieved the error message "Request Failed: Unable to connect to remote server". The client had a proxy server so I thought maybe that was causing the problem and adding the proxy settings and it worked. And When I had a close look at I found out that If I added the proxy lines below, it still worked even though the proxy address is commented. I found this a bit wierd.

WebClient web = new WebClient();
WebProxy proxy = new WebProxy();
//proxy.Address = new Uri("*************:8080");
web.Proxy = proxy;

Even though I am creating an object of type Proxy I am not assigning the proxy address and it is working. But If comment the line //web.Proxy = proxy then it fails...I am not sure why.

Wednesday 7 November 2007

ErrorInstalling IIS after .Net 2.0

The error message below will be displayed when IIS is installed on a Windows XP machine that has .Net 2.0 already installed.

NullReferenceException: Object reference not set to an instance of an object.] System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name) +1126 System.Web.Hosting.ISAPIWorkerRequest.ReadRequestHeaders() +121 System.Web.Hosting.ISAPIWorkerRequest.GetKnownRequestHeader(Int32 index) +126 System.Web.Hosting.ISAPIWorkerRequestInProc.GetKnownRequestHeader(Int32 index) +104 System.Web.HttpRequest.FillInHeadersCollection() +30 System.Web.HttpRequest.get_Headers() +55 System.Web.Security.CookielessHelperClass.GetCookielessValuesFromHeader() +16 System.Web.Security.CookielessHelperClass.Init() +23 System.Web.Security.CookielessHelperClass.RedirectWithDetectionIfRequired(String redirectPath, HttpCookieMode cookieMode) +13 System.Web.Security.FormsAuthenticationModule.OnEnter(Object source, EventArgs eventArgs) +226 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

The fix is to run C:\windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i in your command prompt.

Thanks aoporto for this fix..http://support.infogenium.com/forums/26/ShowPost.aspx

Tuesday 23 October 2007

Reading from an Excel Sheet and inserting into a Custom List in Sharepoint

//Add reference to using Microsoft.Office.Interop.Excel;

Application ExcelObj = new Application();
Workbook workBook = null;
Worksheet ws = null;

workBook = ExcelObj.Workbooks.Open(_tempFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

ws = new WorksheetClass();
ws = (Worksheet)workBook.ActiveSheet;

Range range = ws.get_Range("A" + i.ToString(), "J" + i.ToString());

System.Array myvalues = (System.Array)range.Cells.Value2;

if (myvalues.GetValue(1, 1) != null)//Check that the row has data
{
String example1 = (string)myvalues.GetValue(1, 1);
String example2 = (string)myvalues.GetValue(1, 2);

..
..
..

//Insert into Sharepoint

SPWeb web = SPContext.Current.Web;
SPListItemCollection listItems = web.Lists[_sharepointListName].Items;

SPListItem newListItem = listItems.Add();
newListItem[ColumnName1] = example1;
newListItem[ColumnName2] = example2;
..
..
..
..
}

//After working with Excel it is important to close the procsses that Excel uses other wise in your task manager you will find loads of Excel processes without being closed, the following method does a complete close down of all excel processes

private void ReleaseResources(Application ExcelObjk)
{
try
{
if (ws != null)
Marshal.ReleaseComObject(ws);

if (workBook != null)
{
workBook.Close(false, false, Type.Missing);
Marshal.ReleaseComObject(workBook);
}

if (ExcelObjk != null)
{
ExcelObjk.Workbooks.Close();
ExcelObjk.Quit();
Marshal.ReleaseComObject(ExcelObjk);
}
CallGarbageCollectorToCleanupReleasedMemory();
}
catch (Exception x)
{

}
}

private void CallGarbageCollectorToCleanupReleasedMemory()
{
GC.Collect();

//The bottom 2 lines are unnecessary and very dirty but can be used in worst case scenarious when you absolutely have to, have included here just to give an idea that this is possible

Process[] processes;
processes = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (Process p in processes)
p.Kill();
}

Monday 22 October 2007

Error 1920.Service Active MQ failed to start When installing Vamosa

When installing Vamosa If you recieve "Error 1920.Service Active MQ (ActiveMQ) failed to start. Verify that you have sufficient privileges to start system services", check the log file under c:\vamosa\activemq\bin\wrapper.log and you will notice that you will see an error message like

STATUS | wrapper | 2007/10/22 09:26:11 | Launching a JVM...
FATAL | wrapper | 2007/10/22 09:26:11 | Unable to execute Java command. The system cannot find the file specified. (0x2)
FATAL | wrapper | 2007/10/22 09:26:11 | "%JAVA_HOME%\bin\java" -
FATAL | wrapper | 2007/10/22 09:26:11 | Critical error: wait for JVM process failed..

This is because a new environment variable has to be created called "JAVA_HOME", this should point to the root folder of where your JDK is installed.

Friday 12 October 2007

A Dummies guide to Creating a custom Web Part for SharePoint Server 2007

We have put together simple but important things about the development and deployment of web parts for Sharepoint server 2007.

1. Download the SharePoint Add on for Visual Stuido, this will create new templates so when you click on New Project in Visual Studio you will see new project types like "WebPart http://www.microsoft.com/downloads/details.aspx?FamilyID=19f21e5e-b715-4f0c-b959-8c6dcbdc1057&DisplayLang=en

2. Sharepoint web parts can be treated like Custom Server Controls in .Net. The difference is SharePoint references are added automatically when you create a new web part project and deploying to SharePoint is as easy as pressing F5.

3. Use the SharePoint object model classes like SPWeb, SPContext, SPListItemCollection and SPListItem to work with lists in Sharepoint. This way there is no need to add a web reference to the web service that is exposed by SharePoint.

4. Deploying - If Sharepoint is installed on the development machine then pressing F5 will automatically add the webpart in the GAC, mark the webpart as safe in the sharepoing web.config files and import the .dwp file into sharepoint. So to add the custom web part that we created, all we need to do is click Add web part and our custom web part should appear in the list of web parts along with other web parts.

5. When you run the project (F5) a setup.bat file is created in the Debug Folder or Release folder (depending on build type you do). Copy this folder accross to the server where you want to install this webpart and run the setup.bat file in the folder. Now this would do the steps mentioned in point 4 above so the webpart can be added to the sharepoint site by using the Add Web Part tab.

.Net Execution Timout - ThreadAbortException

I had to create a custom web part that read a list of items from an excel sheet and upload it into sharepoints custom list. In that excel sheet there was a url field which pointed to pdf documents on the internet, so while uploading contents into the custom list I also had to download the pdf documents and add them to the custom list as attachments.

During this I noticed that a “system.Threading.ThreadAbortException: Thread was being aborted.” exception was being thrown approximately after the web part was running for 125 seconds. I found out that the system was throwing this exception and it was because by default .Net threads have an execution time out limit of 110 seconds. To fix this we need to modify the web.config file that Sharepoint uses. The following line needs to be replaced

with

The executionTimeout value should be set depending on the number of items in the excel sheet. Please refer to the support document below for information on this http://support.microsoft.com/kb/928756.

The other thing to note is when the ThreadAbortException is thrown by the system you cannot catch it in a try catch. If you use a try catch it will go through the catch and at the end of the catch statement it will throw it again.

But if the ThreadAbortException is thrown by our code then we can abort it by using the class Thread.ResetAbort in the finally clause. More info on that can be found on
http://msdn2.microsoft.com/en-gb/library/system.threading.threadabortexception.aspx

Thursday 13 September 2007

Wednesday 22 August 2007

The ultimate Off-roader!

At last I have bought the vehicle that I have been wanting to take to masinagudi for the past 15 years - the Gypsy! And no surprise I made full use of the gypsy in my first trip! Check the photos below..

Modified my Honda Civic

I have done some face lifting to my honda civic in India, It was done by Kitup Rajiv.

Tuesday 21 August 2007

Passing NameValueCollection to WebService

There might be a need to pass in a NameValueCollection class object into a webService. Since you can only pass in serialized objects into a web service it is not a straight forward process. One of the ways to do it is to have a Class that stores the name value pair and add it to an arraylist object. Then pass the arraylist.ToArray() object to the webservice. The other way is to use a string array. I have shown the code to do it using a string array below.

This is the method that takes in a NameValueCollection and returns an Array
///
/// This Method converts the NameValueCollection of all the parameters
/// into an arraylist of string arrays
///

///
///
private Array ConvertNameValueCollectionIntoArrayList(NameValueCollection nvcParams)
{
ArrayList aList = new ArrayList();
string[] strNameValue;

IEnumerator paramsEnum = nvcParams.GetEnumerator();

int i = 0;
while (paramsEnum.MoveNext())
{
string name = nvcParams.GetKey(i);
string value = nvcParams[name];

strNameValue = new string[2];
strNameValue[0] = name;
strNameValue[1] = value;

aList.Add(strNameValue);
i++;
}
return aList.ToArray();
}

Now this Array can be passed into the web service and de-decoded as shown below..

------WebService----

//The ArrayListObject below is the array that contains the arraylist
[WebMethod]
[XmlInclude(typeof(string[]))] //This line is very important
public void Initiate(Object[] ArrayListObject, string .....)
{
ArrayList al = new ArrayList(ArrayListObject); ParameterValue[] Params = ConvertNameValueCollectionIntoParams(al);
..
..}

private ParameterValue[] ConvertNameValueCollectionIntoParams(ArrayList nvp)
{
ParameterValue[] parameters = new ParameterValue[nvp.Count];
for (int i = 0; i < nvp.Count; i++)
{
ParameterValue parameter = new ParameterValue();
string[] strNameValuePair = (string[])nvp[i];
parameter.Name = strNameValuePair[0];
parameter.Value = strNameValuePair[1];
parameters[i] = parameter;
}
return parameters;
}

Tuesday 14 August 2007

Quick Tips C#

This post will contain a list of quick tips and good practices in c# that I use..I will be updating this list as I find new things...If you have anything that you would like to contribute, please post them in the comments...cheers


1. Use string.IsNullOrEmpty() - short and sweet to check whether a string is null or empty

2. goto keyword, this is a very useful keyword that can be used to skip certain lines of code in a method - http://msdn2.microsoft.com/en-us/library/13940fs2(VS.71).aspx

Monday 13 August 2007

Highlighting Row in GridView

I recently had a situation where I had to highlight a row in the gridview if the value of the column Today was 0. Found out there was a very easy way to do it using the OnRowDataBound Event of the GridView, the code's below..

protected void GridViewID_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = row.DataItem as DataRowView;
string fieldName= drv["FIELDNAME"].ToString();
if (fieldName.Equals("0"))
row.CssClass = "YourCssClassName";
}
}

DataFormatString - Formatting Data in a GridView -HTMLEncode to false

The data in a datagrid can be formatted using the DataFormatString. For example you can add a £ sign or make sure the numbers are formatted to 2 decimal points and so on. The link below gives you a reference list of the things you can do but the most important point is, you need to set the HtmlEncode Property to false, or else you will not get the desired results. This one can get you because it is not stated clearly anywhere and spent half an hour trying to figure out why it wasnt working!

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

Programmatically Adding Columns to GridView

There are cases where you might want to use the same GridView to display diferent columns in a page. This is possible by programmatically adding the columns using the BoundField Object. The key thing to note here is the diference in adding a column to a DataGrid and a GridView, to add a bound column in the DataGrid you should use the BoundColumn object and to add a column to the GridView, you should use a BoundField object. You can create the gridview in your .aspx page and set the sorting and things like that or you can create the complete gridview in your code behind using the GridView class.
The code below shows how to add columns to a gridview that was created in the .aspx page..

--.aspx page--

AllowPaging="True" AllowSorting="True" PageSize="21" EmptyDataText="No data available."
CssClass="gridstyle" GridLines="None" OnSorting="GridViewID_Sorting" OnPageIndexChanging="GridViewID_Paging"
Width="100%">





--.cs page--

private void DefineGridViewColumns()
{
BoundField column = new BoundField(); //You can use the TemplateField object to add a template column
column.HeaderText = "XX";
column.DataField = "ID";
column.SortExpression = "ID";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);
GridViewID.Columns.Add(column);

column = new BoundField();
column.HeaderText = "RANK";
column.DataField = "RANKNO";
column.SortExpression = "RANKNO";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);
GridViewID.Columns.Add(column);

if (TimeSpan.Equals("TW"))
{
column = new BoundField();
column.HeaderText = "THIS WEEK";
column.DataField = "XX_TARGET";
column.SortExpression = "XX_TARGET";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(10);
GridViewID.Columns.Add(column);
}
}

Wednesday 1 August 2007

Error "The permissions granted to user '\IUSR_' are insufficient in Reporting Server

If you get this error "The permissions granted to user '\IUSR_' are insufficient for performing this operation." when you work with Reporting Server, it is because the reporting server's virtual directories under IIS is running in 'Annonymous Mode'. To fix this open IIS, right click on the virtual directory that your report server uses, click on the tab 'Directory Settings' and click edit under 'Authentication and Access Control' and untick the 'Enable Anonymous Access'.

Sunday 29 July 2007

Issues with Integrating SharePoint 2007 and SQL Server 2005 Reporting Services

While installing the Reporting Services add on for SharePoint 2007, the reporting services section does not appear on the SharePoint Central Administration Console if not installed by a user with the needed rights. The installation wizard would give you a installation successful message and the required files also seems to appear in the installation folder. The reason for this is that the user installing the add on should have domain access rights and should be a Web Farm Administrator and Site Administrator.