Tuesday, August 4, 2009

Debugging custom SharePoint applications

I got an excellent article which outlines common debugging issues and their solutions.Here it goes:

First thing you need to do to avoid the old fashioned trial and error technique is to enable debugging. This could be achieved by setting the compilation element debug attribute to true in Web.Config from the virtual directory containing your SharePoint application, otherwise, breakpoints inside Visual Studio will be shown but will never be used.
Once you’ve done that, there is one more thing you need to do in order to be able to debug your SharePoint code in Visual studio which is attaching a debugger to W3WP.EXE, Attaching a debugger in Visual Studio will allow you to step through the code and find exactly where the error occurs.




























It’s really handy but there are three questions that I always receive from my colleagues when they get started to SharePoint programming, so I decided to share the answers with you.
The first one is: Sometimes, I set breakpoints and attach to the process but Visual Studio skips loading the symbols if I’m debugging a deployed assembly to the GAC and I fail to debug the code
Easy, Open Tools -> Options -> Debugging.
You’ll find an option labeled Enable Just My Code (Managed Only) as shown in the figure which is checked by default. Uncheck this option to be able to debug the assemblies located in the GAC.

There is a common myth among .NET developers in general and especially SharePoint ones, that in order to debug assemblies that have been deployed to the GAC, you need to copy the debug symbols (PDB File) to the GAC as well. This was true in the early days of .NET but this is no longer true. The second one is: When I try to attach the debugger to the W3WP.exe process to debug, I always see multiple instances of it. Which one should I attach to?
Ok, this is easy too. Just follow the following steps.
Open the command prompt window, run IISAPP to get a list of the current instances of W3WP.exe.

Note the PID of the instance that corresponds to your web application.

Now return to VS, select Debug -> Attach to process and attach to the W3wp.exe instance with an ID equivalent to the PID you got in step 2 -> click Attach.
Now you can trace through the code and find the error causes easily.
But the question still remains, why sometimes are they more than one w3wp.exe instance? This is because the SharePoint Administration Site Collection and the SSP Administration site always have their own Application pools for error isolation purposes.

And the third question is : Is it possible to debug Inline code blocks ?
Actually, I hate Inline Code Blocks and it’s really a good practice to avoid it due to the performance issues when the JIT compiler compiles them but YES, you can do that.
So now you knew the process of debugging? Very boring and repetitive huh? Hmm, Can’t that be automated?Fortunately Jonathan Dibble, a brilliant developer, created a very handy “
Debugger Feature” that can be installed and activated on a SharePoint site to automate this process. Once activated, the Debugger Feature adds an “Attach Debugger” menu item to the Site Actions menu. Extremely helpful and faster than doing that from visual studio.

To read the complete article go on to:http://sharepointmagazine.net/technical/development/getting-started-with-sharepoint-programming-simplifying-sharepoint-debugging-by-creating-the-troubleshooting-toolbox

Happy Debugging :)





Monday, August 3, 2009

How to write to Event Log using visual C#

Write to an event log
Event logging provides a standard, centralized way for your applications to record important software and hardware events. Windows supplies a standard user interface for viewing the logs, the Event Viewer. By using the common language's run-time EventLog component, you can connect to existing event logs easily, on both local and remote computers, and write entries to these logs. You can also read entries from existing logs and create your own custom event logs. In its simplest form, writing to an event log involves only a few steps to create a sample application. To do this, follow these steps:
  • Open Visual Studio C#.
  • Create a new Console application in Visual C#.
  • The Console application creates a public class and an empty Main method for you.
  • Verify that the project references at least the System.dll file.
  • Use the using directive on the System and System.Diagnostics namespaces so that you do not have to qualify declarations from these namespaces later in your code.

You must use these statements before any other declarations.

using System;

using System.Diagnostics;
To write to an event log, you must have several pieces of information:

Your message, the name of the log you to which you want to write (which will be created if it does not already exist), and a string that represents the source of the event.

You can register a particular source with only a single event log; if you want to write messages to more than one log, you must define multiple sources.

string sSource;

string sLog;

string sEvent;

sSource = "dotNET Sample App";

sLog = "Application";

sEvent = "Sample Event";
Use two static methods of the EventLog class to check whether your source exists, and then, if the source does not exist, to create this source that is associated with a particular event log.

If the log name that you specify does not exist, the name is created automatically when you write your first entry to the log.

By default, if you do not supply a log name to the CreateEventSource method, the log file is named "Application Log."

if (!EventLog.SourceExists(sSource))EventLog.CreateEventSource(sSource,sLog);


