Dynamic Images from Web Parts

What are you mad !! You can’t do it, how on earth are you going to suddenly be called again as an image and write binary data out.  

If you’ve read previous posts you will see that I have been writing a web part that gets SiteUsageData and displays it.  It uses some nasty COM+.  I got to thinking how on earth am I going to graph this dynamic data that changes per site?

HttpHandlers - for a bit more info on HttpHandlers see here on Scott Hanselman’s blog.

Now your up to speed on them, how can we use them with a part. 

First we have to spit out the IMG tag in your render section.

textWriter(”<img src=’_layouts/MyWebPartImage.ashx/’>”);

Of course that on its own does nothing, but note the url “_layouts”, not “/_layouts” this is very important as your part will run in the Site context of “/” if you do, instead of the current site.

Second you need to register this HttpHandler in web.config

<add verb=”GET” path=”MyWebPartImage.ashx” type=”Binaryjam.MyWebPartNs.MyWebPart, Binaryjam.MyWebPartNs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5**************d” />

Replace those details with your own namespace, typename, public key.  Ahh Did I say you need to GAC this part.  I haven’t experimented with not GACing this feel free to but to be sure GAC IT.

Third add the IHttpHAndler interface to your web part class

public class MyWebPart: WebPart, IHttpHandler

Nearly there now add your Interface methods, remember Scott’s article ? Here is a simple example of this:

public void ProcessRequest(HttpContext context)
{
    HttpResponse response=context.Response;
    HttpRequest request=context.Request;
    response.ContentType=”image/jpeg”

    Graphics g=null;
    System.Drawing.Image image=null;
    image=new Bitmap(Width,Height,PixelFormat.Format32bppArgb);
    g=Graphics.FromImage(image);
    g.FillRectangle(new SolidBrush
            (Color.White),0,0,image.Width,image.Height);
    g.DrawString(”YooHoo”,new Font(”Verdana”, 10.0f),
             new SolidBrush(Color.Black),0,0);
    image.Save(response.OutputStream,ImageFormat.Jpeg);

}

public bool IsReusable
{
  get
  {
    return true;
  }
}

These pieces of code give you the basics to dynamically generate an image from a web part.  Now because the webpart is being created again in the context of the site its running in in theory its Properties are populated, however you would need to check this.  For my use I only needed to now the site Context, I could then use the Sharepoint API and the site context

SPWeb thisWeb = SPControl.GetContextWeb(Context);

To get the data I need.  You could use the API to access list information in a site and graph that too.

And no I don’t have a full code example, my graphics code is too embarrassing.
:-)

 

Its here I might point out that doing this might be a really bad idea, lets face it we don’t know what’s happening behind the scenes, but lets have a guess.  You have already hit this page once to display the web part. To display the image the site is visited a second time, admittedly not the rendered page but the site all the same.  Now what could be happening here is that the Sharepoint httphandlers, because remember its not the aspx ones running, initialise loads of Sharepoint stuff.  Then you create a webpart, now either the Sharepoint context exists already of the webpart creates it, considering that on a page could be multiple web parts are they all creating contexts ? or calling a static to get it ?  Anything could be happening here.   On the other hand is it as bad as hosting an image held in a site list in a CWP part on your site ?  Probably very similar.

If there are any Sharepoint under-the-covers experts who know what’s happening, let me know please.

tags: sharepoint+webparts, sharepoint, GetUsageData, evil+voodoo+in+sharepoint

Goggles

This is a fantastic mashup of with google maps.

Goggles

Fly your plane over, wherever, and shoot at it. 

Installing the COM+ .NET component

A couple of posts ago I talked about a GetUsageData webpart and how to access the data when not a site admin by using COM+ to retrieve the data.

But of course this needs to be installed.  Luckily for me we have a MSI library that Dan developed, which does all the sharepoint web part installation, as well as a host of other web based installation tasks that do not come out of the box with MS’s installation project.  But this only helps me with the webpart.

The earlier articles mention using Regsvsc to register the COM+ component or to allow lazy regristation.  Whilst manually registering is certainly better than lazy its still yet one more task that could be done as an MSI.

Here is a great article about registering COM+ components from start to finish with multiple methods. There is a section on writing an MSI installer class to acheive this using the .NET Library RegistrationHelper, it won’t take a genious for you to figure out how you can extend this to install your web part.

Note specifically the custom actions bit,  also look at custom action data as this is how you can get data into the MSI from the command line enabling unattended installations.  Also you can create a form in your dll to capture data in interactive mode to be fed into your installer (check /SILENT).

