/// <summary>
/// Method that copies attachments from one item to another
/// </summary>
/// <param name="currentWeb">Current web object</param>
/// <param name="sourceListName">The name of the list in which the source item is available</param>
/// <param name="targetListName">The name of the list in which the target item is available, provide the name even if both the source and target list are the same</param>
/// <param name="sourceItemId">Item id of the source item</param>
/// <param name="targerItemId">Item id of the target item</param>
/// <returns>Boolean value</returns>
public static bool CopyAttachmentsFromOneItemToOther(SPWeb currentWeb, string sourceListName, string targetListName, int sourceItemId, int targerItemId)
{
bool operationComplete = false;
try
{
SPAttachmentCollection attachmentCollection = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
currentWeb.AllowUnsafeUpdates = true;
SPList sourceList = currentWeb.Lists.TryGetList(sourceListName);
SPList targetList = currentWeb.Lists.TryGetList(targetListName);
if (sourceList != null)
{
SPListItem targetItem = null;
SPListItem sourceItem = sourceList.Items.GetItemById(sourceItemId);
if (targetList != null)
{
targetItem = targetList.Items.GetItemById(targerItemId);
}
if (sourceItem != null && targetItem != null)
{
attachmentCollection = sourceItem.Attachments;
if (attachmentCollection.Count > 0)
{
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
StreamReader fsReader = new StreamReader(file.OpenBinaryStream());
byte[] contents = null;
using (Stream fStream = fsReader.BaseStream)
{
contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
}
// byte[] fileData = file.OpenBinary();
targetItem.Attachments.Add(fileName, contents);
}
targetItem.Update();
}
}
}
else
{
LoggingService.WriteExceptionsToLog("Common", Constants.FORMATTEDERRORMESSAGE, "CopyAttachmentsFromOneItemToOther","","");
operationComplete = false;
}
});
}
catch (Exception ex)
{
operationComplete = false;
LoggingService.WriteExceptionsToLog("Common", Constants.FORMATTEDERRORMESSAGE, "CopyAttachmentsFromOneItemToOther", ex.Message, ex.StackTrace);
throw;
}
finally
{
currentWeb.AllowUnsafeUpdates = false;
}
return operationComplete;
}
/// Method that copies attachments from one item to another
/// </summary>
/// <param name="currentWeb">Current web object</param>
/// <param name="sourceListName">The name of the list in which the source item is available</param>
/// <param name="targetListName">The name of the list in which the target item is available, provide the name even if both the source and target list are the same</param>
/// <param name="sourceItemId">Item id of the source item</param>
/// <param name="targerItemId">Item id of the target item</param>
/// <returns>Boolean value</returns>
public static bool CopyAttachmentsFromOneItemToOther(SPWeb currentWeb, string sourceListName, string targetListName, int sourceItemId, int targerItemId)
{
bool operationComplete = false;
try
{
SPAttachmentCollection attachmentCollection = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
currentWeb.AllowUnsafeUpdates = true;
SPList sourceList = currentWeb.Lists.TryGetList(sourceListName);
SPList targetList = currentWeb.Lists.TryGetList(targetListName);
if (sourceList != null)
{
SPListItem targetItem = null;
SPListItem sourceItem = sourceList.Items.GetItemById(sourceItemId);
if (targetList != null)
{
targetItem = targetList.Items.GetItemById(targerItemId);
}
if (sourceItem != null && targetItem != null)
{
attachmentCollection = sourceItem.Attachments;
if (attachmentCollection.Count > 0)
{
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
StreamReader fsReader = new StreamReader(file.OpenBinaryStream());
byte[] contents = null;
using (Stream fStream = fsReader.BaseStream)
{
contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
}
// byte[] fileData = file.OpenBinary();
targetItem.Attachments.Add(fileName, contents);
}
targetItem.Update();
}
}
}
else
{
LoggingService.WriteExceptionsToLog("Common", Constants.FORMATTEDERRORMESSAGE, "CopyAttachmentsFromOneItemToOther","","");
operationComplete = false;
}
});
}
catch (Exception ex)
{
operationComplete = false;
LoggingService.WriteExceptionsToLog("Common", Constants.FORMATTEDERRORMESSAGE, "CopyAttachmentsFromOneItemToOther", ex.Message, ex.StackTrace);
throw;
}
finally
{
currentWeb.AllowUnsafeUpdates = false;
}
return operationComplete;
}



No comments:
Post a Comment