People in the field

Posted in SharePoint, Snippets with tags , , on April 25, 2008 by Si

SharePoint again folks! When dealing with users and groups (for example with list columns), SharePoint likes to store the underlying value in it’s own special format, usually ID;#Value (e.g “1;#neboddy”). That’s all well and good, but when you need to retrieve that value and display it in a friendly format (e.g. “Mr N E Boddy”) then there’s some work to do.

Fortunately SharePoint gives you a quick and easy way of getting at all the user properties for Mr Boddy (or anyone else for that matter!): SPFieldUserValue. Tada! Create a new object of that type, give it the value you’ve read from the list field, and then point an SPUser object at it, hey presto, you’ve got everything you could want. This saves having to write your own parser, and if Microsoft change the format down the line, they should update this class in the process.

Here is some lovely (delete if appropriate) code to demonstrate. This code was written to return either login names or email addresses for a multi-user field, hence the foreach() loop:


public string GetUserListFromField(SPWeb web,
                 string fieldValue,
                 bool returnEmail)
{
    string returnValue = "";

    //get collection of users from passed-in value
    SPFieldUserValueCollection allUsers = new
      SPFieldUserValueCollection(web, fieldValue);

    foreach(SPFieldUserValue fieldUser
               in allUsers)
    {
        //convert value to user
        SPUser user = fieldUser.User;

        if (returnEmail)
        {
            //return email address if requested
            returnValue += user.Email + ";";
        }
        else
        {
            //return login names
            returnValue += user.LoginName + ";";
        }
    }

    //trim the trailing semicolon
    int i = returnValue.LastIndexOf(";");
    returnValue = returnValue.Substring(0, i);

    //tidy up
    web.Dispose();

    return returnValue;
}

Now you see it, now you don’t

Posted in SharePoint with tags , , on April 23, 2008 by Si

A lovely thing is a SharePoint document library. Well it is until users are added in to the equation. At which point, either you don’t want them using the toolbar, or they don’t want to see it (or bits of it). Fair enough, just go into the web part properties and hide the blasted toolbar. Job done! Well, no, because if I wanted to template that site and use it again (which I often do), the toolbar settings don’t get saved so I have to go back in and turn them off. Boring!!

So I set about creating a feature that would enable me to quickly set up a site and configure all the different bits, and all was going well until I tried to get at this toolbar setting. You see, its not directly available through the SharePoint object model, and is hard to get at using System.Reflection (I tried lots of code posted on other sites, none seemed to work for me – says a lot about me then!). I was going to skip over it and then I found this cool little web part – Toolbar Manager . Problem solved!

This neat web part sits on the page like any other, and has a simple set of properties which basically hide any or all of the toolbar buttons (and associated menu items) on a list view web part. It is SO easy to use, and the source code is available to so you can customise it if needed. I have added an extra bit to hide the view selector as well. So now the users can have whatever they want to see on the toolbar, and us developers can stop them doing anything but change the view! My next tweak will be trying to get it to add custom buttons as well. It can’t get much better!! Thanks Scot!! (BTW you can download the source code and/or solution packages from CodePlex)

Si

All odd numbers are prime…..

Posted in Uncategorized on March 20, 2008 by Si

Mathematician: 3 is a prime, 5 is a prime, 7 is a prime, and by induction – every odd integer higher than 2 is a prime.
Physicist: 3 is a prime, 5 is a prime, 7 is a prime, 9 is an experimental error, 11 is a prime,…
Engineer: 3 is a prime, 5 is a prime, 7 is a prime, 9 is a prime, 11 is a prime,…
Programmer: 3 is a prime, 5 is a prime, 7 is a prime, 7 is a prime, 7 is a prime,…
Salesperson: 3 is a prime, 5 is a prime, 7 is a prime, 9 — we’ll do for you the best we can,…
Computer Software Salesperson: 3 is prime, 5 is prime, 7 is prime, 9 will be prime in the next release,…
Biologist: 3 is a prime, 5 is a prime, 7 is a prime, 9 — results have not arrived yet,…
Advertiser: 3 is a prime, 5 is a prime, 7 is a prime, 11 is a prime,…
Lawyer: 3 is a prime, 5 is a prime, 7 is a prime, 9 — there is not enough evidence to prove that it is not a prime,…
Accountant: 3 is prime, 5 is prime, 7 is prime, 9 is prime, deducting 10% tax and 5% other obligations.
Statistician: Let’s try several randomly chosen numbers: 17 is a prime, 23 is a prime, 11 is a prime…
Professor: 3 is prime, 5 is prime, 7 is prime, and the rest are left as an exercise for the student.
Computational linguist: 3 is an odd prime, 5 is an odd prime, 7 is an odd prime, 9 is a very odd prime,…
Psychologist: 3 is a prime, 5 is a prime, 7 is a prime, 9 is a prime but tries to suppress it,…
Psychiatrist: 3 is a prime, 5 is a prime, 7 is a prime, 9 would be a prime if it wasn’t for his relationship with 3.
Project Manager: Firstly, what’s a prime, and how long will it take to make 9 one of those?