To write a message to an event log, you can use the EventLog.WriteEntry static method.

This method has several different overloaded versions.

The following sample code shows the simplest method, which takes a source string and your message, and one of the more complex methods, which supports specifying the event ID and event type:

EventLog.WriteEntry(sSource,sEvent);

EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 234);
Save your application.

Run your application, and then check the Application log in the Event Viewer to see your new events.

Complete code

using System;

using System.Diagnostics;

namespace WriteToAnEventLog_csharp

{

/// Summary description for Class1.class

Class1{static void Main(string[] args)

{

string sSource;

string sLog;

string sEvent;

sSource = "dotNET Sample App";

sLog = "Application";

sEvent = "Sample Event";

if (!EventLog.SourceExists(sSource))

EventLog.CreateEventSource(sSource,sLog);EventLog.WriteEntry(sSource,sEvent);

EventLog.WriteEntry(sSource, sEvent,EventLogEntryType.Warning, 234);}}}

MOSS 2007 ebooks

Thursday, July 30, 2009

Create a feature to add custom menu item in Site Actions


Below are the steps I followed to create a custom menu item for the Site Actions Menu on the MOSS 2007 portal.
I went on to create this and it is very simple.Just follow the steps below: http://www.sharemuch.com/2009/04/17/adding-custom-context-menu-items-to-sharepoint-list-or-library/


http://blog.rafelo.com/2008/06/how-to-add-menu-items-to-sharepoint.html


http://msdn.microsoft.com/en-us/library/bb418728.aspx

Custom Feature in MOSS and its uses

What and Why Custom Features

  • Features in Microsoft Office SharePoint Server 2007 allow you to easily package up functionality that can be deployed and installed across the server farm. This article walks you through the steps of creating and installing a custom Feature.

  • Features are a major enhancement to the template framework of Microsoft Office SharePoint Server 2007 (MOSS). Features allow you to test, deploy, and activate custom functionality inside Office SharePoint Server 2007 and provide a way to make functionality available across your server farm. This functionality can be custom workflows, content types, modifications to the user interface, or new templates for lists and document libraries. For example, you can roll out the definition of a custom document library to any number of team sites by packaging it up as a Feature, complete with metadata expressed in the Collaborative Markup Language (CAML) in the schema. You can package the Feature up as a SharePoint solution and provide it to an administrator, where it can be installed and deployed as a unit.

  • Another benefit of using Features is in the use of site definition (ONET.xml) files. ONET.xml files provide Windows SharePoint Services with information about the navigation, lists, and document libraries that users can create. In the previous version of SharePoint, ONET.xml files have a tendency to get very large. With the advent of Features, the ONET.xml file shrinks because Features can now contain the information that was previously defined in the ONET.xml file.
  • Features provide a level of reusability by providing you with a means to package and deploy functionality as a unit. You can install the Feature at the level of a server farm and then active it only when needed. This has the additional benefits of allowing you to deploy updates to all Web front-end machines in one operation, more easily applying source-control to site resources, and allowing less-technical people to roll-out or roll-back changes, if required.

Exploring the Components of a Feature

Features are typically stored on the SharePoint server at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES. Each Feature has its own sub-directory that resides in this directory. Inside each Feature's sub-directory, you will find, as a minimum, a header file named feature.xml. The feature.xml file points to a manifest file that tells SharePoint where to find the code and XML that defines the Feature. The feature.xml file contains the Feature element that specifies the location of assemblies, files, dependencies, or properties that support the Feature. Optionally, the Feature element may point to an XML file that contains an Element element which defines the components and types that make up the Feature.

SSP in MOSS

SSP

In MOSS 2007 there is this new concept of Shared Services Providers(SSP). The idea being that there are certain services that really make sense to centrally manage and share. A good example being profiles. With a SSP we can import all of the profile information from AD once and then our various web applications can consume the data. So maybe we have http://marketing and http://accounting it doesn't make sense for each one to maintain identical profile information, they should share.

The major services that are handled by the SSP are:

  • Profiles and Audiences
  • My Sites
  • Search
  • All of Excel Services
  • All of the BDC (Business Data Catalog)

Below is an example screen shot from MOSS 2007 Enterprise:

Wednesday, July 29, 2009

Switch Views in InfoPath

An easy way to switch views in InfoPath 2007 is the following:
Go to Tools --> FormOptions --> Open and Save -->Click on Rules-->Add a Rule -->Set Condition -->Add Action
Here you will see an action called Switch Views.
From here you can toggle views of InfoPath