Communardo Software GmbH, Kleiststraße 10 a, D-01129 Dresden
0800 1 255 255

Sharepoint Listenelement (SPListItem) in eine andere Liste kopieren

Dafür gibt es (theo­re­tisch) eine denk­bar ein­fa­che Lösung: SPListItem bie­tet eine Methode CopyTo(destinationUrl) an (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx) – lei­der scheint diese aber nicht (in jedem Fall?) zu funk­tio­nie­ren. Zumindest in mei­nem Fall (benut­zer­de­fi­nierte Liste mit benut­zer­de­fi­nier­tem Inhaltstyp UND Attachments – viel­leicht erwarte ich ein­fach auch zu viel von Sharepoint…) tat sie es nicht. Stattdessen erhielt ich fol­gende Exception: "Source item can­not be found. Verify that the item exist and that you have per­mis­sion to read it." Eine rasche Recherche bei Google brachte mir die Erkenntnis, dass andere Leute das glei­che Problem auch schon hat­ten – lei­der ohne ver­wert­bare Lösungsvorschläge…

Also erstel­len wir uns eben selbst eine kleine sta­ti­sche Methode, die das gewünschte tut:

Die Methodensignatur erwar­tet ein Quellelement und einen Listenname und gibt das kopierte Zielelement zurück:

image

Zuerst erstel­len wir das Zielelement in der ange­ge­be­nen Liste. Dann gehen wir Schritt für Schritt alle Fields des Quellelementes durch und kopie­ren diese zum Zielelement:

image

Achtung! Wir soll­ten nicht ver­su­chen, rea­donly Fields zu kopie­ren und auch die Attachments las­sen sich nicht auf diese Weise "abfer­ti­gen". Diese behan­deln wir fol­gen­der­ma­ßen:

image

Nun noch schnell das Zielelement gespei­chert und zurück­ge­ge­ben – fer­tig 🙂

image

So könnte z.B. der Aufruf der Methode aus­se­hen:

image

Zum bes­se­ren Kopieren hier das Ganze noch­mal als Text:

public sta­tic SPListItem CopyItem(SPListItem sourceItem, string destinationListName)
{
//copy sourceItem to destinationList
SPList destinationList = sourceItem.Web.Lists[destinationListName];
SPListItem targetItem = destinationList.Items.Add();
fore­ach (SPField f in sourceItem.Fields)
{
if (!f.ReadOnlyField && f.InternalName != "Attachments")
{
targetItem[f.InternalName] = sourceItem[f.InternalName];
}
}
//copy attach­ments
fore­ach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
targetItem.Attachments.Add(fileName, imageData);
}
targetItem.Update();
return targetItem;
}

Related Posts

Hi Dorrit,
möchte "Danke" sagen.
Das kann ich dem­nächst sehr gut gebrau­chen 🙂
Liebe Grüsse
aus Köln
Manfred

Hallo Dorrit,

vie­len Dank für die­sen SEHR hilf­rei­chen Beitrag.
Hat mir aktu­ell sehr gehol­fen 😉

LG Tom

[…] showed that I was not the only one struggling with this. Several solu­ti­ons are avail­able like: https://www.communardo.de/techblog/2008/01/08/sharepoint-listenelement-splistitem-in-eine-andere-list… (you can read the code… ) […]

Hi Dorrit,
The solu­tion works fine. But it does not copy the "Created By" field i.e. when a lis­ti­tem is copied to ano­t­her list, the "crea­ted by" is set to the sys­tem account and not the "crea­ted by" column from the source list. I have a requi­re­ment where i want to repli­cate the "Created by" field too.

Avatar Dorrit Riemenschneider

Hi pan­kaj, you should be able to solve your pro­blem by using the SPUserToken object. Here you can find a simi­liar (not just the same but I think you can adapt this) solu­tion: http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/41d34a5e-e53b-4d27-bc0a-4a4bb2107e6a
Cheers, Dorrit

Hallo!

Danke für den hilf­rei­chen Post! Wollte nur noch anmer­ken, dass beim kopie­ren ein "Assigned To" Field nicht mit­ko­piert wird, da (unter Umständen) der User in einer ande­ren Site Collection eine andere Userid haben kann 😉 )
Ansonsten funk­tio­niert das aber super!

viele Grüße aus München
Johannes

thank you, it was very hel­pful for me.

OMG.…you are a GENIUS ! Thank you so much for covering this pro­blem, your solu­tion save my day ! I really2 appre­ciate your effort for this ! 😀

Hello

I am having trou­ble imple­men­ting the code above. I have two site­collec­tions and want to copy a list item from one site collec­tion list to ano­t­her site collec­tion list.

Sitecollection 1name: mySiteCollection1

Sitecollection 2 name: mySiteCollection2

This is my code which is run in Site Collection 1. I have crea­ted the event hand­ler and have added it to the GAC and imple­men­ted the Feature.

public over­ride void ItemAdded(SPItemEventProperties pro­per­ties) {
SPListItem approvedItem = CopyItem(properties.ListItem, "http://localhost/mysitecollection2/mysubsite/Contacts/AllItems.aspx");
}

public sta­tic SPListItem CopyItem(SPListItem sourceItem, string destinationListName)
{
//copy sourceItem to destinationList
SPList destinationList = sourceItem.Web.Lists[destinationListName];
SPListItem targetItem = destinationList.Items.Add();
fore­ach (SPField f in sourceItem.Fields)
{
if (!f.ReadOnlyField && f.InternalName != "Contacts")
{
targetItem[f.InternalName] = sourceItem[f.InternalName];
}
}
//copy attach­ments
fore­ach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
targetItem.Attachments.Add(fileName, imageData);
}

targetItem.Update();
return targetItem;
}

Thank you

Yoshi

Avatar Dorrit Riemenschneider

Hello Yoshi, the code is meant for copy­ing items bet­ween two lists in the same site. The code line "SPList destinationList = sourceItem.Web.Lists[destinationListName];" won't work bet­ween site collec­tions.

Hello

Will it work for two lists in two sub­sites of the same site collec­tion?

Example:

Subsite A – List 1

To

Subsite B – List 2

Thank you

Yoshi

Avatar Dorrit Riemenschneider

Hello Yoshi, same pro­blem regar­ding code line "SPList destinationList = sourceItem.Web.Lists[destinationListName];". Of course you can adapt it in order to bring it to work bet­ween dif­fe­rent sites.

Hi Dorrit

Thank you for sharing the code above. I have mana­ged to get this to work with 2 iden­ti­cal cus­tom lists in the same site. However, when I add a new list item to List A, it will create appro­xi­mately 10 iden­ti­cal items in List B. Any clues on how to solve this?

Thank you.

Yoshi

Avatar Dorrit Riemenschneider

Hello Yoshi, as funny as it sounds: I think you must have done some­thing wrong.

Hello. That is a nce solu­tion, howe­ver I can't get it working with External Data column (that con­tains a refe­rence to exter­nal con­tent type). The text comun con­tent is being copied, the exter­nal data column stays empty. Do you know if it is pos­si­ble at all? Cheers.

Avatar Dorrit Riemenschneider

For External Data columns it has to pos­si­ble, too. I think you had to verify whe­ther it con­cerns an External Data column and if so, then put some code there refe­ren­cing the exter­nal con­tent type.

Danke!!!

Comments are closed.

Pin It on Pinterest