Wednesday, February 23, 2011

Extension functions

When ever I need to convert any object value into string, I normally uses the .net function ToString(). Even though this gives me the string value but this is quite annoying sometimes because when the object value is null then it throws an Object Reference not set to an instance of object error. So to avoid this error, I have to write a check like this before using the toString function. 
if(objValue != null)
{
objValue.ToString();
}

But this brings an additional task of writing this check every time in the code, which in turns makes the function body lengthy. With the implementation of .Net 3.0 Extension functionality, I could make my function clean, less lengthy and readable. More information on Extension functions can be found here.

Below is how to do this,
Firstly I added an extension function to the object type and named it as "ToValueString()"
The extension class and function definition is as follows, 

public static class Extensions
{
public static string ToValueString(this object value)
        {
            return value == null ? string.Empty : value.ToString();
       }
}

Once the above class is there in the project, then once we type the dot after the object name, then the .net intellisense will be show up and we can find the custom extension function in the list. By this way, code block can be clean, and doesn't need to bother about the Object Reference not set to an instance of object because the null check is handled inside the extension function. Since we have added the extension to object class which is the base of other .net classes this extension function will be attached to other .net class objects as well.

Another usefulness of the extension came during the validation for empty string. .Net string class comes with a useful function to check whether the string value is Null or empty, string.IsNullOrEmpty(stringValue), this work when the stringvalue is null or empty string, but to use this function effectively then we have to use trim() function to remove the unwanted empty spaces in string, but to use trim() function we again have to add a check because stringValue.Trim() will again throw Object Reference not set to an instance of object when stringValue is null.

if(stringValue != null)
{
stringValue = stringValue.Trim();
}

if(string.IsNullOrEmpty(stringValue))
{
MessageBox.Show("Value not entered");
}

So this will in turn make my function lengthy which I don't like because it just consumes developer's time and redundant code for doing the same functionality for multiple objects. The extension function again came to ease my development, I wrote one extension function for string object which can be found below,

public static bool IsNullOrEmpty(this string obj)
{

if (obj == null)
{
return true;
}

if (obj.Trim() == string.Empty)
{
return true;
}

return false; 
}

In short, the extension functions are very helpful in coding and increases developer’s productivity.

Thursday, January 20, 2011

Redirecting back to the view post page instead to default.aspx on publish/cancel button click

Clicking on the edit button on the top right corner of the post content of post view page (Posts.aspx?Id=PostId) navigates to the edit post page. And from the edit post page, once the publish or cancel button is clicked the system redirects the page to the default page of the blog site, ie the default.aspx page. But in some cases this behavior may be confusing to user because normally the user might expecting to return back to the post which he was viewing, ie the post view page where he clicked the edit button.

To do this change we can either do it by modifying the site definition file of the blog template or by opening the Posts.aspx file in Sharepoint Designer. Here we will discuss on how to change this at the site definition level so that it changes this behavior globally.

Firstly, we need to open the blog site definition folder under the 12 Hive,

Image 01 

then we need to go to Lists\Posts folder, in this folder we can find an file named schema.xml, this xml file is schema definition for the posts list, that means the xml definitions about the list such as fields, content types, views etc.

Here we need to modify the view of the posts list, when added as the listviewwebpart in the page posts.aspx, and that can be found under, xml tree node path,
<List .... >
<MetaData>
    <Views>
        <View BaseViewID="7" FreeForm="TRUE" Type="HTML">

Image 02 

Be careful while modifying this xml file, a minor typo may even break the site definition, so keep a back up at first [:-)]

Now search for the section which actually add the edit button link html, ie. search for the TD which add the button, (<TD align="right" class="ms-blogedit">)
Here the button is being added based on the permissions, i.e. we need to modify it in 3 places. So if you go through the HTML sections, the above mentioned TD is being added 3 times, for each section of that HTML
<![CDATA[]]>,
replace the node which has default.aspx text enclosed in HTML (<HTML><![CDATA[%2Fdefault.aspx]]></HTML>)
with the below 2 nodes,
<HTML><![CDATA[/$Resources:core,lists_Folder;/$Resources:core,blogpost_Folder;/Post.aspx?ID=]]></HTML><Column Name="ID" URLEncode="TRUE" />

By adding the above 2 nodes, we mean that we need to replace the default.aspx with the url of post.aspx with ID value being the currently edited post id.
So before the change the node will be like this,
Image 03

and after change

Image 04

Do an IISRESET to reload the sitedefinitions, and then refresh the posts.aspx page to see the change.

P.S: If you wish to alter the behavior specific to a single web only, if requirement is for a fresh web then you can create a new site definition based on blog, so the default blogs remain intact. For modifying few of the existing blogs, the option is to make this change in posts.aspx page by opening that in Sharepoint designer for that web.

Friday, October 1, 2010

Today - Binary Day 01-10-10

Interestingly today’s date looks like a binary date to me, today is 1st October 2010, which is 01-01-10 in dd-MM-yy format. And binary values are important for all the computer professionals.

Happy Binary Day to all :-)

Tuesday, September 21, 2010

Apply Filter on Category Field in Content Query Webpart which displays Posts

Recently I came across an issue on applying filter on a content query webpart(CQWP) which was configured to show list items from Posts list. The filter which needs to be applied was against the Category column of the post so that the webpart shows on posts of that category.

So I choose,

image but when I click Apply button, the webpart threw an error.

image

To solve this error, and to apply the filter properly we need to add an additional filter property on the .webpart file. Export the webpart to hard disk, open the .webpart file in any text editing tool, find the property tag with name “AdditionalFilterFields

image

Replace that with

image

Actually we need to add additional filter field PostCategory, which is the internal column name of the category field of posts.

Now save the .webpart file and import this to the webpart page, after this if we go to the modify webpart, and in the additional filter column we will find the PostCategory displayed,

image

Choose that PostCategory, and apply the filter logic,

image Click OK or Apply button to make the changes.

That’s it, now the CQWP will show the posts where category contains Category 1,

image

Thursday, September 16, 2010

Very useful and easy tool to take screen snap shots

Hi All, I found it very useful and easy to use Screen Clipper http://www.screenclipper.com/ to get the screen snap shots. This is free application.

Technorati Tags: ,