Getting Safe Value From ListItem In Office 365 And SharePoint Online

Reading a ListItem in SharePoint online is nothing fantastic and is pretty similar to reading list items in the throwback server OM models. Consider the below example that starts with a ListItemCollection. We are reading the value of SomeCrap. The problem here is, what if that returns null? That’s an issue.

[csharp]

ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
list.Context.Load(items);
list.Context.ExecuteQuery();
foreach (ListItem item in items)
{
list.Context.Load(item, x => x[“SomeCrap”]);
list.Context.ExecuteQuery();
string someCrap = item[“SomeCrap”].ToString();
}

[/csharp]

There is an easy extension means to handle this though, where instead of null due to the ToString() it will just give the default string.Empty. Here is the extension and an example of its use.

[csharp]

public static T GetSafeValue(this ListItem item, string fieldName) where T : class
{
object o = item[fieldName];
if (o == null || !(o is T))
{
return null;
}
return (T)o;
}

then its use.

[csharp]

foreach (ListItem listItem in items)
{
string someCrap = listItem.GetSafeValue(“SomeCrap”);
}

[/csharp]

Share

Returning The SharePoint Start Workflow Link

Building the “Start Workflow” link is pretty straight forward. I am pretty sure there are better ways to do it, but here is an approach when you have to build the link using a string return. How it works is pretty straightforward. Consuming a SPListItem and SPWorkflowAssociation parameter, the SPListItem exposes the ParentList and ID properties and the SPWorkflowAssociation provides the InstantiationUrl and Id properties. The only field level stuff is I was passing a finish url in the query string (_finalurl in the below). When the link is built, it is cleaned up using the inherent SPHttpUtility.UrlKeyValueEncode method.

[csharp]
private string _finalurl;

public static string QueryStringAppend(string url, string args)
{
if (string.IsNullOrEmpty(url))
{
return url;
}
var num = url.LastIndexOf(“?”);
switch (num)
{
case -1:
return (string.Format(“{0}?{1}”, url, args));
}
return num == (url.Length – 1) ? url + args : string.Format(“{0}&{1}”, url, args);
}

protected string BuildWorkflowStartLink(SPListItem listItem, SPWorkflowAssociation workflowAssociation)
{
var builder = new StringBuilder();
builder.Append(SPHttpUtility.UrlPathEncode(string.Format(“{0}/{1}”, Web.Url, workflowAssociation.InstantiationUrl), true));
builder.Append(“?List=”);
builder.Append(listItem.ParentList.ID.ToString());
builder.Append(“&ID=”);
builder.Append(listItem.ID.ToString());
builder.Append(“&TemplateID=”);
builder.Append(workflowAssociation.Id.ToString(“B”));
string url = _finalurl ?? Request.QueryString[“Source”];
url = QueryStringAppend(url, string.Format(“{0}={1}”, FinishIdName ?? “ID”, listItem.ID));
if (!string.IsNullOrEmpty(url))
{
builder.Append(“&Source=”);
builder.Append(SPHttpUtility.UrlKeyValueEncode(url));
}
return builder.ToString();
}
[/csharp]

:)

Share