Archive for the ‘SourceCode’ Category
Get TcmId from web dav url
Many of tridion developer would have tried to get tcm id from web dav. This might be one of the common scenarios where we are using web dav when we are rendering embedded components (in another component template).
I think this is simple scenario so I’m just sharing C# code snippet for this. I’m posting this snippet becoz somebody was googling this yesterday
))
IdentifiableObject identifiableObject = engine.GetObject("<<Web dav url>>");
package.PushItem("TCMID", package.CreateStringItem(ContentType.Text, identifiableObject.Id));
Publication URL TBB
In most cases, you will not be publishing the binary files (images/css/js) in the same location of where you publish pages. As a good practice, these external files will be stored in separate folders. In such cases, you will need to specify path (relative) in the page DWT template.
Generally external css files will be declared like
<link href="/styles/main.css" rel="stylesheet" type="text/css"/>
This would work only if you have styles folder immediately after server path.
http://<server>/styles/main.css
In Tridion, default publication URL is “/” but if you have specified any path in the Publication, you will need to append that path also. It should be accessed like.
http://<server>/<pub url>/styles/main.css
The below C# TBB will get the publication URL of the current publication.
//gets the current session object
Session session = engine.GetSession();
//gets page object from the package
Item item = package.GetByType(ContentType.Page);
//gets TcmUri object of Page
TcmUri tcmObj = new TcmUri(item.GetValue("ID"));
//generate tcm string format with publication id
string publicationId = string.Format("tcm:0-{0}-1", tcmObj.PublicationId);
//gets publication object from the current session
Publication publication = new Publication(new TcmUri(publicationId), session);
//writes the publication URL into the package
package.PushItem("PublicationUrl", package.CreateStringItem(ContentType.Text, publication.PublicationUrl));
As you aware, variable “PublicationUrl” will be included in the package so this variable is accessible when you include this TBB on your page template.
Now you need to call this variable on the page DWT like below,
<link href="@@PublicationUrl@@/styles/main.css" rel="stylesheet" type="text/css"/>
Validate Web service in C#
I’ve got many web services to read IMDB content. Some of them are failed to access while testing. So I needed to check whether they are really active before accessing them.
Here’s the code snippet to check any website/web services are active.
public bool IsWebServiceActive(string webServicePath)
{
try
{
WebRequest request = WebRequest.Create(webServicePath);
return ((HttpWebResponse)request.GetResponse()).StatusCode == HttpStatusCode.OK;;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
Update registry files using C#
When I was doing my IMDB app, I found some of the code snippets are frequently used for desktop applications. Here’s the one code sample.
I was having all my registry settings in .reg file instead of adding each entry through code. All we need to do is run this reg file through code.
string filePath = "IMDBSettings.reg";
.
.
.
Process.Start("regedit.exe", "/s " + filePath).WaitForExit();
Get object types in C#/Tridion 2009
In C#, the general usage to identify the object type is using “is” keyword. This is okay to some extent but you have to write “switch” of “if”, for multiple types. If there are more object types, code will be lengthy. Recently I came across a easy way of checking the object’s type. This is helpful when the object is of COM type.
Just add the below reference in your project and call its TypeName method.
using Microsoft.VisualBasic;
.
.
.
.
.
public string GetTypeOf(object obj)
{
return Microsoft.VisualBasic.Information.TypeName(obj);
}
Output:
This was very handy for me in my migration project where I needed to write all the object details in log file.