But is it enough, for me no.  RegistrationHelper does not let you configure the admin user details.  To achieve this you can do more.  First capture those details either from CustomActionData or your form. Then use techniques I found here

For those who hate vb.net here is some c#

Set a reference to COMAdmin library (its a COM one not .NET),  Have already registered the component in COM+ and stored the Package name in appID

catalog=new COMAdmin.COMAdminCatalog();
applications = (COMAdmin.COMAdminCatalogCollection)catalog.GetCollection(”Applications”);
applications.Populate();

foreach (COMAdmin.COMAdminCatalogObject applicationItem in applications)
{
    string s=applicationItem.Name.ToString() ;
    if (s==appID)
    {
        application=applicationItem;
        bGotOne=true;
        break;
    }
}

if (!bGotOne) return//I’m so slack !!

if (login.Length>0 && loginpwd.Length>0)
{
    application.set_Value(”Identity”, login);
    application.set_Value(”Password”,loginpwd);

    applications.SaveChanges();
}

Remember to Have GAC’d your component in code, and remember to uninstall all this too in the correct order.

Full list of articles I used to get here

http://www.carlosag.net/Articles/configureComPlus.aspx
http://www.15seconds.com/issue/030501.htm
http://blog.u2u.info/DottextWeb/patrick/archive/2004/10/20/235.aspx
http://blogs.microsoft.fr/rlondner/articles/7089.aspx

tags: , , , ,

How not to charge an iPod

I love gadgets, I have a few.  Like most they are USB devices that require charging.  I tend to charge them from my PC, when Im using it, or with the proper charger.

The proper charger, especially for my windos phone, charges in record time, the PC charges a lot slower and so I only PC charge when at work.

There is a reason for this, a USB socket from the PC will output 500ma at 5v, thats a power consumption of 2.5 Watts.  (If there are any electric engineers who want to correct this please do, but its the next part thats the kicker).

Tuesday I get a call from my mate saying we’re not going drinking he’s got back late and knackered, a sometimes regular occurance and a symptom of working in the games industry, but during this he tells me he is going to have words with his son.  Apparently when he got home he found his PC on, a device with a 450 Watt power supply and all the bells and whistles and uber gfx card, as you would expect for a games write, left on with the screen saver running maybe not the full 450watt consumption but a damn site more than 2.5Watts.  Plugged into said “small replica if blackpool illuminations” was his son’s GIRLFRIEND’S iPod on charge.  Some explaining was given to the son on his return.

So a lesson for probably quite a few people out there, it might be a good idea to not use your PC as the sole charge device for your USB /Firewire enabled kit IF your not using it.

You could however try this

http://www.ladyada.net/make/mintyboost/

but best of all is Go Gadget Go (not mentioned on his blog yet) who can charge all his devices with solar power, which I find soooo cool.

Blood Sweat and Tea

Earlier this week I was late for work, which is no big deal and it gave me the chance to listen to radio 4 programming that is not the news.  I was listening to Midweek with guests like Rupert Everett two and two artists and the last guest was a man who has a blog.  I enjoyed the other guests but this last guest was intriguing. 

He is an inner city London ambulance driver who for the past two years has kept a blog.  Quite a popular blog, but until this interview I had not come across it.  Some bright spark publisher has had the idea to take this man’s writing and turn it into a book. 

Blood, Sweat and Tea: Real Life Adventures in an Inner-city Ambulance

Now this isn’t the first time I’ve heard of blogs being turned into books so far I have heard of two

1. The confessions of a Video Store Clerk (Yes that kind of video store)
2. The Best Software Writing I: Selected and Introduced by Joel Spolsky

I have read the blog of number one and own number 2 (which I wish I didn’t as whilst the writing is good I am undecided about how I feel about a compilation of others works in a book I do hope the profits are shared out to the other writers or at least to an agreed charity, if not this is why I am uneasy about it).  Its also as dry as you can get.

However this blokes blog/book has some great stories in it and is worth hunting down.  As the title suggest it details the day to day activity of an Ambulance driver the highs and lows and the stupidity of the human race. Why casualty is feeding the nation nonsese and what people dial 999 for will horrify you, unless of course your one of those people who have never actually had proper flu and think a bad cold qualifies. Please note: Doctors, Hospitals and Ambo’s (His name for Ambulances) can’t cure colds and flu, flu vaccines don’t cause flu (the virus components are dead), antibiotics will not clear up a runny nose.

For those in the UK you can try to “Listen Again” on the BBC website http://www.bbc.co.uk/radio4/midweek.

