在运行时创建 SharePoint 服务中的新列表






3.64/5 (6投票s)
2006年5月17日
1分钟阅读

52736
如何在 SharePoint 服务区域中在运行时创建新列表。
引言
由于没有找到关于如何在 SharePoint Portal Server 运行时创建新列表的文档,我决定发表我的发现,以便其他人也能从中受益。
背景
在最近的企业内网重新设计中,我们发现有必要在 SharePoint Portal Server 区域创建具有一致结构的新列表。 只是一个问题:在调用 Update
时尝试提交新列表时,代码会失败。
一些假设
- 您熟悉在 VS.NET 中创建 WebPart。
- 您已在开发 PC 上安装并运行了 WebPart 框架。
使用代码
首先获取您想要操作的当前区域的引用。 我假设它是 WebPart 正在运行的当前区域,因此引用 Context
。
Dim thisWeb As SPWeb = SPControl.GetContextWeb(Context)
创建一个对您想要添加到区域的新列表的引用。 该引用必须是 Guid
,因为 SPList
接受这种类型的标识符。
我将 GenericList
模板分配给列表,因为我希望从一个干净的列表模板开始。 如果您想使用通用列表格式,可以将任何 SPListTemplateType
模板分配给列表。
Dim docLibName As String = "New List Name"
Dim guid As System.Guid = thisWeb.Lists.Add(docLibName, _
"A custom list created during runtime.", SPListTemplateType.GenericList)
添加您希望在字段中拥有的字段。 首先应用全局设置,并在将字段添加到列表之前强制执行 Update
。
Dim newList As SPList = thisWeb.Lists(guid)
With newList
.AnonymousPermMask = SPRights.EmptyMask
.EnableVersioning = True
.OnQuickLaunch = True
.ReadSecurity = 1
.WriteSecurity = 2
.Update()
.Fields.Add("Text field one", SPFieldType.Text, True)
.Fields.Add("Text field two", SPFieldType.Text, False)
.Fields.Add("Boolean field", SPFieldType.Boolean, False)
End With
据我所知,任何模板都会“强制”在列表上的唯一字段是 Title
字段。
关注点
在添加新字段后,不要再次调用 Update
! 如果您调用另一个 Update
,那么您将在下一个 Update
中收到以下异常
Save Conflict
Your changes conflict with those made concurrently by another user.
If you want your changes to be applied, click Back in your Web browser,
refresh the page, and resubmit your changes.