2014-09-03 72 views

回答

1

您可以实现自定义令牌并使用您的字段。我认为这将是您的问题的最干净的解决方案。您可以添加自定义算法以确保ID是唯一的,或者您可以使用Guid.NewGuid()。您可以检查如何在此blog post中创建自定义令牌。

+0

听起来不错!会尝试 – CosX 2014-09-03 13:25:30

0

好的。我想出了一个受nsgocev博客文章启发的解决方案。我们的ID需要存储在某个地方,所以我在/ sitecore/content /中创建了一个项目,它将最后一个ID存储为一个字符串。将开始设置为“AA000000”。我们的ID有一个“AA”和6位数的前缀。

这是一个计数逻辑:

Namespace Tokens 

Public Class GeneratedArticleId 
    Inherits ExpandInitialFieldValueProcessor 

    Public Overrides Sub Process(ByVal args As ExpandInitialFieldValueArgs) 
     If args.SourceField.Value.Contains("$articleid") Then 
      Dim database = Sitecore.Client.ContentDatabase 
      Dim counter = database.GetItem(New ID("Our Item")) 

      If counter Is Nothing Then 
       args.Result = "" 
       Exit Sub 
      End If 

      Dim idfield = AppendToIdValue(counter("ID")) 

      Using New SecurityDisabler() 
       counter.Editing.BeginEdit() 
       counter.Fields("ID").Value = idfield 
       counter.Editing.EndEdit() 
      End Using 

      If args.TargetItem IsNot Nothing Then 
       args.Result = args.Result.Replace("$articleid", idfield) 
      End If 
     End If 
    End Sub 

    'Extracts the digits and adds one 

    Private Shared Function AppendToIdValue(ByVal id As String) 
     Dim letterprefix = Left(id, 2) 
     Dim integervalue = CInt(id.Replace(letterprefix, "")) 
     integervalue += 1 
     Return letterprefix & integervalue.ToString("000000") 
    End Function 
End Class 
End Namespace 

我们还需要我们的类添加到Web配置文件。补丁给出类:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
     <sitecore> 
     <pipelines> 
      <expandInitialFieldValue help="Processors should derive from Sitecore.Pipelines.ExpandInitialFieldValue.ExpandInitialFieldValueProcessor"> 
      <processor patch:after="*[@type='Sitecore.Pipelines.ExpandInitialFieldValue.ReplaceVariables, Sitecore.Kernel']" type="OurLibrary.Tokens.GeneratedArticleId, OurLibrary"/> 
      </expandInitialFieldValue> 
     </pipelines> 
     </sitecore> 
    </configuration> 

现在,当我们创建标记“$条款ArticleID”的新项目,该ID是AA000001。下一个将是AA000002等。

谢谢@nsgocev的资源和答案。

相关问题