创建列表之前检查列表是否已存在
我正在我的 SharePoint 2010 解决方案中使用 xml 代码创建列表定义和列表实例。现在,每次我部署解决方案时,它都会删除该列表并创建一个新列表。我只想在列表不存在时创建该列表。
如何检查列表是否已存在以及将代码放在哪里?
我的列表定义和列表实例出现在我的功能之一的“功能中的项目”中。
I am creating a list definition and a list instance with xml code in my SharePoint 2010 solution. Now, each time I deploy my solution it deletes the list and creates a new one. I only want to create the list if it doesn't exist.
How do I check if the list already exists and where do I put the code?
My list definition and list instance appear among "Items in the Feature" in one of my features.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢您的回答。我在列表实例文件夹中的 SharePointProjectItem.spdata 文件中找到了解决方案。将“DeploymentConflictResolutionBehavior”设置为“None”可以阻止 Visual Studio 在每次部署时删除我的列表。
我的 SharePointProjectItem.spdata 文件现在如下所示:
Thank you for your answers. I found the solution in the SharePointProjectItem.spdata file located in the list instance folder. Setting "DeploymentConflictResolutionBehavior" to "None" stopped Visual Studio from deleting my list on every deploy.
My SharePointProjectItem.spdata file now looks like this:
目前 SP 对象模型中没有方法来确定这一点。正如 Beytan 提到的,扩展方法可以帮助解决这个问题。我认为这个 link 是实现此类扩展方法的更好方式。它迭代整个列表集合,如果找到匹配则返回 true,如果没有则返回 false。以下是帖子中的代码。
由于您的列表定义和实例已在功能中,因此您可以从功能的事件接收器的 FeatureActivated 方法中调用扩展方法。
There is no method currently included in SP object model to determine this. As Beytan mentioned, an extension method can help solve this issue. I think the example in this link is a better way to implement this type of extension method. It iterates through the entire collection of lists, returning true if it finds a match and false if it does not. The following is the code from the post.
Since your list definitions and instances are already in a feature, you can call the extension method from the FeatureActivated method of the event receiver for your feature.
从代码(web.Lists.Add)创建实例,并使用它来检查是否已经存在:
web.Lists.TryGetList("listTitle")
Create instance from code (web.Lists.Add), and use this to check if already exists:
web.Lists.TryGetList("listTitle")
如何通过服务器端对象模型检查 SPList 是否存在:
How to check if SPList exists via Server Side Object Model: