I'm trying to call AlterTask on a newly cre开发者_Go百科ated Sharepoint task to add extended properties to my task, and a NotSupportedException is thrown.
What's going on?
The newly created task is actually a SharePoint ListItem. ExtendedProperties is specifically a Workflow Task property.
As per the MSDN documentation:
The content type of the item passed to the task parameter is not derived from the WorkflowTask content type.
This means that Content Type of the SPListItem that represents your new Task must be set to "Workflow Task" before the AlterTask method can be called on it:
Dim selectedTaskList As SPList = web.Lists(taskListName)
' Create a new task item
Dim newTask As SPListItem = selectedTaskList.Items.Add()
' Turn the new task item into a Workflow Task
Dim newTaskContentType As Microsoft.SharePoint.SPContentType = web.AvailableContentTypes("Workflow Task")
newTask("ContentTypeId") = newTaskContentType.Id
' Now the AlterTask method will work. (assume you've alreade declared a hashtable of properties to set)
Microsoft.SharePoint.Workflow.SPWorkflowTask.AlterTask(newTask, myHashTable, True)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim IntItemID As Integer
Dim siteId As Guid = SPContext.Current.Site.ID
Dim webId As Guid = SPContext.Current.Web.ID
Using objSpSite As New SPSite(siteId)
Using objSpWeb As SPWeb = objSpSite.OpenWeb(webId)
If Not Page.Request.QueryString("ItemID") Is Nothing And Page.Request.QueryString("ItemID") <> "" Then
IntItemID = CInt(Page.Request.QueryString.Item("ItemID").ToString)
Panel1.Visible = False
txtID.Text = IntItemID.ToString
Dim objList As SPList = objSpWeb.Lists("RequestList")
Dim objListItem As SPListItem = objList.Items.GetItemById(IntItemID)
dtPermission.SelectedDate = objListItem("PermissionDate")
dtTimeFrom.SelectedDate = objListItem("PermissionFromTime")
dtTimeTo.SelectedDate = objListItem("PermissionToTime")
cmbType.SelectedValue = objListItem("PermissionType")
'dtCreated.SelectedDate = objListItem("")
Else
IntItemID = 0
txtID.Text = "New"
dtCreated.SelectedDate = Today
txtCreatedBy.Text = objSpWeb.CurrentUser.Name
Dim objServiceContext As SPServiceContext = SPServiceContext.GetContext(objSpSite)
Dim objUserProfileManager As New UserProfileManager(objServiceContext)
Dim objUserProfile As UserProfile
Dim strUserAccount As String
strUserAccount = objSpWeb.CurrentUser.LoginName.Replace("i:0#.w|", "")
If objUserProfileManager.UserExists(strUserAccount) Then
objUserProfile = objUserProfileManager.GetUserProfile(strUserAccount)
Try
txtManager.Text = objUserProfile.GetManager.AccountName
Catch ex As Exception
txtManager.Text = ex.Message
End Try
End If
Panel2.Visible = False
End If
End Using
End Using
End Sub
Protected Sub cmdSubmit_Click(sender As Object, e As EventArgs) Handles cmdSubmit.Click
Dim siteId As Guid = SPContext.Current.Site.ID
Dim webId As Guid = SPContext.Current.Web.ID
Using objSpSite As New SPSite(siteId)
Using objSpWeb As SPWeb = objSpSite.OpenWeb(webId)
objSpWeb.AllowUnsafeUpdates = True
Dim list As SPList = objSpWeb.Lists("RequestList")
Dim item As SPListItem = list.Items.Add()
item("PermissionDate") = dtPermission.SelectedDate
item("PermissionFromTime") = dtTimeFrom.SelectedDate
item("PermissionToTime") = dtTimeTo.SelectedDate
item("PermissionType") = cmbType.SelectedValue
item("PermissionApprover1") = txtManager.Text
item.Update()
list.Update()
objSpWeb.AllowUnsafeUpdates = False
End Using
End Using
End Sub
Protected Sub cmdApprove_Click(sender As Object, e As EventArgs) Handles cmdApprove.Click
Dim siteId As Guid = SPContext.Current.Site.ID
Dim webId As Guid = SPContext.Current.Web.ID
Using objSpSite As New SPSite(siteId)
Using objSpWeb As SPWeb = objSpSite.OpenWeb(webId)
Dim objList As SPList = objSpWeb.Lists("RequestList")
Dim objListItem As SPListItem = objList.Items.GetItemById(CInt(txtID.Text))
Dim objWFTask As SPWorkflowTask = objListItem.Tasks(0)
If objWFTask Is Nothing Then
' no matching task
Return
End If
' alter the task
Dim ht As New Hashtable()
ht("Status") = "Complete"
ht("PercentComplete") = 1.0F
SPWorkflowTask.AlterTask(TryCast(objWFTask, SPListItem), ht, True)
End Using
End Using
精彩评论