Tuesday, June 2, 2009

Sample SharePoint project localization Part 1 of 2

By Hristo Yankov

Hello,

This is a step-by-step tutorial on how to create a localized SharePoint custom web application. In this first part, we will discuss the creation of the sample web application itself, while the second one, will emphasize on the localization.

Prerequisite is a Visual Studio 2008 with WSS extensions 1.3. You can also work with Visual Studio 2005, but you will have to do part of the deployment manually.


1. Start Visual Studio and create new project of type SharePoint.



I entered 'SPLocalization' as name for the project.

2. Select "Full Trust"




Note: Check debug tab of Project Setting to ensure it contain correct path to your SharePoint server.




3. Add new item to our project





4. Select 'Template' element in SharePoint section. Change file name to 'LocalizedPage.aspx'






5. Now you should see project structure like this:








6. Create subfolder LAYOUTS under Template folder


7. Create subfolder SPLocalization under LAYOURS folder


8. Move LocalizedPage.aspx to SPLocalization folder (I use drag and drop the file). Now you should have the following structure:






9. Now let's create a class that extends SharePoint layout pages. Add new class to your project.






10. Name it 'LocalizedPage.aspx.cs' and click "Add" button




11. Now few other steps ...

First add references to SharePoint and SharePoint.WebControls
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

Secondly make your class public, partial and derived from LayoutsPageBase

You should see something like this:


Now, since we have chosen GAC deployment model, we need to deploy our solution to Global Assembly and determine Public Key.

12. In Visual Studio Menu, Selection, Build -> Quick Deploy Solution -> Copy Binary(s)


This will compile our SharePoint project into DLL and deploy it to GAC. After deployment Output window will show that Deployment was successful:


13. Let's go to Windows Assembly folder and find our library


If you open Properties of our assembly, you will be able to copy its public key token. In my case it was 7a1fde53a908a4fe.

We will need Public Key Token for our aspx.page.

Now, let's return to our ASPX page.

14. Remove default text and add Page registration tag




<%




@







Page







Language




="C#"




MasterPageFile




="~/_layouts/application.master"




Inherits




="{ClassName}, {Assembly}, Version=1.0.0.0, Culture=neutral, PublicKeyToken={PublicKeyToken}"



%>

Now let's replace {ClassName} with SPLocalization.LocalizedPage {Assembly} with SPLocalization {PublicKeyToken} with Public Key Token we obtained from GAC.

Now you should see something like this:





Now let's add some content to the page and test if everything works.



15. Add ASP tag Content with ContentPlaceHolderID equals to PlaceHolderMain. Here is complete code





<





asp




:




Content







ID




="Main"







ContentPlaceHolderID




="PlaceHolderMain"







runat




="server">




Test message










asp




:




Content




>




Your Localized.aspx page should look like this:





<%




@







Page







Language




="C#"










MasterPageFile




="~/_layouts/application.master"










Inherits




="SPLocalization.LocalizedPage, SPLocalization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a1fde53a908a4fe"



%>







<





asp




:




Content







ID




="Main"







ContentPlaceHolderID




="PlaceHolderMain"







runat




="server">






Test message












asp




:




Content




>






16. Let's make a full deploy. Visual Studio menu Build -> Deploy Solution.
After deployment is completed, Output window will show that Deploy was successful.





17. Now let's go and check manually if LocalizedPage.aspx was deployed.
Browse to 12 directory (by default it is "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12") then browse to TEMPLATE, then to LAYOUTS, then to SPLocalization. You should see our LozalizedPage.aspx was successfully deployed.





Now let's try to access this page through SharePoint

18. Open SharePoint url of your site with prefix "_layouts/SPLocalization/LocalizedPage.aspx".
In my case, it is "http://itech-vm-spdev/_layouts/SPLocalization/LocalizedPage.aspx"

When page is loaded you should see empty area but with our magic text: Test message




Now, let's add SPLinkButton SharePoint control

19. Change your ASPX page to include Register of Microsoft.SharePoint assembly and replace static text with SPLinkButton control.

Your ASPX page should look like this:




<%




@







Page







Language




="C#"




MasterPageFile




="~/_layouts/application.master"








Inherits




="SPLocalization.LocalizedPage, SPLocalization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a1fde53a908a4fe"



%>




<%



@





Register





Assembly



="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace



="Microsoft.SharePoint.WebControls"



TagPrefix



="cc1"


%>





<





asp




:




Content







ID




="Main"







ContentPlaceHolderID




="PlaceHolderMain"







runat




="server">






<





cc1




:




SPLinkButton







Text




=""







runat




="server"







ID




="myLink"







/>











asp




:




Content




>




20. Update your LocalizedPage class to include myLink definition. We will also assign Text value in OnLoad method. You class should look like this:





public







partial







class







LocalizedPage



:



LayoutsPageBase






{






protected







SPLinkButton



myLink;






protected







override







void



OnLoad(



EventArgs



e)





{





myLink.Text =



"Our test link"



;





}





}



21. Let's do full deploy again


22. Once it is successfully deployed. Open LocalizedPage in the browser again. You should see that now you have a link on the page. This link is provided by SharePoint SPLinkButton control.








In the next article, we will discuss localization of the sample application we created...






Bookmark and Share

1 comment:

KULO said...

very well written. thank you.