Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Custom Workflow Action

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.

  1. Start with an empty SharePoint Project. Name the project CustomWorkflowActions, as shown in Figure 10-18, and select a location easily located.
    images

    Figure 10-18. Empty SharePoint project for custom workflow action

  2. Add a new class called ProcessNewBookAction, as seen in Figure 10-19.
    images

    Figure 10-19. New custom action class

  3. Add the method shown in Listing 10-1 to the 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.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint