I'm trying to add a document from one doclibrary to another via
SPFileCollection.Add method. The problem is that six months
ago it just worked fine but now it doesn't. It is probably a permission
issue, but I don't know.
I get "External component has thrown an exception." error.
Does anyone have a work around for this problem?
Please help.
Thank You
void Page_Load()
{
SPWeb web = SPControl.GetContextWeb(Context);
string path = Request.QueryString["FileName"];
SPFile file = web.GetFile(path);
if(file.Exists)
{
ConfigHandler ch = new ConfigHandler();
string SERVER_URL = ch.GetSystemSetting("ServerUrl");
string ARCHIVE_PATH = ch.GetSystemSetting("ArchivePath");
string MAIL_FROM = ch.GetSystemSetting("MailFrom");
string MAIL_SERVER = ch.GetSystemSetting("MailServer");
int startPos = path.LastIndexOf("/", path.Length-file.Name.Length-2)+1;
int endPos = path.IndexOf("/", startPos);
string docFolder = path.Substring(startPos, endPos-startPos);
SPSite site = new SPSite(SERVER_URL + ARCHIVE_PATH);
SPWeb archiveWeb = site.OpenWeb();
SPFileCollection archive = archiveWeb.Folders[docFolder].Files;
string destUrl = archive.Folder.Url + "/" + file.Name;
byte[] binFile = file.OpenBinary();
// Copy file to archive
archiveWeb.AllowUnsafeUpdates = true;
archiveWeb.Add(destUrl, binFile, true);
archiveWeb.AllowUnsafeUpdates = false;
string docEMail = file.Properties["E-mail"].ToString();
// Delete file from working folder
web.AllowUnsafeUpdates = true;
web.Folders[docFolder].Files.Delete(file.Url);
web.AllowUnsafeUpdates = false;
// Send mail to the owner
MailMessage mail = new MailMessage();
mail.From = MAIL_FROM;
mail.To = docEMail;
mail.Subject = "Arkivering av dokumentet " + file.Name;
mail.Body = "Dokumentet har blivit flyttat till arkivet:\n" + SERVER_URL +
ARCHIVE_PATH + "/" + docFolder;
SmtpMail.SmtpServer = MAIL_SERVER;
SmtpMail.Send(mail);
//Response.Write("<script javascript=\"JavaScript\">history.go(-1);</" +
"script>");
Response.Redirect(SERVER_URL + ARCHIVE_PATH + "/" + docFolder);
}
else
{
Response.Write("The file does not exist.");
}