问题描述
你好
我正在为一个部门创建一个新的SharePoint网站,该部门具有一个称为日志"的列表,该列表可以跟踪他们的案件.以前有一个站点,上面有13,500个案例.由于新网站的新结构非常不同,因此我下载了数据, 创建了各种Excel宏进行修改,然后将其输出到四个文本文件中.我现在正在使用PowerShell脚本将此数据上传到新站点.请注意,此列表中有三种内容类型,一列用于跟踪评论 时间来使用版本历史记录.我正在PowerShell中使用update命令来更新主要信息和所有版本历史信息.我收到许多保存冲突"消息,这些行上的错误.也会删除许多第一条评论 在版本历史记录中.到目前为止,这是我的脚本:
I am building a new SharePoint site for a division that has a list called the 'Log' that tracks their cases. There was a previous site with 13,500 cases on it. Since the new structure is very different in the new site I have downloaded the data, created various Excel macros to modify it, and then output it to four text files. I am now using a PowerShell script to upload this data to the new site. Note that there are three content types in this list and a column which tracks comments over time using version history. I am using the update command in PowerShell to update the main information and all the version history information. I get many "Save Conflict" errors on these lines. Also it drops many of the first comments in the version history. Here is my script so far:
$ SITE_URL ="http://sharepointrd/sites/MySite";
$ LIST_NAME ="CaseLog";
$ TYPE1_FILE ="e:\ TomBrown \ type1.txt" ;;
$ TYPE2_FILE ="e:\ TomBrown \ type2.txt" ;;
$ TYPE3_FILE =" e:\ TomBrown \ type3.txt" ;;
$ VERSION_FILE =" e:\ TomBrown \ version.txt" ;;
#允许访问SharePoint(如果尚未访问的话)
if((Get-PSSnapin |其中{$ _.Name -eq" Microsoft.SharePoint.PowerShell"})-eq $ null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$ host.Runspace.ThreadOptions =" ReuseThread" ;;
#打开Sharepoint网站
$ SPWeb = Get-SPWeb $ SITE_URL;
#打开我们正在处理的列表
$ list = $ SPWeb.lists [$ LIST_NAME];
#获取内容类型ID
$ type1 = $ list.ContentTypes ["Type 1"];
$ type2 = $ list.ContentTypes ["Type 2"];
$ type3 = $ list.ContentTypes ["Type 3"];
#导入内容类型文件
$ data1 = Import-Csv -Delimiter`t"; $ TYPE1_FILE;
$ data2 = Import-Csv -Delimiter`t"; $ TYPE2_FILE;
$ data3 = Import-Csv-Delimiter`t"; $ TYPE3_FILE;
#导入版本历史记录
$ versionData = Import-Csv -Delimiter`t"; $ VERSION_FILE;
#遍历所有数据类型为1的情况
foreach($ data1中的$ type1Case){
#为此SharePoint列表创建一个新项目
$ newItem = $ list.Items.Add();
#设置为此内容类型
$ newItem ["ContentTypeId"] = $ type1.Id;
$ id = $ type1Case.ID;
#添加常规列
$ newItem ["Created"] = $($ type1Case.Created).ToString();
$ userEmail = $($ type1Case.CreatedBy).ToString();
$ newItem ["Created By"] = $ SPWeb.EnsureUser($ userEmail);
$ userEmail = $($ type1Case.Advisor).ToString();
$ newItem ["Advisor"] = $ SPWeb.EnsureUser($ userEmail);
$ newItem [提交日期"] = $($ type1Case.DateSubmitted).ToString();
$ newItem ["Manager"] = $($ type1Case.Manager).ToString();
$ newItem ["Case Region"] = $($ type1Case.CaseRegion).ToString();
$ newItem ["Organization"] = $($ type1Case.Organization).ToString();
$ newItem [案例状态"] = $($ type1Case.CaseStatus).ToString();
#添加类型1的特定列
$ newItem ["Type 1 Sub-Categories"] = $($ type1Case.Type1SubCategories).ToString();
$ IsDog = $($ type1Case.IsDog).ToString();
if($ IsDog.Equals("Yes")){
$ newItem ["IsDog?"] = $ true;
}其他{
$ newItem ["IsDog?"] = $ false;
}
#获取版本历史记录并将其插入
foreach($ versionData中的$ version){
if($ version.ID -eq $ type1Case.ID){
$ newItem ["Modified"] = $($ version.Modified).ToString();
$ userEmail = $($ version.ModifiedBy).ToString();
$ newItem ["Modified By"] = $ SPWeb.EnsureUser($ userEmail);
$ newItem ["Chronology"] = $($ version.Chronology).ToString();
$ newItem.update();
}
}
}#for for循环遍历类型1的案例行
#遍历所有2类案例
foreach($ data2中的$ type2Case){
#为此SharePoint列表创建一个新项目
$ newItem = $ list.Items.Add();
#设置类型
$ newItem ["ContentTypeId"] = $ type2.Id;
$ id = $ type2Case.ID;
#在此条目中添加常规列
$ newItem ["Created"] = $($ type2Case.Created).ToString();
$ userEmail = $($ type2Case.CreatedBy).ToString();
$ newItem ["Created By"] = $ SPWeb.EnsureUser($ userEmail);
$ newItem [提交日期"] = $($ type2Case.DateSubmitted).ToString();
$ newItem ["Manager"] = $($ type2Case.Manager).ToString();
$ newItem ["Case Region"] = $($ type2Case.CaseRegion).ToString();
$ newItem ["Organization"] = $($ type2Case.Organization).ToString();
$ newItem [案例状态"] = $($ type2Case.CaseStatus).ToString();
#2类特定列
$ IsCatCase = $($ type2Case.IsCatCase).ToString();
if($ IsCatCase.Equals("Yes"))){
$ newItem ["Cat Case?"] = $ true;
}其他{
$ newItem ["Cat Case?"] = $ false;
}
if($ type2Case.CatSubCategories){
$ newItem ["Cat Sub-Categories"] = $($ type2Case.CatSubCategories).ToString();
}
$ IsRabbitCase = $($ type2Case.IsRabbitCase).ToString();
if($ IsRabbitCase.Equals("Yes"))){
$ newItem [兔子案例?"] = $ true;
}其他{
$ newItem [兔子大小写?"] = $ false;
}
if($ type2Case.RabbitSubCategories){
$ newItem [兔子子类别"] = $($ type2Case.RabbitSubCategories).ToString();
}
#获取版本历史记录
foreach($ versionData中的$ version){
if($ version.ID -eq $ type2Case.ID){
$ newItem ["Modified"] = $($ version.Modified).ToString();
$ userEmail = $($ version.ModifiedBy).ToString();
$ newItem ["Modified By"] = $ SPWeb.EnsureUser($ userEmail);
$ newItem ["Chronology"] = $($ version.Chronology).ToString();
$ newItem.update();
}
}
}#for for循环遍历2种情况
#遍历所有3类案例
foreach($ data3中的$ type3Case){
#为此SharePoint列表创建一个新项目
$ newItem = $ list.Items.Add();
#设置内容类型
$ newItem ["ContentTypeId"] = $ type3.Id;
$ id = $ type3Case.ID;
#通用列
$ newItem ["Created"] = $($ type3Case.Created).ToString();
$ userEmail = $($ type3Case.CreatedBy).ToString();
$ newItem ["Created By"] = $ SPWeb.EnsureUser($ userEmail);
$ userEmail = $($ type3Case.Advisor).ToString();
$ newItem ["Advisor"] = $ SPWeb.EnsureUser($ userEmail);
$ newItem [提交日期"] = $($ type3Case.DateSubmitted).ToString();
$ newItem ["Manager"] = $($ type3Case.Manager).ToString();
$ newItem ["Case Region"] = $($ type3Case.CaseRegion).ToString();
$ newItem ["Workplace"] = $($ type3Case.Workplace).ToString();
$ newItem ["Organization"] = $($ type3Case.Organization).ToString();
$ newItem [案例状态"] = $($ type3Case.CaseStatus).ToString();
#类型3的特定列
if($ type3Case.type3SubCategories){
$ newItem [类型3子类别"] = $($ type3Case.type3SubCategories).ToString();
}
if($ type3Case.DateResolved){
$ newItem [已解决的日期"] = $($ type3Case.DateResolved).ToString();
}
#获取版本历史记录
foreach($ versionData中的$ version){
if($ version.ID -eq $ type3Case.ID){
#执行更新将更新非版本列中的所有内容,并为版本列创建新版本
$ newItem ["Modified"] = $($ version.Modified).ToString();
$ userEmail = $($ version.ModifiedBy).ToString();
$ newItem ["Modified By"] = $ SPWeb.EnsureUser($ userEmail);
$ newItem ["Chronology"] = $($ version.Chronology).ToString();
$ newItem.update();
}
}
}#for for循环遍历3种情况
#释放SharePoint资源
$ SPWeb.Dispose();
$SITE_URL = "http://sharepointrd/sites/MySite";
$LIST_NAME = "CaseLog";
$TYPE1_FILE = "e:\TomBrown\type1.txt";
$TYPE2_FILE = "e:\TomBrown\type2.txt";
$TYPE3_FILE = "e:\TomBrown\type3.txt";
$VERSION_FILE = "e:\TomBrown\version.txt";
# Allow access to SharePoint if not already
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$host.Runspace.ThreadOptions = "ReuseThread";
# Open the Sharepoint site
$SPWeb = Get-SPWeb $SITE_URL;
# Open the list we are working on
$list = $SPWeb.lists[$LIST_NAME];
# Get content type ids
$type1 = $list.ContentTypes["Type 1"];
$type2 = $list.ContentTypes["Type 2"];
$type3 = $list.ContentTypes["Type 3"];
# Import the content type files
$data1 = Import-Csv -Delimiter "`t" $TYPE1_FILE;
$data2 = Import-Csv -Delimiter "`t" $TYPE2_FILE;
$data3 = Import-Csv -Delimiter "`t" $TYPE3_FILE;
# Import the version history
$versionData = Import-Csv -Delimiter "`t" $VERSION_FILE;
# Loop through all cases of data type 1
foreach ($type1Case in $data1) {
# Create a new item for this SharePoint list
$newItem = $list.Items.Add();
# Set to this content type
$newItem["ContentTypeId"] = $type1.Id;
$id = $type1Case.ID;
# Add general columns
$newItem["Created"] = $($type1Case.Created).ToString();
$userEmail = $($type1Case.CreatedBy).ToString();
$newItem["Created By"] = $SPWeb.EnsureUser($userEmail);
$userEmail = $($type1Case.Advisor).ToString();
$newItem["Advisor"] = $SPWeb.EnsureUser($userEmail);
$newItem["Date Submitted"] = $($type1Case.DateSubmitted).ToString();
$newItem["Manager"] = $($type1Case.Manager).ToString();
$newItem["Case Region"] = $($type1Case.CaseRegion).ToString();
$newItem["Organization"] = $($type1Case.Organization).ToString();
$newItem["Case Status"] = $($type1Case.CaseStatus).ToString();
# Add type 1 specific columns
$newItem["Type 1 Sub-Categories"] = $($type1Case.Type1SubCategories).ToString();
$IsDog = $($type1Case.IsDog).ToString();
if ($IsDog.Equals("Yes")) {
$newItem["IsDog?"] = $true;
} else {
$newItem["IsDog?"] = $false;
}
# Get the version history and insert it
foreach ($version in $versionData) {
if ($version.ID -eq $type1Case.ID) {
$newItem["Modified"] = $($version.Modified).ToString();
$userEmail = $($version.ModifiedBy).ToString();
$newItem["Modified By"] = $SPWeb.EnsureUser($userEmail);
$newItem["Chronology"] = $($version.Chronology).ToString();
$newItem.update();
}
}
} #end of for loop through type 1 case rows
# Loop through all type 2 cases
foreach ($type2Case in $data2) {
# Create a new item for this SharePoint list
$newItem = $list.Items.Add();
# Set the type
$newItem["ContentTypeId"] = $type2.Id;
$id = $type2Case.ID;
# Add general columns to this entry
$newItem["Created"] = $($type2Case.Created).ToString();
$userEmail = $($type2Case.CreatedBy).ToString();
$newItem["Created By"] = $SPWeb.EnsureUser($userEmail);
$newItem["Date Submitted"] = $($type2Case.DateSubmitted).ToString();
$newItem["Manager"] = $($type2Case.Manager).ToString();
$newItem["Case Region"] = $($type2Case.CaseRegion).ToString();
$newItem["Organization"] = $($type2Case.Organization).ToString();
$newItem["Case Status"] = $($type2Case.CaseStatus).ToString();
# Type 2 specific columns
$IsCatCase = $($type2Case.IsCatCase).ToString();
if ($IsCatCase.Equals("Yes")) {
$newItem["Cat Case?"] = $true;
} else {
$newItem["Cat Case?"] = $false;
}
if ($type2Case.CatSubCategories) {
$newItem["Cat Sub-Categories"] = $($type2Case.CatSubCategories).ToString();
}
$IsRabbitCase = $($type2Case.IsRabbitCase).ToString();
if ($IsRabbitCase.Equals("Yes")) {
$newItem["Rabbit Case?"] = $true;
} else {
$newItem["Rabbit Case?"] = $false;
}
if ($type2Case.RabbitSubCategories) {
$newItem["Rabbit Sub-Categories"] = $($type2Case.RabbitSubCategories).ToString();
}
# Get the version history
foreach ($version in $versionData) {
if ($version.ID -eq $type2Case.ID) {
$newItem["Modified"] = $($version.Modified).ToString();
$userEmail = $($version.ModifiedBy).ToString();
$newItem["Modified By"] = $SPWeb.EnsureUser($userEmail);
$newItem["Chronology"] = $($version.Chronology).ToString();
$newItem.update();
}
}
} #end of for loop through type 2 cases
# Loop through all type 3 cases
foreach ($type3Case in $data3) {
# Create a new item for this SharePoint list
$newItem = $list.Items.Add();
# Set the content type
$newItem["ContentTypeId"] = $type3.Id;
$id = $type3Case.ID;
# Common Columns
$newItem["Created"] = $($type3Case.Created).ToString();
$userEmail = $($type3Case.CreatedBy).ToString();
$newItem["Created By"] = $SPWeb.EnsureUser($userEmail);
$userEmail = $($type3Case.Advisor).ToString();
$newItem["Advisor"] = $SPWeb.EnsureUser($userEmail);
$newItem["Date Submitted"] = $($type3Case.DateSubmitted).ToString();
$newItem["Manager"] = $($type3Case.Manager).ToString();
$newItem["Case Region"] = $($type3Case.CaseRegion).ToString();
$newItem["Workplace"] = $($type3Case.Workplace).ToString();
$newItem["Organization"] = $($type3Case.Organization).ToString();
$newItem["Case Status"] = $($type3Case.CaseStatus).ToString();
# Type 3 specific columns
if ($type3Case.type3SubCategories) {
$newItem["Type 3 Sub-Categories"] = $($type3Case.type3SubCategories).ToString();
}
if ($type3Case.DateResolved) {
$newItem["Date Resolved"] = $($type3Case.DateResolved).ToString();
}
# Get the version history
foreach ($version in $versionData) {
if ($version.ID -eq $type3Case.ID) {
# Doing update will update everything in non-version columns plus create a new version for the version columns
$newItem["Modified"] = $($version.Modified).ToString();
$userEmail = $($version.ModifiedBy).ToString();
$newItem["Modified By"] = $SPWeb.EnsureUser($userEmail);
$newItem["Chronology"] = $($version.Chronology).ToString();
$newItem.update();
}
}
} #end of for loop through type 3 cases
# Release SharePoint resources
$SPWeb.Dispose();
推荐答案
请尝试删除版本历史记录代码,并检查其是否有效.
Please try to remove the version history code and check if it works.
或者我们可以先新建项目,然后根据版本历史记录更新列表项目(分为两个步骤).
Or we can new the item firstly, then update the list item basic on the version history(split two steps).
或者尝试使用以下实用程序方法来简化项目的重新加载.
Or try to use the following utility method to ease the reloading of items.
public static SPListItem ReloadListItem(SPListItem item)
{
if (item == null)
return null;
return item.ParentList.GetItemByUniqueId(item.UniqueId);
}
更多信息在这里:
最好的问候,
丹尼斯
这篇关于使用PowerShell脚本将数据插入具有版本历史记录的列表中时保存冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!