2009-07-17 8 views
1

もうSPListに1社のSPListからSPListitemをコピーする方法:ここ <p></p>が動作していないコードで、私は別の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(); 
} 
+0

チェックアウト:http://stackoverflow.com/questions/1059175 –

答えて

3

がSPListItemの種類がありあなたが望むことをするCopyToメソッド。

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

+0

が、私は読んで同じサイトに、CopyToメソッドのみSPListitemsがオブジェクトではなく、ファイルのために働きます。 – Azra

+0

これはリスト項目にも適用されます。http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx –

+0

これはカスタムリストの場合は機能しません。 http://blogs.msdn.com/b/bgeorgi/archive/2008/03/06/the-splistitem-copyto-method-doesn-t-seem-to-work-on-a-custom-list.aspx –

0

、この記事を見てlink。これが私が見つけた最良のアプローチです。

0
  1. あらゆる分野のDestItem の "更新" を呼び出さないでください。あなたは一度だけ が必要です!
  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! 
     } 
     } 
     } 
+0

これはECMA Scriptを使用して可能ですか?現在、私の環境はVS 2010がインストールされておらず、dev.serverの完全な制御権がありません – SaMolPP

3

を表示するようにコードを変更しました。 this articleを見ると、コードの一部が以下に繰り返されています。

// ** 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();