通过对象模型上传List Template

List template文件放在Site Actions – Galleries  - List templates中。

1.上传.stp文件

 

   1: Private Sub UploadTemplates(ByVal _site As SPSite)
   2:     Dim directory As String = String.Format("{0}\ListTemplates", Application.StartupPath)
   3:     Try
   4:         If System.IO.Directory.Exists(directory) Then
   5:             Dim templates As String() = System.IO.Directory.GetFiles(directory, "*.stp", System.IO.SearchOption.TopDirectoryOnly)
   6:             Dim listTemplates As SPDocumentLibrary = TryCast(_site.GetCatalog(SPListTemplateType.ListTemplateCatalog), SPDocumentLibrary)
   7:             If Not IsNothing(listTemplates) Then
   8:                 For Each template As String In templates
   9:  
  10:                     Dim fileInfo As New System.IO.FileInfo(template)
  11:                     Dim query As New SPQuery()
  12:                     query.Query = String.Format("<Where><Eq><FieldRef Name='FileLeafRef'/>" & "<Value Type='Text'>{0}</Value></Eq></Where>", fileInfo.Name)
  13:                     Dim existingTemplates As SPListItemCollection = listTemplates.GetItems(query)
  14:  
  15:                     Dim Ids As Integer() = New Integer(existingTemplates.Count - 1) {}
  16:                     For i As Integer = 0 To existingTemplates.Count - 1
  17:                         Ids(i) = existingTemplates(i).ID
  18:                     Next
  19:                     For j As Integer = 0 To Ids.Length - 1
  20:                         listTemplates.Items.DeleteItemById(Ids(j))
  21:                     Next
  22:  
  23:                     Dim stp As Byte() = System.IO.File.ReadAllBytes(template)
  24:                     listTemplates.RootFolder.Files.Add(fileInfo.Name, stp)
  25:                 Next
  26:             End If
  27:         End If
  28:     Catch ex As Exception
  29:  
  30:     End Try
  31: End Sub

2.得到ListTemplate Collection

 

 

 

   1: Dim listTemplates As SPListTemplateCollection = _site.GetCustomListTemplates(_web)
   2:  ListBox1.Items.Clear()
   3: For Each listTemplate As SPListTemplate In listTemplates
   4:     ListBox1.Items.Add(listTemplate.Name)
   5: Next

3.通过List template创建SPList

   1: Private Sub CreateList(ByVal _site As SPSite, ByVal _web As SPWeb, ByVal listTemplateName As String)
   2:  
   3:     Dim listTemplates As SPListTemplateCollection = _site.GetCustomListTemplates(_web)
   4:  
   5:     Dim listTemplate As SPListTemplate = listTemplates(listTemplateName)
   6:     _web.Lists.Add(listTemplateName, "description", listTemplate)
   7:  
   8: End Sub

你可能感兴趣的:(通过对象模型上传List Template)