Wednesday, April 30, 2008

Exception Handling

In Sharepoint specially when writing WebParts and Event Handlers exception handling should be done properly else you get mixed results. Try to catch exceptions in two parts one is SPException type and another is SecurityException type.

There is a very good tool called as FxCop. This application checks for if the exception haldings and naming conventions are as per Microsoft Stadards or not. Want to know more about FxCop? Go Here!

Monday, April 28, 2008

Finally REMIX

Finally Microsoft REMIX is in Australia too. Here you will get to see next generation websites developed using Silverlight 2, Expression 2, IE 8 and more. You can register here at REMIX


Editing web.config Programmatically

Today after finishing the webpart development when the time came to deploy them, I realised that everytime I deploy these webparts on another server I have to add SafeControl in web.config manually again and again. Did some research and found some good articles. SPWebConfigModification is the class that we can use to modify the web.config file.

Mark Wagner has dotted down some good best practices to do this. Check here

Ted Pattison has also written some great details about the complete process to do it and he has also uploaded a sample feature to do this. Check here

And if you are still not satisfied then read more information about SPWebConfigModification on msdn

Link to an item when running SPSiteDataQuery

Many a times we will need to run SPSiteDataQuery on a list to get the results from a sitecollection. Now once we have retreived the results from the query we need to get the url of each item. There is a hidden column "EncodedAbsUrl" which consists of complete URL to an Item. For some reason accessing this column doesn't give us the complete URL that we need (I don't know why). But there is another trick to get the URL of an item. There is another hidden column "FileRef" in each list. This column data has got the URL with the item ID concatenated to it, something like this:

row["FileRef"] = 1;#Popular Wiki/How To Use This Wiki Library.aspx

**row is from the datatable returned when you run web.GetSiteData(query) and query is the constructed SPSiteDataQuery.

We can retreive the URL part from it and construct the complete link from the root site. Something like this:

string url = row["FileRef"].ToString().Substring(row["FileRef"].ToString().IndexOf("#") + 1);

string finalUrl = site.MakeFullUrl(url);

You can read more about MakeFullUrl() method here msdn

Tuesday, April 15, 2008

Creating WebPart instances in Custom Site Definitions

Cool Stuff! I have just found or I should say "I just came to know" that we can create a webpart instance on the default page of a site right in the Site Definition. Consider a scenario where you are having a Custom Site Definition and you wish to put some standard webparts for all the users when a site is created using this Site Definition->Site Template. You can do this in two ways one is the through the object model using SPLimitedWebPartManager class and another is via CAML in site definition. I will explain via CAML way in this post:

If you have seen my previous post on Site Def architecture, we have got Modules node:

<Modules> <Module Name="Default" Url="" >
<File Url="default.aspx" NavBarHome="True" Type="Ghostable"> </File>
</Module>
</Modules>

Now under File node we have <AllUsersWebPart> node available. This is the key to add any webpart instances. Below is the example:

<AllUsersWebPart WebPartZoneID="Right" WebPartOrder="0">
<![CDATA[
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2"
xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image%22&gt; <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly> <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName> <FrameType>None</FrameType>
<Title>Home Page</Title> <iwp:ImageLink>/_layouts/images/CONTACT.PNG</iwp:ImageLink> </WebPart> ]]>
</AllUsersWebPart>

Site Definition Architecture

If some one is looking for a basic site definition template or skeleton or wants to see what site definition consists of then please check below. It has also got XML namespace for SharePoint which will allow the intellisense when you eidt the file in Visual Studio:

You will have to remove single quote ( ' ) from the opening and closing angle brackets to use this. Sorry for this as they were being removed when publishing.

'<'?xml version="1.0" encoding="utf-8"?'>'
'<'Project Title="My Site Definition" Revision="0" ListDir="Lists" xmlns:ows="Microsoft SharePoint" xmlns="http://schemas.microsoft.com/sharepoint/"'>'
'<'NavBars /'>'
'<'DocumentTemplates /'>'
'<'Configurations'>'
'<'Configuration ID="-1" Name="NewWeb" /'>'
'<'Configuration ID="0" Name="Default"'>'
'<'Lists'>'
'<'/Lists'>'
'<'Modules'>'
'<'Module Name="Default" /'>'
'<'/Modules'>'
'<'SiteFeatures /'>'
'<'WebFeatures /'>'
'<'/Configuration'>'
'<'/Configurations'>'
'<'Modules'>'
'<'Module Name="Default" Url="" '>'
'<'File Url="default.aspx" NavBarHome="True" Type="Ghostable"'>''<'/File'>'
'<'/Module'>'
'<'/Modules'>'
'<'/Project'>'

Web Part Class/Assembly Details

Many times you would like to know the assembly details and class name of a webpart. Now you can ask why you need to know these details. This is why! for example you are trying to dynamically trying to add some webparts on a page. To add these webparts you will first have to load the assembly and then have to create an instance of that webpart class through reflection. Now to load these assemblies and to create instance of webparts from webpart gallery you need to know there full 4 part assembly name and class name.

This is how! Try to add this webpart on your page through EditPage action. Once you have added you can now Export this webpart and open the .webpart file in a notepad. There you will see something like this:

'<'assembly'>'Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'<'/assembly'>' '<'typename>Microsoft.SharePoint.Portal.WebControls.QuickLinksMicroView'<'/typename'>'


Here Typename is the class name of the webpart. And this is for a My Links webpart. This will come handy manytimes when the name of Webpart class is different from the name in webpart gallery (Which is the case most of the time)

Finally got started

"I believe knowledge sharing is key to knowledge gain"

Hi All,

Finally bolgger world has pulled me to start my own blog. I have been thinking this for so long and now my self motivation has forced me to create my knowledge sharing place. I have created this blog to share any issues , solutions, tricks etc..that I find when working on MOSS projects. I hope this information will be helpful to some of you and will save some time.

Will try my best to actively post any solutions or tips I find or come across.

Oh yeah! and just to start with - tomorrow is a SharePoint UserGroup happening in Canberra. Patrick Tisseghem is in town and will be delivering a session on Customizing Search page and results. Check this: http://www.sharepointusers.org.au/Canberra/default.aspx

I am defi going for this session and will try to share some information on here.