Embedding resources in WebParts

Today I came across a requirement of showing an swf file in the webpart. So I thought I will upload the swf file in folder and give its url to the webpart. But then that will be hardcoded path. What will happen if the user deleted that folder or rather deleted that file itself? So my webpart will not run.

So I found some alternative to this and thought of embedding my swf file and js file in webpart itself. I googled on how can I do this, and here are the steps for embedding the files in the webpart itself.

I ll assume that you are ready with the Webpart and you just need to remove the hard coded file’s url’s in your webpart. And i ll be using swfobject.js file in this example you can change the file and the type though, as per your requirements.

– Add swfobject.js file in the same project as webpart.

– Right click on js file select properties and change build action to “Embedded Resource”.

– Make an entry in AssemblyInfo.cs or AssemblyInfo.vb file like

[assembly: WebResource(“ReadENote.swfobject.js”, “text/js”)]

format : [assembly: WebResource(“<project namespace>.<filename>.<extension>”, “<content type>”)]

Note : Make sure you specify the namespace along with filename else it will not recognize the file.

– You can access this embedded resource in your webpart code like this

Type t = typeof(<webpartname>); //e.g. Type t = typeof(ReadENote);
string js = “<namespace>.<filename>.<extension>”; //e.g. string js = “ReadENote.swfobject.js”;
jslocation = Page.ClientScript.GetWebResourceUrl(t, js);
Page.ClientScript.RegisterClientScriptResource(t, js);

This code will embed the specified file, in this case swfobject.js file in my webpart.

Note: you only need to use the last line “Page.ClientScript.RegisterClientScriptResource(t, js);” if you are using the javascript file. If you are using some other file like an image file or swf file, you can use the jslocation in place of your hard coded location.

– my previous code to embed swf file looks like this

strBody = strBody + “var so = new SWFObject(\”notes.swf\”, \”stickies\”, \”250\”, \”250\”, \”9\”, \”#FF6600\”);”;

– my code after using these steps will look like this

Type t = typeof(<webpartname>);

string js = “<namespace>.<filename>.<extension>”;

jslocation = Page.ClientScript.GetWebResourceUrl(t, js);

strBody = strBody + “var so = new SWFObject(\”” + swflocation + “\”, \”stickies\”, \”250\”, \”250\”, \”9\”, \”#FF6600\”);”;

Make these changes, build and deploy your web part.

Enjoy dynamic coding and prevent hard code.

Friendship’s Day

We have something called as Fun Friday’s in our office we normally celebrate that every alternate friday. What we do in that is play games and enjoy. All it does is it keeps you fresh and agile its one of the most happening thing in my office. And the games are based on the theme or some festival which is goin on or which is awaited. The winning team is presented with choclates.

This friday it was Friendship day which was comming up this sunday. So we decided so have something related to friendship some game related to friendship but our different ideas were all used in the previous fun friday’s so we had to think about some good idea for this Fun Friday to celebrate Friendship’s day.

So we came up with the idea of Face Painting. We had created 5-6 teams of 7-8 members each of these 7-8 members 2 members were to be face painted and other need to paint their face and the theme was Friendship. So my team thought of painting the Tom and Jerry as they are very good friends because they cant live together and best part of their friendship is they cant even live without each other how much ever they fight but they cant live with and without each other. Here are the pics from this time fun friday,

Tom n Jerry

Tom n Jerry