1

ある SPList から別の SPList にアイテムをコピーする必要があります。

動作していないコードは次のとおりです。

public void CopyList(SPList src)
{
    //Copy items from source List to Destination List
    foreach (SPListItem item in src.Items)
    {
        if(isUnique(item.UniqueId))
        {
            foreach (SPField field in src.Fields)
            {
               try
               {
                    if (!field.ReadOnlyField)
                        newDestItem = DestinationList.Items.Add();
                    newDestItem[field.Id] = item[field.Id];
                    newDestItem.Update();
               }
               catch (Exception ex)
               {
                   ex.ToString();
               }
            }
            //newDestItem["wrkspace"] = src.ParentWeb.Name;
            // newDestItem.Update();
        }
        DestinationList.Update();  
    }
    // DestinationList.Update();
}
4

4 に答える 4

3

アイテムの添付ファイルをコピーするのを忘れました。この記事を見てください。コードの一部が以下で繰り返されています。

// ** Copy the fields
foreach(SPField field in sourceItem.Fields)
{
    if (newItem.Fields.ContainsField(field.InternalName) == true && 
        field.ReadOnlyField == false && field.InternalName != "Attachments")
    {
       newItem[field.InternalName] = sourceItem[field.InternalName];
    }
}

// ** Delete any existing attachments in the target item
for (int i = newItem.Attachments.Count; i > 0; i-- )
{
    newItem.Attachments.Delete(newItem.Attachments[i-1]);
}

// ** Copy any attachments
foreach (string fileName in sourceItem.Attachments)
{
    SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + 
                                                          fileName);
    byte[] imageData = file.OpenBinary();
    newItem.Attachments.Add(fileName, imageData);
}

// ** Remember where the original was copied from so we can update it in the future
newItem["_M_CopySource"] = sourceItem["FileRef"];

newItem.Update();
于 2010-01-28T16:42:10.963 に答える
3

SPListItem 型には、必要な処理を行う CopyTo メソッドがあります。

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx

于 2009-07-17T08:42:45.400 に答える
0
  1. すべてのフィールドの DestItem で「update」を呼び出さないでください。一度だけ必要です。
  2. field.id異なるリストでは異なる場合があります。InternalName代わりにプロパティを使用してください。
  3. キャッチした例外はどこにも保存されません!
  4. を呼び出すDestionationList.Update必要はありません。宛先リストの設定などを変更しているわけではありません。

これらの変更を表示するためにコードを修正しました

public void CopyList(SPList src) 
{ 
    //Copy items from source List to Destination List 
    foreach (SPListItem item in src.Items) 
    { 
        if(isUnique(item.UniqueId)) 
        { 
          newDestItem = DestinationList.Items.Add(); 

          foreach (SPField field in src.Fields) 
          { 
             try 
              { 
                if ((!field.ReadOnlyField) && (field.InternalName!="Attachments"))
                  newDestItem[field.InternalName] = item[field.InternalName]; 
               } 
             catch (Exception ex) 
              { 
              //you should save the "ex" somewhere to see its outputs
               ex.ToString(); 
              } 
           } 
           newDestItem.Update();  //only now you call update!
        } 
       } 
      } 
于 2010-01-07T11:44:29.877 に答える
0

この投稿、リンクを見てください。これは私が見つけた最良のアプローチです。

于 2010-01-07T11:27:50.283 に答える