Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Visual Studio provides the ability to build custom functionality that can be added to the workflows within SharePoint Online. This project will build a custom action that processes the Title and BookAuthor fields of the Books list into proper casing.
Figure 10-18. Empty SharePoint project for custom workflow action
ProcessNewBookAction, as seen in Figure 10-19.
ProcessNewBookAction class:
Listing 10-1. Implementation of the ProcessNeweBook() method
public Hashtable ProcessNewBook(SPUserCodeWorkflowContext context)
{
Hashtable response = new Hashtable();
try
{
using (SPSite site = new SPSite(context.CurrentWebUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList bookList = web.Lists[context.ListId];
SPListItem currentBook = bookList.GetItemById(context.ItemId);
//proper case title and author
CultureInfo culture = CultureInfo.CurrentCulture;
TextInfo textInfo = culture.TextInfo;
currentBook["Title"] = textInfo.ToTitleCase(currentBook[“Title”].ToString().ToLower());
currentBook["BookAuthor"] =
textInfo.ToTitleCase(currentBook["BookAuthor"].ToString().ToLower());
currentBook.Update();
response["result"] = "success";
}
}
}
catch (Exception ex)
{
response["result"] = "error: " + ex.Message;
}
return response;
}
The method returns a hashtable to the workflow. This hashtable will hold any return values for the workflow to process. This particular method only returns the status of the action.
The method accepts a SPUserCodeWorkflowContext parameter. This holds the contextual information for the SharePoint site where the workflow is running. Using the context, the method accesses the list and the specific item currently being referenced. The CurrentCulture.TextInfo object is used to set the Title and BookAuthor fields to the proper casing. This only works on lowercased values, so the values use the ToLower() method. The list item is updated and the result is returned.