another technical blog...technically

Tuesday, April 2, 2013

Workflow task SPUser field

Working with workflow, i had this problem: i created a User type custom field to map who is the user who started the workflow.
I know there is a builtin field which does this work, but i need to create a custom one. So i tried to set the value with this code:
this.taskProperties.ExtendedProperties["RequestedBy"] = this.WorkflowProperties.OriginatorUser;
The result was an exception. I tried to create an SPUserFieldValue in order to set the extended property using this object. Since i receive another exception i looked into the ULS logger and i found this:

System.InvalidOperationException: The platform does not know how to deserialize an object of type Microsoft.SharePoint.SPFieldUserValue. The platform can deserialize primitive types such as strings, integers, and GUIDs; other SPPersistedObjects or SPAutoserializingObjects; or collections of any of the above. Consider redesigning your objects to store values in one of these supported formats, or contact your software vendor for support.
The code below solved the problem
this.taskProperties.ExtendedProperties["RequestedBy"] = this.WorkflowProperties.Originator;
Morality is: don't try stupid things and also refer to primitive types.
Share:
written in: Milano, Italia

4 comments:

  1. Replies
    1. Hi Martin, you're right, i wrote a wrong snippet.

      I edited the post right now, Originator is a string type and must be used instead of OriginatorUser (SPUser type) in order to avoid the above error.

      Thanks :)

      Delete
  2. Thanks Valerio.

    Along similar lines: If anyone is doing this with a user field other than the out-of-box ones, the following is a way to get the loginName string written into ExtendedProperties.

    var userVal = new SPFieldUserValue(workflowProperties.Web, this.workflowProperties.Item["myFieldInternalName"] as string);

    myTask_TaskProperties.ExtendedProperties.Add(myFieldGUID, userVal.User.LoginName);

    ReplyDelete

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?