Tuesday 2 April 2013

Output Task Fields

When you are developing workflows there`s many times when you need to interpret fields stored in your task list item or workflow properties contained in a hash table. Now you could very well step through your code but I find that way too much of a nuisance so I just created a class that takes in a file path, creates a log file and dumps the contents of these collections. it`s simple to implements but hey this is how I did it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
namespace SharePoint.Magic.Workflow.Helper
{
public class propertyWriter
{
    private string _path = null;
    /// <summary>
    /// constructor to include file path where generated log files will be stored
    /// </summary>
    /// <param name="logPath">path to where log files will be stored ie "c:/logs/</param>
    public propertyWriter(string path)
    {
        if (Directory.Exists(path))
        {
            _path = string.Concat(path, string.Format("log{0}.txt"Directory.GetFiles(path, "log*.txt").Length));
        }
        else
        {
            throw new Exception(string.Format("Path does not exist, please ensure that {0} is a valid path", path));
        }
 
    }
 
    /// <summary>
    /// Print out list of keys with values from hash table
    /// </summary>
    /// <param name="properties">Hashtable to print</param>
    /// <param name="sectionTitle">section title you want in file</param>
    /// <param name="keys">array of keys that are of interest, ommiting will result in all entries being listed</param>
    public void writeProperties(Hashtable properties, string sectionTitle, params string[] keys)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(_path, true))
        {
            file.WriteLine(sectionTitle);
            int i = 0;
 
            foreach (DictionaryEntry de in properties)
            {
                if (keys.Contains<string>(de.Key.ToString()) || keys.Length == 0)
                    file.WriteLine(string.Format("{3}\t {0}\t({1}) \t {2}", de.Key, de.GetType(), de.Value, i));
                i++;
            }
            file.WriteLine("");
        }
    }
 
    /// <summary>
    /// Print out all spfields in spfield collection of splistitem with keys and associated values
    /// </summary>
    /// <param name="li">sharepoint list item of interest</param>
    /// <param name="sectionTitle">section title you want in file</param>
    /// <param name="keys">array of keys that are of interest, ommmiting will result in all entries being listed</param>
    public void writeProperties(SPListItem li, string sectionTitle, params string[] keys)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(_path, true))
        {
            file.WriteLine(sectionTitle);
            int i = 0;
 
            foreach (SPField f in li.Fields)
            {
                if (keys.Contains<string>(f.Title) || keys.Length == 0)
                {
                    string value = li[f.Title] != null ? ((object)li[f.Title]).ToString() : "Null";
                    string ph = string.Format("{3}\t {0}\t({1}) \t {2}", f.Title, f.Type.ToString(), value, i);
                    file.WriteLine(ph);
                }
                i++;
            }
            file.WriteLine("");
        }
    }
 
    /// <summary>
    /// Print out a list of values that you supply as a string array
    /// </summary>
    /// <param name="sectionTitle">Section title you want in the file</param>
    /// <param name="values">Array of string values you would like displayed in the file.</param>
    public void writeValues(string sectionTitle, params string[] values)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(_path, true))
        {
            file.WriteLine(sectionTitle);
            int i = 0;
 
            foreach (string value in values)
            {
                file.WriteLine(string.Format("{0}\t{1}",i++, value));
            }
            file.WriteLine("");
        }
    }
 
    public void writeString(string key, string value)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(_path, true))
        {
            file.WriteLine(string.Format("{0}: {1}",key,value));
                
            file.WriteLine("");
        }
    }
}
}