Sharepoint 2013 - 如何以编程方式清除(删除)单值分类(托管元数据)字段

时间:2014-09-11 20:46:53

标签: sharepoint-2013 taxonomy

我有一个事件监听器,可以在发生操作时更新列表。该列表有两个需要更新的分类字段:“状态”和“产品”

State是一个多值字段,Product是单值字段。我可以毫无问题地清除“状态”字段,但在尝试清除“产品”字段时会出错。以下是代码:

Private Sub RemoveStateProduct(ctx As ClientContext, stateGuidToRemove As String, id As String)
    Dim listItems As ListItemCollection
    Dim queryXml As String = String.Empty
    Dim camlQueryForItem As New CamlQuery()

    ' get list item from item id
    Dim list As List = ctx.Web.Lists.GetByTitle("StateProductList")
    Dim fields As FieldCollection = list.Fields
    Dim statefield As Field = fields.GetByInternalNameOrTitle("States")
    Dim productfield As Field = fields.GetByInternalNameOrTitle("Product")

    queryXml = "<View>" & vbCr & vbLf & _
        "                                <Query>" & vbCr & vbLf & _
        "                                    <Where>" & vbCr & vbLf & _
        "                                        <Eq>" & vbCr & vbLf & _
        "                                            <FieldRef Name='ID'/>" & vbCr & vbLf & _
        "                                            <Value Type='Counter'>!@itemid</Value>" & vbCr & vbLf & _
        "                                        </Eq>" & vbCr & vbLf & _
        "                                    </Where>" & vbCr & vbLf & _
        "                                </Query>" & vbCr & vbLf & _
        "                            </View>"
    camlQueryForItem.ViewXml = queryXml.Replace("!@itemid", id)
    listItems = list.GetItems(camlQueryForItem)
    ctx.Load(listItems)
    ctx.Load(fields)
    ctx.Load(statefield)
    ctx.Load(productfield)
    ctx.ExecuteQuery()

    Dim stateTaxFieldAs TaxonomyField = ctx.CastTo(Of TaxonomyField)(statefield)
    Dim productTaxFieldAs TaxonomyField = ctx.CastTo(Of TaxonomyField)(productfield)
    Dim item As ListItem = listItems(0)

    Dim productTaxFieldValue As TaxonomyFieldValue = Nothing
    Dim stateTaxFieldValueCollAs TaxonomyFieldValueCollection = Nothing
    Dim stateTermValueString As String = String.Empty
    Dim tempString As String = String.Empty


    stateTaxFieldValueColl= TryCast(item("State"), TaxonomyFieldValueCollection)
    For Each tv As TaxonomyFieldValue In taxfieldvaluecollState
        tempString = tv.WssId & ";#" & tv.Label & "|" & tv.TermGuid
        If Not stateGuidToRemove.Equals(tv.TermGuid) Then
            stateTermValueString = stateTermValueString & tempString & ";#"
        End If
    Next

    stateTermValueString = stateTermValueString.TrimEnd("#", ";")
    stateTaxFieldValueColl= New TaxonomyFieldValueCollection(ctx, stateTermValueString, taxfieldState)
    taxfieldState.SetFieldValueByValueCollection(item, taxfieldvaluecollState)

    If stateTermValueString = String.Empty Then
        productTaxFieldValue = TryCast(item("Product"), TaxonomyFieldValue)
        'productTaxFieldValue = New TaxonomyFieldValue()
        productTaxFieldValue.WssId = 0 '-1
        productTaxFieldValue.Label = Nothing 'String.empty() ; vbNull
        productTaxFieldValue.TermGuid = Nothing 'String.empty() ; vbNull
        taxfieldProduct.SetFieldValueByValue(item, productTaxFieldValue)
    End If

    item.Update()
    ctx.Load(item)
    ctx.ExecuteQuery()

End Sub

错误发生在最后一节:

    If stateTermValueString = String.Empty Then
        productTaxFieldValue = TryCast(item("Product"), TaxonomyFieldValue)
        'productTaxFieldValue = New TaxonomyFieldValue()
        productTaxFieldValue.WssId = 0 '-1
        productTaxFieldValue.Label = Nothing 'String.empty() ; vbNull
        productTaxFieldValue.TermGuid = Nothing 'String.empty() ; vbNull
        taxfieldProduct.SetFieldValueByValue(item, productTaxFieldValue)
    End If

如果stateTermValueString为空,我知道我必须清除'Product'字段。我已经尝试创建一个新的TaxonomyFieldValue对象并将值设置为Nothing(由this文章建议,尽管这是2010年)。我还尝试使用项目的“产品”字段并重置其值以使其为空。我已经为WssId,Label和Guid字段尝试了不同的值组合,如这些行的注释部分所示。代码返回错误:

creatingField

就是这样。这是我收到的错误消息的全文。当我检查源代码时,只有这样:Microsoft.SharePoint.Client.Runtime

所以,问题是:我如何以编程方式清除单值分类法字段?

1 个答案:

答案 0 :(得分:0)

找到解决方案。对于单值分类法字段,您还必须更新隐藏的备注字段,但不能通过TaxonomyFieldValue对象执行此操作。您必须使用数组直接更新项目字段,并使用字符串更新注释字段。

加载分类法字段和项目后,必须重新加载项目字段并执行查询,然后使用上下文加载隐藏的备注字段。

Dim stateTaxField As TaxonomyField = ctx.CastTo(Of TaxonomyField)(statefield)
Dim productTaxField As TaxonomyField = ctx.CastTo(Of TaxonomyField)(productfield)
Dim item As ListItem = listItems(0)

'Add these lines
ctx.Load(stateTaxField)
ctx.Load(productTaxField)
ctx.Load(ctx.Web.Fields, Function(ff) ff.Include(Function(f) f.Id, Function(f) f.InternalName))
ctx.ExecuteQuery()
Dim productNoteField As Field = ctx.Web.Fields.ToList.SingleOrDefault(Function(f) f.Id.Equals(productTaxField.TextField))

我修改了最后一部分,因为用空值更新项目字段造成了很多麻烦:

If termValueString = String.Empty Then
    item(productfield.InternalName) = String.Format("-1;#{0}|{1}", String.Empty, Guid.Empty).ToArray
    item(productNoteField.InternalName) = String.Format("{0}|{1}", String.Empty, Guid.Empty)
End If

productTaxFieldValue 不再使用,可以删除。 This article最有用的是确定如何格式化字段字符串(并解释产品字段需要是一个数组,即使只是一个单独的&#39;空字符串)。希望这可以帮助其他有需要的人。