String Writer

my tech stuffs…

Archive for the ‘SourceCode’ Category

Get TcmId from web dav url

leave a comment »

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));

Written by visvabalaji

January 10, 2012 at 5:52 pm

Posted in SourceCode, Tridion

Tagged with ,

Publication URL TBB

leave a comment »

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"/>

Written by visvabalaji

November 22, 2011 at 3:49 pm

Posted in C#, SourceCode, Tridion

Tagged with , ,

Validate Web service in C#

leave a comment »

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;
  }
}

Written by visvabalaji

September 16, 2011 at 12:23 pm

Posted in C#, SourceCode

Update registry files using C#

leave a comment »

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();

Written by visvabalaji

September 16, 2011 at 12:04 pm

Posted in C#, SourceCode

Get object types in C#/Tridion 2009

leave a comment »

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.

image


using Microsoft.VisualBasic;
.
.
.
.
.
public string GetTypeOf(object obj)
{
   return Microsoft.VisualBasic.Information.TypeName(obj);
}

Output:

image

This was very handy for me in my migration project where I needed to write all the object details in log file.

Written by visvabalaji

August 11, 2011 at 11:10 pm

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: