Archive for the Snippets Category

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

Advertisements

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

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.