Monthly Archives: September 2011

How to add directly to the Workflow History from a custom SharePoint workflow

This one was a little hard to track down. How do you write directly to the SharePoint workflow history? It’s easy enough to add a LogToHistory activity and set the HistoryDescription and HistoryOutcome, but what if you want to create those entries dynamically?

After toying around with different ways of trying this, the answer was pretty easy:

Here’s the code sample:

SPMember usermember = workflowProperties.OriginatorUser;
SPWorkflow.CreateHistoryEvent(this.workflowProperties.List.ParentWeb,
this.workflowId,
[...]

By |September 12th, 2011|Sharepoint on Premise|7 Comments|

How to pull the email addresses from a SharePoint User field (allow multiple users)

To pull the user information, you first have to convert the field into a SPFieldUser object, and then create a collection of the SPFieldUserValueCollection.

Then, loop through the collection and pull the Email property of the SPFieldUserValue.

code sample:
———————————————————————-
string sFieldNameTo = “BU Legal To Email”;
string sFieldNameCC = “BU Legal CC Email”;

//Add To and CC based on multiple user field
if (workflowProperties.Item[sFieldNameTo] != null)
{
SPFieldUser UsersColumn = (SPFieldUser)workflowProperties.Item.Fields.GetField(sFieldNameTo);
SPFieldUserValueCollection Users = (SPFieldUserValueCollection)UsersColumn.GetFieldValue(workflowProperties.Item[sFieldNameTo].ToString());

foreach (SPFieldUserValue fieldUser in Users)
{
if (fieldUser.User.Email != null && fieldUser.User.Email != string.Empty)
{
mail.To.Add(fieldUser.User.Email);
}//
}//

}//

if (workflowProperties.Item[sFieldNameCC] != null)
{
SPFieldUser UsersColumn = (SPFieldUser)workflowProperties.Item.Fields.GetField(sFieldNameCC);
SPFieldUserValueCollection Users = (SPFieldUserValueCollection)UsersColumn.GetFieldValue(workflowProperties.Item[sFieldNameCC].ToString());

foreach (SPFieldUserValue fieldUser in Users)
{
if (fieldUser.User.Email != null && fieldUser.User.Email != string.Empty)
{
mail.CC.Add(fieldUser.User.Email);
}//
}//

}//

 

Updated 5/11/16

For CSOM use this:

foreach (FieldUserValue userValue in item[“MultiUser”]as FieldUserValue[])
{

}//

 

 

By |September 12th, 2011|Sharepoint on Premise|1 Comment|