The blog is still up and running and you can continue to read about these adventures on

http://randomreality.blogware.com/blog

 

Last Note.  I recommend the video store thing too but not at work, learn the horrors of DVD returns and the bliss of the anti-bac handwash.

 

tags: , , ,

Posted in Misc, blog. No Comments »

Sharepoint GetUsageData In a WebPart

  So you want the world to see your sites Usage data, except you have a problem, access to the reports is only provided to the administrators of a site.  Well you could give everyone administrator rights and you could gnaw off your own foot but neither is a good idea the administration one less so.

So I know I’ll do this in a web part and just call some impersonation stuff, you know that WindowsImpersonationContext object will do fine. So you build a part, do the “Mike Yarwood” and whoooah now no one ‘cept you, can see your site.

So this is the point you get stuck, and think “google”.  After some searching I found Patrick Tisseghem’s blog with a post on this very subject.

First he solves your “Bang” no one see this, which could come in handy for Admin only web parts

SPControl.GetContextSite(HttpContext.Current).CatchAccessDeniedException = false;

But then comes up with good a old .NET running under COM+ example.  His example shows a webs.count usage, but UsageData works in the same way.

Another helpful article I found later in trying to get this mare to work ( I just love mixing Sharepoint, Gac, Com+  IISReset is your friend!!!)  was found here.  This article fills in some blanks about auto configuring the COM+ Application settings, What it doesn’t do is set the correct user, Make sure you go and set this else it will not work, make sure the user has sharepoint admin access to ALL areas, cos this is a web part anyone can use remember.

When using GetUsageData, remember that this information is gathered once a day so plenty of Caching opportunities here, don’t keep doing cross process communication as this has to be a Server component not a library one.

One thing that does puzzle me, is why dont my components spin in the Component Services tool when called ?

So Why ain’t I spinning then ? (and yes timeout of 3 minutes is set)

And one more thing, try at your own risk.  Im not saying it doesn’t work, just that you should investigate whats happening before you do it.

tags: , , ,

Parenting

When you hear this from your four year old you have to wonder is this a good thing or bad.

Dad, have you seen my boba-fet ?

I need my light sabre !

sung - “Sherree da di da da, bop the cat bah”

I don’t care that the lyrics aren’t right but the fact that my kid is trying to sing rock the cashbah - The Clash at the top of his voice was great.

Down side

Macdonalds spoiling all my good work by giving out crappy pop music players

We don’t do MacD’s very often but the one time we do, damn.

Posted in Misc. No Comments »

Core2Duo Upgrade

Well I finally upgraded my home PC, its been a while I was making do with an AMD XP1900+ and whilst faster than my previous PC it never behaved like the workhorse that was promised.  I mean take video encoding or picture manipulation, it was slow, real slow, primarily due to the lack of extensions in the chip that the intel had.

Living with the AMD was also a problem, it was so loud and cost to make it cool quitely was a little steep so it stayed upstairs in the backroom.  You could hear this thing from outside the room with the door closed.

This new chip though, talk about small, talk about cool, talk about quiet.  Hey I’m just using the standard components that come, admittedly the graphics card I have at this minute is passive cooled, but this new build is almost totally silent.  I have it in the lounge and you cant hear it above the TV, Sky+ box and of course the wind tunnel that is the xbox360.

Anyway a little advise for those thinking of upgrading to a core2duo.  BEWARE.  Hardly anyone else has one and when your build goes wrong for whatever reason there is no one to swap parts with.

Take my build put all the pieces together, turned it on, wheeeee……off……wheeeeee…off.  Not good.  So what is it Motherboard, Chip, Hard drive, graphics ?  I trim it down to smallest amount of components and connections, power, memory*1, chip, on switch.  Same again.  Swap Memory.  Same again.

Ring…Ring… “Steve Help”, “Well I have AMD64 mate with DDR not DDR2, but I have a card to try, bring it over”.

Long rambling short, the video card was duff, put in any machine it stops it booting, but had this not been the problem I was pretty much stuffed in diagnosing chip vs motherboard.

So bear that in mind if you upgrade, try and have a spare available for as much as possible (oh and a PCI graphics card is really handy).

Bionic Woman

Source BBC News

A former US Marine has become the first woman in the world to be fitted with a “bionic” arm that she can control by her thoughts alone.

Its a start, but a pretty amazing one.

tags:

XNA Physics API

Can’t speak for it but I found the link on the forum and thought I’d publish it, just to raise its profile a little more.

Project is here

tags: , ,