using (SPSite site = new SPSite("http://server:name/sites/"))
{
site.AllowUnsafeUpdates = true;
using (SPWeb webApp = site.OpenWeb())
{
webApp.AllowUnsafeUpdates = true;
SPList list = webApp.Lists["list"];
SPQuery query = new SPQuery();
query.Query = @"<OrderBy> <FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";
SPListItemCollection itemCollection = list.GetItems(query);
SPListItem sourceItem = itemCollection[0];
SPFolder mylibrary = webApp.GetFolder("Meeting Documents");
// Copy Attachmets from source list to destination Library
if (sourceItem.Fields.ContainsField("Attachments"))
{
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
webApp.Files.Add("Shared%20Documents/" + System.IO.Path.GetFileName(file.Url), imageData,true);
}
}
webApp.AllowUnsafeUpdates = false;
}
site.AllowUnsafeUpdates = false;
}
{
site.AllowUnsafeUpdates = true;
using (SPWeb webApp = site.OpenWeb())
{
webApp.AllowUnsafeUpdates = true;
SPList list = webApp.Lists["list"];
SPQuery query = new SPQuery();
query.Query = @"<OrderBy> <FieldRef Name='ID' Ascending='FALSE' /></OrderBy>";
SPListItemCollection itemCollection = list.GetItems(query);
SPListItem sourceItem = itemCollection[0];
SPFolder mylibrary = webApp.GetFolder("Meeting Documents");
// Copy Attachmets from source list to destination Library
if (sourceItem.Fields.ContainsField("Attachments"))
{
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
webApp.Files.Add("Shared%20Documents/" + System.IO.Path.GetFileName(file.Url), imageData,true);
}
}
webApp.AllowUnsafeUpdates = false;
}
site.AllowUnsafeUpdates = false;
}
No comments:
Post a Comment