Archive for April, 2008

While I’m here…….

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

I remembered a couple of other little SharePoint snippets that I have found useful.

  1. Want to use that lovely green spinning icon that SharePoint uses when its doing something? Try the SPLongOperation class – good example here.
  2. Need to set the audience for a webpart through code? This is what I used!.
  3. Get the current user in InfoPath (not strictly SharePoint, but uses the SharePoint web services). Read this.
  4. Convert the XML that SharePoint returns into a dataset (I find myself using this all the time, probably because I don’t know better!):


public DataSet NSXmlToDataSet(string xmlString)
{
DataSet ds = new DataSet();
XmlDocument xd = new XmlDocument();

xd.LoadXml(xmlString);

XmlNamespaceManager nsMgr = new
XmlNamespaceManager(xd.NameTable);
nsMgr.AddNamespace("rs",
xd.DocumentElement.GetNamespaceOfPrefix("rs"));

XmlNode xn = xd.SelectSingleNode("//rs:data", nsMgr);

XmlNodeReader reader = new XmlNodeReader(xn);
ds.ReadXml(reader);

return ds;
}

Si

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