(plagiarised from somewhere….)

K2 – Accessing environment fields from code

Posted in ASP / ASP.Net, K2 [blackpearl], Snippets on March 20, 2008 by Si

So I needed to get at the K2 blackpearl environment settings from the aspx pages I have been putting together. A quick trawl on the K2 user group (www.k2underground.com) quickly led to some code that lets you do just that (code below is based on that). You need to know the host server name and port number, but I put that in my web.config file to allow for configuration later when we move to the staging and live servers.

Using SourceCode.Workflow.Management;

WorkflowManagementServer server = new WorkflowManagementServer("blackpearl", 5555);

server.Open();

SourceCode.Workflow.Management.StringTable st = server.GetStringTable("Development");

SourceCode.Workflow.Management.StringTableEntry ste = st["field name"].Value;

server.Connection.Close();

Easy! Just remember to add a reference to the right assembly, SourceCode.Workflow.Management.

Joining the ASP.Net party – better late than never

Posted in ASP / ASP.Net, K2 [blackpearl] with tags , , on March 13, 2008 by Si

Yes folks, forever at the cutting edge of technology I have this week taken my first dive into the exciting world of ASP.Net 2, naturally on a key business project with short timescales – that’s the way we like ’em!!

We have been developing some K2 [blackpearl] workflows, and had prepared beautiful InfoPath forms for handling user input and the like. However, trying to deploy the workflows to just six pilot SharePoint sites brought me that close from throwing my laptop out the window and storming off in frustration. K2 is simply not designed for that kind of deployment, but we had to get the pilot rolling and so that’s what we did. New deployments of smart objects and workflows customised for each site – torture!

So…… I am still in the job, and I have an undamaged laptop. We have decided to abandon the InfoPath forms as they were just too limiting, and abandon the pesky smart objects as well, and instead head over to ASP.Net 2 forms, with an even smarter interface! Yay! By doing this, we are able to code behind each page in order to do the stuff we couldn’t easily do before, like identify which site we are coming from, get into the SharePoint object model, and ultimately deploy just one version of our workflows that will apply to all sites.

My starting point for this adventure was the K2 Forms Generation wizard, which was very kind enough to generate a basic page which tied into the workflow server backend, so I had the hard work done for me. All that remained was a bit of css to get the right layout, and perhaps the use of a master page or two (as is good practice). And indeed, here I am, almost at the end of my first week, and I have the first page done! Ok it was a simple “type text in box, select value, click submit” page so not particularly taxing, but an achievement none the less I feel. I also have a tidy structured rough slap-dash class library containing a lot of functions I will use in the rest of the pages, and a crumb trail-type thing generated from an xml file. I’ll be the first to admit that it isn’t necessarily the smartest (in terms of code) solution, but it works, and that for me, at the moment, is the main thing.

Short timescales – well, it’s all got to be done by the end of this month (March), so no pressure! tick, tock, tick, tock……..

Si

Loader Lock error using DirectX AudioVideoPlayback

Posted in .NET, DirectX with tags , , , on March 9, 2008 by Si

I am knocking up a small app and need it to play a few sounds here and there. Now, most of them are straight forward wav files, which is not too much of a problem. However, I also have a few clips in mp3 format, and so have ended up using DirectX as a (supposedly) quick way out – DirectX.AudioVideoPlayback gives you access to a whole host of media functionality. So, having found a few examples on the web I dropped some code in and was then presented with the error message:

LoaderLock was detected

Message: DLL '.......\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

Scary!! LoaderLock is one of the Managed Debug Assistants (MDAs) that Microsoft introduced in VS2005 to help track down and debug runtime issues in managed code. Anyway, here’s how to get round this issue:

  1. In VS2005, go to Debug > Exceptions (Ctrl + Alt + E)
  2. In the Managed Debugging Assistants section, scroll down till you find the offender (LoaderLock)
  3. Untick the box in the “Thrown” column. This is a per-solution setting, not a global setting!

Hey presto! Error message has gone. OK, I can hear you wondering, what about my app out in the real world – will it crash and bring the world to a standstill? Well apparently this MDA only affects code run under the debugger, so won’t be a problem. Incidentally, I tried turning the LoaderLock back on and mysteriously my app decided it no longer wanted to throw the error. Strange. So you may get away with turning it off, running your app then turning it back on if you are really concerned.

Si