Zieglers

Just little about C#, .NET, SQL Server, SharePoint and SAP

Archive for June, 2010

How to create a Visual Studio Item Template

Posted by zieglers on June 24, 2010

BACKGROUND:A number of predefined project and project item templates are installed when you install Visual Studio. You can use one of the many project templates to create the basic project container and a preliminary set of items for your application, class, control, or library. You can also use one of the many project item templates to create, for example, a Windows Forms application or a Web Forms page to customize as you develop your application.
 

In addition to the installed templates that are available in the New Project and Add New Item dialog boxes, you can access templates that you have created.

MOTIVATION:Developers are lazy. I don’t mean that in a bad way, but in the same way that a smart horse is a lazy horse. Developers just don’t stand for repeating the same tasks without finding a way to automate them. I count myself among the lazy developers. I’m quite willing to spend time figuring out how to automate those repetitive tasks that I find myself doing often.

Visual Studio provides enough extensibility points that I’ve made a new rule for myself: If I do the same task twice, I figure out how to automate it.

In this post, I’ll try to show you ‘How to create an Item Template‘ for your Visual Studio project.

This will allow you to take advantage of C#’s Item Templates to automate tasks that you find yourself having to perform on a regular basis.

DEMO:

Now, let’s assume we have an ‘Application Pages’ project. We’ve been creating some app pages for a while and now we’d like to automate this process.

Since creating an app page requires a number of steps, we don’t want to perform those steps again and again, so it’s time for an app page template for our project.

Before creating your item template, get your code in a position where it can be used as a template once it’s been generated by template. This means include all the tasks you repeat and only leave page specific code / logic. For that part of the code, simply you can use a comment saying: // YOUR CODE GOES HERE.

Ok, say you have your project item (you’ll create your template from) ready.

First, we start Export Template wizard from File menu.

Then, we choose Template Type. In our case, it’s ‘Item Template‘.

Next, we select the item we’d like to export as an item template.

Then, we select item references to include with our item.

Finally we name our item template, provide a custom icon for it if you want and a description.

Once you click finish, your item template will be generated. Solution and Item templates in Visual Studio are in ZIP format.

Now, let’s create an application page by using item template we’ve just created. For this, simply go to your project, right click where you want to add a new app page and select new item.

Select item template just created.

Provide a name for your app page and click Add. That’s all! Now a new app page has been created using your Item Template.

This concludes our post on ‘How to create an Item Template‘. Remember to give a second thought while coding, in case you find yourselves doing same thing more than once or twice, it might be a good candidate for an item template.

Zieglers

Posted in .NET Framework, C#, IT Stuff, SharePoint | Tagged: , , , | Leave a Comment »

Using jCarousel in SharePoint sites

Posted by zieglers on June 8, 2010

Recently i was experimenting with jQuery and was thinking how to apply it on some SharePoint scenarios. I must say that more I dive into jQuery, more impressed i am with its capabilities. There are many ways of using jQuery for SharePoint.

In this post, as a starting point, i’ll try to explain a control called jCarousel for SharePoint.

Carousel is a web component which allows you to browse through a set of items in groups. It’s called carousel since when you click left and right arrows it gives you the feeling that it’s turning. Usually it’s used for previewing photos.

jCarousel is using jQuery technology to give that turning look and feel. However in order to make it SharePoint friendly, you have to go thru some steps.

Enabling jQuery for SharePoint

Eventhough technique i’ll mention here is documented in this post, it’s not specific to jCarousel. It acts as a jQuery infrastructure for SharePoint.

Actually, idea is really simple. In order to run jQuery in SharePoint pages, jQuery library must be referenced in page headers. Once jQuery library is referenced, functions in that library can be called anywhere within that page.

Since SharePoint is controlling page rendering, you have no option of editing those pages and including your reference between html head tags of that page. So, how do we do that?

Firstly, we create a controltemplate which holds reference information to jQuery library.

Secondly, we implement an additional page header and render our control template using that code.

Thirdly, we create a site-scoped feature to activate/deactivate jQuery for site collections.

Before we finalize infrastructure details, we need to put actual jQuery library in 12 hive. I picked Layouts/jQuery folder for this. This folder will keep jQuery libraries and any jQuery related files.

Now, we are done. Our feature to enable jQuery for SharePoint is ready. All we need to do is to activate the feature on site collections where we want to enable jQuery.

Using jCarousel in SharePoint

Once jQuery is activated for a site collection, controls using jQuery can easily be used in pages. Here is how you can use jCarousel in SharePoint pages:

jCarousel is actually an unordered list (<ul> tag in html) which contains some list items (<li> tag in html). In this example those list items will be images which can be retrieved from picture library. Also we wrap list items with a asp.net repeater control so that we can bind image urls dynamically.

So, jCarousel html looks like this:

And you can bind images to jCarousel as follows:

In above code snippet, we create a datatable with a column called ItemUrl, then we loop thru MyPicZ picture library in current web object and add urls to datatable. Finally we bind datatable to repeater control.

That concludes this post. In the end we have jCarousel control working in a SharePoint page.

zieglers

Posted in IT Stuff, SharePoint | Tagged: , , , , , , | 2 Comments »