After spending way too many hours messing with this, I got it to work. I was trying to update the Assigned To in a Form Library (also works in a Document Library) using a Workflow in Visual Studio 2008. SharePoint being SharePoint, nothing is ever easy.
In the VS I created a Sequential Workflow. Then I added a Code activity (codeActivity1) to the design. After that, double click codeActivity1 in the design window.
Create the Public variable wfP:
Public wfP As SPWorkflowActivationProperties = New Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties
Then add the following code to your codeActivity1 module.
Dim site As SPWeb = New SPSite(wfP.WebUrl).OpenWeb()
Dim list As SPList = site.Lists(wfP.List.Title)
Dim listItem As SPListItem = wfP.Item
listItem(“Assigned To”) = site.Groups(“gLeads”)
listItem.Update()
It should look like this when you are done:
Private Sub codeMoveToLead_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim site As SPWeb = New SPSite(wfP.WebUrl).OpenWeb()
Dim list As SPList = site.Lists(wfP.List.Title)
Dim listItem As SPListItem = wfP.Item
listItem(“Assigned To”) = site.Groups(“gLeads”)
listItem.Update()
End Sub
Trying to set the .AssignedTo never worked for me.
The real issue is that the item has to be updated twice…
newItem = spList.Items.Add();
// assign field values
newItem.Update()
newNewItem = spList.GetItemById(newItem.ID);
newNewItem[“AssignedTo”] = spUser.ID;
newNewItem.Update();
no wait, that didn’t work either
I am trying to update the assignedto in a personal web site list. In other code, using the SpUser.ID works. However, you have to have the SpUser from the current SpWeb.Users… Match the AD user that you get with the SpWeb.User by the email address. Then use the SpUser.ID of the SpWeb.User
here is all my code to update the AssignedTo:
Private Sub Routing()
Dim eFire As hEventFiring = New hEventFiring
Using spsSite As SPSite = New SPSite(sPi.WebUrl)
Using spwWeb As SPWeb = spsSite.OpenWeb
Dim iSPl As SPListItem = spwWeb.Lists(sPi.ListId).GetItemById(sPi.ListItem.ID)
Try
eFire.dEventFire()
Dim roleAssig As New SPRoleAssignment(spwWeb.SiteGroups(mRouter))
roleAssig.RoleDefinitionBindings.Add(spwWeb.RoleDefinitions(“Contribute”))
If iSPl.HasUniqueRoleAssignments = False Then
iSPl.BreakRoleInheritance(True)
End If
For Each spra As SPRoleAssignment In iSPl.RoleAssignments
spra.RoleDefinitionBindings.RemoveAll()
spra.Update()
Next
iSPl.RoleAssignments.Add(roleAssig)
iSPl(“Assigned To”) = spwWeb.Groups(mRouter)
iSPl(“sNoteField”) = mRouter
iSPl.Update()
eFire.eEventFire()
Catch ex As Exception
iSPl(“sNoteField”) = ex.Message.ToString
iSPl.Update()
Finally
End Try
End Using
End Using
End Sub