Getting SharePoint Enumerable Field Values

More a quick tip than anything. In the below are two helper methods that make the process a lot easier. The first method will help to normalize the values for handling things like delimiters:

[csharp]
private static string NormalizeValue(string fieldValue)
{
if (!string.IsNullOrEmpty(fieldValue))
{
var str = fieldValue;
if (str.StartsWith(SPFieldMultiChoiceValue.Delimiter))
{
str = str.Substring(SPFieldMultiChoiceValue.Delimiter.Length);
}
if (str.EndsWith(SPFieldMultiChoiceValue.Delimiter))
{
str = str.Substring(0, str.Length – SPFieldMultiChoiceValue.Delimiter.Length);
}
return str.Replace(SPFieldMultiChoiceValue.Delimiter, “, “);
}
return string.Empty;
}

[/csharp]

Secondly, we use the helper method to return the enumerable value in a nice generic format.

[csharp]

public static T ReturnEnumValue(SPListItem listItem, string fieldName, T defaultValue) where T : struct
{
var str = listItem[fieldName] as string;
if ((str == null) || string.IsNullOrEmpty(str))
{
return defaultValue;
}
var normalized = NormalizeValue(str);
return (T) Enum.Parse(typeof (T), normalized);
}
[/csharp]

Share

Programatically Working With TFS Check-In Policies

Check-in policies within mature development environments are imperative, allowing the construction of code, to police checking in of code. Policies within TFS aid in enforcing restrictions and limitations whenever files are checked into TFS VC. TFS supplies a multitude of pre-existing check-in policies for actions like checking that unit tests are implemented, executing static code analysis to check for standards, and a bunch of others.

Unfortunately, there are some limitations however which I ran into recently with a current client of mine. The long and short of it is check-in policies cannot be included in process templates. However, there are custom development avenues you can use by building some managed code against the TFS API to execute this, the objects and methods that are used are fairly self explanatory. Consider the following that shows getting a collection of the current policies, and then setting the policies.

Firstly, getting an appropriate reference to the desired project:

[csharp]
TeamFoundationServer tfs = new TeamFoundationServer(http://tfs:8080);
VersionControlServer vcs = (VersionControlServer) tfs.GetService(typeof(VersionControlServer));
TeamProject tp = vcs.GetTeamProject(“Project”);
[/csharp]

Off the TeamProject object instance, you can following call the GetCheckinPolicies method, which will allow you to return a Microsoft.TeamFoundation.VersionControl.Client.PolicyEnvelope array type. Essentially this is what we can refer to as the policy instance, representing the applied policy’s of type IPolicyDefinition. Since you have an active array, you can do push the array into a typed collection of PolicyEnvelope objects using List.AddRange

[csharp]
tp.SetCheckinPolicies(policyCollection.ToArray());
[/csharp]

Share

Getting Safe String Values

I know there are better ways of getting SharePoint field values then indexing the SPListItem for a particular field and using archaic string manipulation to make things dance. But sometimes when you are retrofitting a legacy application it can be intrusive to the holistic application and cause all sorts of issues in other portions.

While doing a code review today at a client this came up. We were discussing when using the SPListItem indexing approach how to best get safe string values. The extension method in use that we were discussing today was:

[csharp]
public static string ToSafeString(this T obj)
{
if (typeof(T).IsValueType)
{
return obj.ToString();
}
return Equals(obj, null) ? string.Empty : obj.ToString();
}

[/csharp]

Now I don’t think that this is too bad, but doesn’t Convert.ToString() already handle null values? I guess I can see the use since it will show on every derived class depending on the implementation of the extension and might look more natural from a language perspective. Furthermore, I find the value type guard clause unnecessary, and could be simply truncated as written as a helper method i.e.:

[csharp]
return value == null ? string.Empty, obj.ToString();
[/csharp]

Share