0

I have to modify a .Net console application written in C#.
The application references the following assemblies :

  • Microsoft.SharePoint
  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

The current application copies items from a SharePoint 2010 source list to a SP2010 target list.
The two lists do not belong to the same web application.

Here is the main code block :

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem l_targetItem = l_targetList.AddItem(itemCreateInfo);

foreach (Field l_field in l_sourceFields)
{
    if (!l_field.ReadOnlyField && l_field.InternalName != "Attachments" && l_field.InternalName != "ContentType")
    {
        l_targetItem[l_field.InternalName] = l_sourceItem[l_field.InternalName];
    }
}

For each item to copy :
The application adds an item into the target list.
Then the application sets target item's fields with the values of the corresponding source item's fields.
If the copy succeed, the application deletes the source item.

What I have to do

A source item can have an attachment.
If so, the application must copy the attachment to the target item.
If the copy succeed, the application must delete the source item's attachment.

I am beginner in SharePoint programming.
Can someone please help me to achieve my goals ?
Any help will be greatly appreciated

0
private SPListItem CopyItem(SPListItem sourceItem, string destinationListName) {
    //Copy sourceItem to destinationList
    SPList destinationList = sourceItem.Web.Lists[destinationListName];
    SPListItem targetItem = destinationList.Items.Add();
    foreach (SPField f in sourceItem.Fields) {
        //Copy all except attachments.
        if (!f.ReadOnlyField && f.InternalName != "Attachments"
            && null != sourceItem[f.InternalName])
        {
            targetItem[f.InternalName] = sourceItem[f.InternalName];
        }
    }
    //Copy attachments
    foreach (string fileName in sourceItem.Attachments) {
        SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
        byte[] imageData = file.OpenBinary();
        targetItem.Attachments.Add(fileName, imageData);
    }

    return targetItem;
}

private void CopyAttachmentsToList(SPListItem srcItem, SPListItem tgtItem)
{
    try
    {
       //get source item attachments from the folder
        SPFolder srcAttachmentsFolder = srcItem.Web.Folders["Lists"].SubFolders[srcItem.ParentList.Title].SubFolders["Attachments"].SubFolders[srcItem.ID.ToString()];

    //Add items to the target item
    foreach (SPFile file in srcAttachmentsFolder.Files)
    {
        byte[] binFile = file.OpenBinary();
        tgtItem.Update();
        tgtItem.Attachments.AddNow(file.Name, binFile);
        tgtItem.Update();
    }
}
catch
{
    //exception message goes here
}
finally
{
    srcItem.Web.Dispose();
}

}

More details can be seen in below link: https://stackoverflow.com/questions/1075323/copy-list-items-from-one-list-to-another-in-sharepoint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.